pygame窗口collison bug

时间:2015-01-01 00:51:32

标签: python window pygame

我用箭头键制作了一个可移动的白色坦克。我做了一些碰撞检测,以便“坦克”。不会走出屏幕。碰撞不起作用的唯一地方是“坦克”。它位于角落附近,当相应地向上或向下按压时,它会从窗口掉出来。任何想法如何解决这一问题?碰撞代码位于 def drawtank()。

import pygame, sys
from pygame.locals import *

FPS = 30
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
TANK_SIZE = 20

BLACK = (0  ,0  ,0  )
WHITE = (255,255,255)

def drawArena():
    DISPLAYSURF.fill(BLACK)

def drawTank(tank):
    if tank.bottom > WINDOW_HEIGHT:
        tank.bottom = WINDOW_HEIGHT
    elif tank.top < 0:
        tank.top = 0
    elif tank.right > WINDOW_WIDTH:
        tank.right = WINDOW_WIDTH
    elif tank.left < 0:
        tank.left = 0
    pygame.draw.rect(DISPLAYSURF, WHITE, tank)

def main():
    pygame.init()
    global DISPLAYSURF
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption('Tanks')

    tankX = 200
    tankY = 200
    tankX_change = 0

    tank = pygame.Rect(tankX, tankY, TANK_SIZE, TANK_SIZE)

    drawArena()
    drawTank(tank)

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        keys_pressed = pygame.key.get_pressed()

        if keys_pressed[K_LEFT]:
            tankX -= 20

        if keys_pressed[K_RIGHT]:
            tankX += 20

        if keys_pressed[K_UP]:
            tankY -=20

        if keys_pressed[K_DOWN]:
            tankY += 20


        tank = pygame.Rect(tankX, tankY, TANK_SIZE, TANK_SIZE)

        drawArena()
        drawTank(tank)

        pygame.display.update()
        FPSCLOCK.tick(FPS)

if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:0)

我不熟悉pygame。但是,在调用drawTank之前,是否可以按下多个箭头键?如果是这样的话,事情发生在角落附近的事实告诉我,坦克可能会移动到盒子左边和盒子下面的位置(例如)。然后,您调用的if ... elif只会更正其中一个错误。

如果是这样,只需将elif中的每个drawtank转为if

def drawTank(tank):
    if tank.bottom > WINDOW_HEIGHT:
        tank.bottom = WINDOW_HEIGHT
    if tank.top < 0:
        tank.top = 0
    if tank.right > WINDOW_WIDTH:
        tank.right = WINDOW_WIDTH
    if tank.left < 0:
        tank.left = 0
    pygame.draw.rect(DISPLAYSURF, WHITE, tank)

答案 1 :(得分:0)

对于每种可能性,您需要if/elif'selif tank.right > WINDOW_WIDTH仅评估之前的if和elif是False,因此您在角落中的坦克时会错过:

def drawTank(tank):
    if tank.bottom > WINDOW_HEIGHT:
        tank.bottom = WINDOW_HEIGHT
    elif tank.top < 0:
        tank.top = 0
    if tank.right > WINDOW_WIDTH: # need an if here
        tank.right = WINDOW_WIDTH
    elif tank.left < 0:
        tank.left = 0
    pygame.draw.rect(DISPLAYSURF, WHITE, tank)

如果坦克不能同时位于屏幕的顶部和底部,则不需要额外的一个。

有一种更简单的方法:

screen = pygame.display.set_mode((800, 600)) # add this 
screen_rect = screen.get_rect() # this 

现在你的代码是:

import pygame, sys
from pygame.locals import *

FPS = 30
TANK_SIZE = 20
BLACK = (0  ,0  ,0  )
WHITE = (255,255,255)
screen=pygame.display.set_mode((800, 600))
screen_rect=screen.get_rect()
def drawArena():
    screen.fill(BLACK)

def drawTank(tank):
    tank.clamp_ip(screen_rect)
    pygame.draw.rect(screen, WHITE, tank)

def main():
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    pygame.display.set_caption('Tanks')
    tankX = 200
    tankY = 200
    tankX_change = 0

    tank = pygame.Rect(tankX, tankY, TANK_SIZE, TANK_SIZE)

    drawArena()
    drawTank(tank)
    ......