Python 皮加梅扑翼鸟碰撞
我正在制作一个flappy bird克隆游戏,我正试图用烟斗撞击这只鸟。我没有收到任何错误消息,只是当鸟撞到上面的管道时,printcollision没有显示。问题在类主窗口中 你有缩进吗/ 编辑:这是固定的Python 皮加梅扑翼鸟碰撞,python,pygame,collision-detection,collision,Python,Pygame,Collision Detection,Collision,我正在制作一个flappy bird克隆游戏,我正试图用烟斗撞击这只鸟。我没有收到任何错误消息,只是当鸟撞到上面的管道时,printcollision没有显示。问题在类主窗口中 你有缩进吗/ 编辑:这是固定的 import pygame pygame.init() WIDTH = 800 HEIGHT = 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("FlapPY Bird
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.0
self.bird_x = 150
self.bird_y = ((HEIGHT / 2) - int(bird_height / 2.0))
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")
break # If you don't break the loop, it will keep printing "collision" to the terminal
exit(0) # Just in case.
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)
不太确定这是否是你想要的,但它是有效的。
我所做的是把self.bird\u y>=p1\u y改为self.bird\u y你的问题是什么?有什么问题?您收到了哪些错误消息?你要求宽大处理,但没有提供我们解决问题所需的基本信息……我删除了冗长的序言——这使问题更难理解。但是正如@darthbith指出的:没有实际的问题,只是一个代码转储。我很抱歉问这么短的问题,如果self.bird_y=p1_y和self.bird_x+bird_width>=p1_new_x和self.bird_x请用代码编辑你的问题。永远不要把代码放在注释中-完全不可读。2提示:1。从一张图表纸开始,画出碰撞和不碰撞的形状,用坐标表示。2.我是一个愚蠢的程序员:我现在只是在哭,不要对自己太苛刻了。编程是困难的,需要花费大量的时间和精力才能做好。我们都会犯错误,但重要的是要把你的错误看作是一个学习新东西的机会,一个改进自己和工作的机会,而不是失败。如果您在编程或调试过程中完全陷入困境,可以尝试做一些完全不同的事情,或者睡一晚,然后再回到程序中,问题通常会更清楚。
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.0
self.bird_x = 150
self.bird_y = ((HEIGHT / 2) - int(bird_height / 2.0))
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")
break # If you don't break the loop, it will keep printing "collision" to the terminal
exit(0) # Just in case.
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)