Probleme bei der Collisionserkennung

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Lowpoly
User
Beiträge: 3
Registriert: Donnerstag 30. November 2017, 02:51

Hallo

Ich komme bei der Collisionserkennung einfach nicht mehr weiter, Ich habe komplett google durch aber nichts will Funktionieren, vielleicht kann mir hier jemand weiterhelfen, würde mich sehr Freuen.

Also ich habe meinen Code in drei File geteilt (Main.py, Player.py und World.py), Jetzt will ich das sich das Objekt Player mit einem Objekt aus World Collidiert.

MAIN.py

Code: Alles auswählen

import pygame
from player import *
from world import *
from pygame.locals import *

#Define Display surface
W, H = 800, 600
WH, HH = W / 2, H / 2
AREA = W * H

# initialisiere Display
pygame.init()
CLOCK = pygame.time.Clock()
SCREEN = pygame.display.set_mode((W, H))
pygame.display.set_caption('DaniTest')
FPS = 60

# Define some Colors
DARKSKYBLUE = (0, 174, 255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

DONE = True
P = Player(H, W, SCREEN, 3, 30)
B = Block(0, 580, 800, 20, BLACK, SCREEN)
B1 = Block(300, 550, 30, 30, BLACK, SCREEN)
P.setLocation(WH, 0, 32, 32)

Collision = P.detectCollision(P.x, P.y, P.w, P.h, B1.x, B1.y, B1.w, B1.h)

# Gameloop
while DONE:
    for event in pygame.event.get():
       if event.type == QUIT:
            DONE = False

    SCREEN.fill(DARKSKYBLUE)

    P.do(Collision)
    B.draw(False)
    B1.draw(False)

    pygame.display.flip()
    pygame.display.update()
    CLOCK.tick(FPS)


PLAYER.py

Code: Alles auswählen

import pygame
from world import *
from pygame.locals import *

class Player(pygame.sprite.Sprite):
    def __init__(self, windowHeight, windowWidth, surface, velocity, maxJumpRange):
        self.velocity = velocity
        self.maxJumpRange = maxJumpRange
        self.H = windowHeight
        self.W = windowWidth
        self.DS = surface
        self.image = pygame.image.load('Images/Player.png')
        self.rect = self.image.get_rect()
        self.numImage = 16
        self.currentImage = 0

    def setLocation(self, x, y, width, height):
        self.x = x
        self.y = y
        self.w = width
        self.h = height
        self.xVelocity = 0
        self.jumping = False
        self.jumpCounter = 0
        self.falling = True

    def keys(self, Collision):
        if Collision == True:
            print ('Funktionier')
        KEYS = pygame.key.get_pressed()
        if KEYS[K_a]:
            self.x -= 5
        if KEYS[K_d]:
            self.x += 5
        else:
            self.xVelocity = 0

        if self.x < 0:
            self.x = 0
        if self.x > self.W - self.w:
            self.x = self.W - self.w

        if KEYS[K_SPACE] and not self.jumping and not self.falling:
            self.jumping = True
            self.jumpCounter = 0

    def detectCollision(self, x1, y1, w1, h1, x2, y2, w2, h2):

        if x2+w2 >= x1 >= x2 and y2+h2 >= y1 >= y2:
            return True
        elif x2+w2 >= x1+w1 >= x2 and y2+h2 >= y1 >= y2:
            return True
        elif x2+w2 >= x1 >= x2 and y2+h2 >= y1+h1 >= y2:
            return True
        elif x2+w2 >= x1+w1 >= x2 and y2+h2 >= y1+h1 >= y2:
            return True
        else:
            return False


    def move(self):

        if self.jumping:
            self.y -= self.velocity
            self.jumpCounter += 1
            if self.jumpCounter == self.maxJumpRange:
                self.jumping = False
                self.falling = True
        elif self.falling:
            if self.y <= self.H -52 and self.y + self.velocity >= self.H - 52:
                self.y = self.H -52
                self.falling = False
            else:
                self.y += self.velocity

        if self.currentImage >= self.numImage -1:
            self.currentImage = 0
        else:
            self.currentImage += 1

    def draw(self):
        self.DS.blit(self.image, (self.x, self.y), (self.currentImage * self.w, 0, self.w, self.h))

    def do(self, Collision):
        self.keys(Collision)
        self.move()
        self.draw()
WORLD.py

Code: Alles auswählen

import pygame

class Block(pygame.sprite.Sprite):
    def __init__(self, x, y, w, h, color, surface):
        super(Block, self).__init__()
        self.w = w
        self.h = h
        self.x = x
        self.y = y
        self.DS = surface
        self.color = color
        self.rect = pygame.Rect(self.x, self.y, self.w, self.h)

    def draw(self, Collision):
        pygame.draw.rect(self.DS, self.color, (self.x, self.y, self.w, self.h), 1)
Vielen Dank im vorraus..
__deets__
User
Beiträge: 14533
Registriert: Mittwoch 14. Oktober 2015, 14:29

Das ist schon implementiert in pygame in Form der Rect-Klasse. Alle Objekte - Block und Player - bekommen ein solches rect, bzw. erzeugen es aus Position & Größe bei bedarf. Der Test auf Kollision ist dann trivial.

https://www.pygame.org/docs/ref/rect.ht ... olliderect
__deets__
User
Beiträge: 14533
Registriert: Mittwoch 14. Oktober 2015, 14:29

Und noch ein Nachtrag: deine detectCollision ist zum einen nicht pythonisch benannt (detect_collision wuerde der Konvention entsprechen. Generell hast du diesbezueglich ein Problem, auch SCREEN, P etc sind "falsch", und verwirren beim lesen sehr).

Vor allem aber ist sie "Unsinn", weil faktisch statische Methode. Stattdessen wuerde man das als normale Methode implementieren, und die Koordinaten (bzw das rect) des Spielers aus self._collision_rect oder so rausziehen.

Code: Alles auswählen

class Player:

    def collision_check(self, other):
          return self.bounding_box.collides(other.bounding_box)
Antworten