碰撞在pygame中无法正常工作,没有错误消息

时间:2019-02-01 22:49:08

标签: python pygame collision-detection

我正在pygame中创建游戏,但玩家(矩形)和墙(图像)之间的碰撞无效。

这里的v190131a/__init__.py

import os
import sys
import pygame
import time
import screeninfo

screendata = screeninfo.get_monitors()
screendata = screendata[0]


class Player(object):
    def __init__(self):
        self.rect = pygame.Rect(32, 32, 16, 16)
        # self.offset = (screen.get_size()[0]-32, screen.get_size()[1]-32)
        self.offset = (1, 1)

    def move(self, dx, dy):

        # Move each axis separately. Note that this checks for collisions both times.
        if dx != 0:
            self.move_single_axis(dx, 0)
        if dy != 0:
            self.move_single_axis(0, dy)

    def move_single_axis(self, dx, dy):

        # Move the rect
        self.rect.x += dx
        self.rect.y += dy

        for wall in walls:
            if self.rect.colliderect(wall.rect):
                print(wall.rect)
                if dx > 0:  # Moving right; Hit the left side of the wall
                    self.rect.right = wall.obj.left
                if dx < 0:  # Moving left; Hit the right side of the wall
                    self.rect.left = wall.obj.right
                if dy > 0:  # Moving down; Hit the top side of the wall
                    self.rect.bottom = wall.obj.top
                if dy < 0:  # Moving up; Hit the bottom side of the wall
                    self.rect.top = wall.obj.bottom

        for border in borders:
            if self.rect.colliderect(border):
                if dx > 0:  # Moving right; Hit the left side of the wall
                    self.rect.right = border.left
                if dx < 0:  # Moving left; Hit the right side of the wall
                    self.rect.left = border.right
                if dy > 0:  # Moving down; Hit the top side of the wall
                    self.rect.bottom = border.top
                if dy < 0:  # Moving up; Hit the bottom side of the wall
                    self.rect.top = border.bottom


class Wall(object):
    def __init__(self, pos):
        walls.append(self)
        self.obj = assets["whitebrick"]
        self.rect = self.obj.get_rect()
        self.pos = pos


borders = [pygame.Rect(0, 0, screendata.width, 16),
           pygame.Rect(0, 16, 16, screendata.height),
           pygame.Rect(0, screendata.height-64, screendata.width, 16),
           pygame.Rect(screendata.width-16, 0, 16, screendata.height)
           ]
walls = []
screen = None
clock = None
player = None
assets = None


def assetgrab():
    return {
        "whitebrick": pygame.image.load("v190131a/assets/img/whitebrick.png").convert_alpha()
    }


def init():
    global screen, clock, player, assets
    os.environ["SDL_VIDEO_CENTERED"] = "1"
    pygame.init()

    # Set up the display
    pygame.display.set_caption("Get to the red square!")
    screen = pygame.display.set_mode((0, 0), pygame.RESIZABLE)

    clock = pygame.time.Clock()
    player = Player()  # Create the player
    assets = assetgrab()

    Wall((32, 256))


def event(evnt):
    if evnt.type == pygame.QUIT:
        sys.exit()
    if evnt.type == pygame.KEYDOWN and evnt.key == pygame.K_ESCAPE:
        sys.exit()


def mainloop():
    global borders

    # Move the player if an arrow key is pressed
    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        player.move(-5, 0)
    if key[pygame.K_RIGHT]:
        player.move(5, 0)
    if key[pygame.K_UP]:
        player.move(0, -5)
    if key[pygame.K_DOWN]:
        player.move(0, 5)

    screen.get_size()

    borders = [pygame.Rect(0, 0, screen.get_size()[0], 16),
               pygame.Rect(0, 16, 16, screen.get_size()[1]),
               pygame.Rect(0, screen.get_size()[1]-64, screen.get_size()[0], 16),
               pygame.Rect(screen.get_size()[0]-16, 0, 16, screen.get_size()[1])
               ]

    player.move(0, 5)

    # Draw the scene
    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (255, 200, 0), player.rect)

    for wall in walls:
        screen.blit(wall.obj, wall.pos)

    [pygame.draw.rect(screen, (0, 0, 0), x) for x in borders]
    pygame.display.flip()


def end():
    time.sleep(0.0166666667)
    pass

然后被core.py抢走:

import pygame
import v190131a as core

core.init()

while True:
    for event in pygame.event.get():
        core.event(event)

    core.mainloop()
    core.end()

该玩家应该降落在墙上并停下来,但它会直接通过。有什么我想念的吗?我需要将播放器更改为图像吗,这是一个明显的逻辑错误吗?

非常感谢您的帮助。

0 个答案:

没有答案