我正在尝试用python编写自顶向下的射击游戏。它会运行,正确显示地图,并且播放器的移动恰到好处。我遇到的问题是碰撞检测。它可以检测到碰撞,但是之后会冻结。有任何想法吗?该代码将运行,并且可以正常工作,直到碰到墙(矩形)为止。然后就冻结!!!我不确定这是怎么回事,但我希望这不是太大的问题!这是我的代码:
import pygame
pygame.init()
screenWidth = 1118
screenHeight = 573
win = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("First Game")
class Rectangle(object):
"""__init__() functions as the class constructor"""
def __init__(self, color=(None, None, None), width=None, height=None, x=None, y=None):
self.color = color
self.width = width
self.height = height
self.x = x
self.y = y
class Circle(object):
"""__init__() functions as the class constructor"""
def __init__(self, color=(None, None, None), radius=None, x=None, y=None):
self.color = color
self.radius = radius
self.x = x
self.y = y
def detectCollisions(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
charwidth = 40
x = 10
charheight = 40
y = 10
vel = 1
armlength = 25
armwidth = 10
run = True
while run:
rectangleList = []
circleList = []
rectangleList.append(Rectangle((0,255,0), 122, 35, 69, 0))
rectangleList.append(Rectangle((0,255,0), 156, 35, 69, 174))
rectangleList.append(Rectangle((0,255,0), 139, 35, 69, 382))
rectangleList.append(Rectangle((0,255,0), 78, 52, 225, 443))
rectangleList.append(Rectangle((0,255,0), 78, 52, 381, 417))
rectangleList.append(Rectangle((0,255,0), 130, 52, 511, 443))
rectangleList.append(Rectangle((0,255,0), 104, 468, 251, 261))
rectangleList.append(Rectangle((0,255,0), 372, 104, 849, 96))
circleList.append(Circle((0,255,0), 39, 277, 170))
circleList.append(Circle((0,255,0), 39, 420, 120))
circleList.append(Circle((0,255,0), 39, 563, 170))
rectangleList.append(Rectangle((0,255,0), 10, 500, 230, 52))
armX = (charwidth/2)+x
armY = (charheight/2)+y
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
collleft = False
for rectangle in rectangleList:
if detectCollisions(x,y,charheight,charwidth,rectangle.x,rectangle.y,rectangle.height,rectangle.width):
collleft = True
if collleft == False:
if x - vel > 0:
x -= vel
print("Move LEFT")
collleft = False
if keys[pygame.K_RIGHT]:
collright = False
for rectangle in rectangleList:
if detectCollisions(x,y,charheight,charwidth,rectangle.x,rectangle.y,rectangle.height,rectangle.width):
collright = True
if collright == False:
if x+vel < screenWidth-charwidth:
x += vel
print("Move RIGHT")
if keys[pygame.K_UP]:
collup = False
for rectangle in rectangleList:
if detectCollisions(x,y,charheight,charwidth,rectangle.x,rectangle.y,rectangle.height,rectangle.width):
collup = True
if collup == False:
if y-vel > 0:
y -= vel
print("Move UP")
if keys[pygame.K_DOWN]:
colldown = False
for rectangle in rectangleList:
if detectCollisions(x,y,charheight,charwidth,rectangle.x,rectangle.y,rectangle.height,rectangle.width):
colldown = True
if colldown == False:
if y+vel < screenHeight-charheight:
y += vel
print("Move DOWN")
win.fill((0, 0, 0))
for rectangle in rectangleList:
pygame.draw.rect(win, rectangle.color, (rectangle.x, rectangle.y, rectangle.height, rectangle.width))
for circle in circleList:
pygame.draw.circle(win, circle.color, [circle.x, circle.y], circle.radius)
pygame.draw.rect(win, (255, 0, 0), (x, y, charwidth, charheight))
pygame.draw.rect(win, (0, 0, 255), (armX, armY, armlength, armwidth))
pygame.display.update()
pygame.quit()