【发布时间】:2017-07-23 12:22:45
【问题描述】:
我正在制作一个飞扬的小鸟克隆游戏,我正试图让小鸟与管道发生碰撞。我没有收到任何错误消息,只是当鸟儿撞到上管时没有显示 print("collision")。问题出在班级MainWindow!
import pygame
pygame.init()
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("FlapPY Bird")
clock = pygame.time.Clock()
# colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# --> variables
FPS = 60
# classes
class MainWindow(object):
def __init__(self, w, h):
self.width = w
self.height = h
self.Main()
def Main(self):
loop = True
bird_width = 25
bird_height = 25
self.bird_x = 150
self.bird_y = HEIGHT/2 - int(bird_height/2)
bird_x_move = 0
bird_y_move = 0
pipe_spacing = 350
pipe_speed = 1
space = 100
p1_x = 300
p1_y = 400
p1_w = 50
p1_h = HEIGHT
p2_x = p1_x + pipe_spacing
p2_y = 250
p2_w = 50
p2_h = HEIGHT
p3_x = p2_x + pipe_spacing
p3_y = 250
p3_w = 50
p3_h = HEIGHT
pipe1 = Pipes(p1_x, p1_y, p1_w, p1_h, space, pipe_speed, red)
pipe2 = Pipes(p2_x, p2_y, p2_w, p2_h, space, pipe_speed, green)
pipe3 = Pipes(p3_x, p3_y, p3_w, p3_h, space, pipe_speed, blue)
while loop:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird_y_move = -7
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
bird_y_move = 3
screen.fill(white)
self.bird_x += bird_x_move
self.bird_y += bird_y_move
bird = FlappyBird(self.bird_x, self.bird_y, bird_width, bird_height)
bird.draw()
pipe1.draw_pipes()
pipe2.draw_pipes()
pipe3.draw_pipes()
pipe1.check_if()
pipe2.check_if()
pipe3.check_if()
p1_new_x = pipe1.pipe_move()
p2_new_x = pipe2.pipe_move()
p3_new_x = pipe3.pipe_move()
if self.bird_y <= p1_y - space and self.bird_y >= p1_y and self.bird_x+bird_width >= p1_new_x and self.bird_x <= p1_new_x+p1_w:
print("collision")
pygame.display.update()
clock.tick(FPS)
class FlappyBird(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self):
pygame.draw.rect(screen, red, (self.x, self.y, self.width, self.height))
class Pipes(object):
def __init__(self, x, y, width, height, space, speed, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.space = space
self.speed = speed
self.color = color
def draw_pipes(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
pygame.draw.rect(screen, self.color, (self.x, self.y-self.space-self.height, self.width, self.height))
def pipe_move(self):
self.x -= self.speed
return self.x
def check_if(self):
if self.x < 0:
self.x = 1000
MainWindow(WIDTH, HEIGHT)
【问题讨论】:
-
您的问题是什么?问题是什么?您收到哪些错误消息?您请求宽大处理,但没有提供我们解决问题所需的基本信息...
-
我删除了那个长序言 - 使问题更难阅读。但是...正如@darthbith 指出的那样:没有实际问题,只是代码转储。
-
我很抱歉这么简短的问题代码 if self.bird_y = p1_y and self.bird_x+bird_width >= p1_new_x and self.bird_x
-
请please用代码编辑您的问题。永远不要将代码放入 cmets - 完全不可读。
-
2 提示: 1. 从一张(方格)纸开始,画出碰撞和不碰撞的形状,并附上坐标。 2. 有时它们不碰撞时更容易锻炼。
标签: python pygame collision-detection collision