作为总结,我在寒假期间有一个 Pygame 的学校项目。我制作了游戏的基础(Flappy Bird),但问题是碰撞无法正常工作。问题在于它将通过管道算作碰撞。
你们能检查我的代码并修复它吗?
import pygame
import sys
import math
import random
from pygame.locals import *
a=320
b=240
da=1
db=0
x=25
y=25
e=50
da2=0
c=200
d=10
c_change=0
d_change=0
clock = pygame.time.Clock()
bg=(36,38,82)
wood=(253,197,136)
green=(79, 255, 101)
pipe=(152,228,86)
end=(137, 226, 57)
bg1=(40,42,86)
gold=(219,178,58)
golden=(254, 197, 34)
golder=(255, 206, 63)
black=(0,0,0)
red=(255, 47, 47)
white=(255,255,255)
pygame.init()
screen = pygame.display.set_mode((1400,700))
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(GREY)
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
crashFlag=0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_SPACE:
c_change=-1
d_change=1
if event.type==pygame.KEYUP:
if event.key==pygame.K_SPACE:
c_change=1
if event.type==pygame.KEYDOWN:
crashFlag==0
c+=c_change
d+=d_change
screen.fill(bg)
pygame.draw.rect(screen,bg1, (0,350,1400,350), 0)
pygame.draw.circle(screen,white, (d,c),5)
#for f in range(195, 390 ,5):
if d>200 and d<275:
if c>340:
crashFlag=1
pygame.draw.rect(screen,end, (195,300,80,40), 0)
pygame.draw.rect(screen,pipe, (200,0,70,300), 0)
pygame.draw.rect(screen,pipe, (350,0,70,450), 0)
pygame.draw.rect(screen,end, (345,420,80,40), 0)
pygame.draw.rect(screen,pipe, (490,0,70,480), 0)
pygame.draw.rect(screen,end, (485,480,80,40), 0)
pygame.draw.rect(screen,pipe, (630,0,70,450), 0)
pygame.draw.rect(screen,end, (625,450,80,40), 0)
pygame.draw.rect(screen,pipe, (770,0,70,430), 0)
pygame.draw.rect(screen,end, (765,420,80,40), 0)
pygame.draw.rect(screen,pipe, (910,0,70,400), 0)
pygame.draw.rect(screen,end, (905,400,80,40), 0)
pygame.draw.rect(screen,pipe, (1050,0,70,470), 0)
pygame.draw.rect(screen,end, (1045,470,80,40), 0)
pygame.draw.rect(screen,pipe, (1190,0,70,430), 0)
pygame.draw.rect(screen,end, (1185,430,80,40), 0)
pygame.draw.rect(screen,gold, (1330,0,70,410), 0)
pygame.draw.rect(screen,golder, (1350,0,70,410), 0)
pygame.draw.rect(screen,golden, (1325,410,80,40), 0)
pygame.draw.rect(screen,pipe, (200,400,70,240), 0)
#lower pipes
pygame.draw.rect(screen,end, (195,400,80,40), 0)
pygame.draw.rect(screen,pipe, (350,520,70,500), 0)
pygame.draw.rect(screen,end, (345,515,80,40), 0)
pygame.draw.rect(screen,pipe, (490,570,70,100), 0)
pygame.draw.rect(screen,end, (485,570,80,40), 0)
pygame.draw.rect(screen,pipe, (630,570,70,100), 0)
pygame.draw.rect(screen,end, (625,540,80,40), 0)
pygame.draw.rect(screen,pipe, (770,550,70,120), 0)
pygame.draw.rect(screen,end, (765,510,80,40), 0)
pygame.draw.rect(screen,pipe, (910,530,70,220), 0)
pygame.draw.rect(screen,end, (905,490,80,40), 0)
pygame.draw.rect(screen,pipe, (1050,560,70,220), 0)
pygame.draw.rect(screen,end, (1045,560,80,40), 0)
pygame.draw.rect(screen,pipe, (1190,530,70,220), 0)
pygame.draw.rect(screen,end, (1185,530,80,40), 0)
pygame.draw.rect(screen,gold, (1330,530,70,220), 0)
pygame.draw.rect(screen,golder, (1350,530,70,220), 0)
pygame.draw.rect(screen,golden, (1325,510,80,40), 0)
pygame.draw.rect(screen, wood, (0,650,1400,50), 0)
pygame.draw.rect(screen,green, (0,640,1400,10), 0)
if crashFlag==1:
pygame.draw.rect(screen,white, (0,0,1400,700), 0)
font = pygame.font.SysFont("Berlin Sans FB Demi", 100, True, False)
text = font.render("You Lost", True, black)
screen.blit(text, (500, 100))
pygame.draw.rect(screen,green, (600,400,190,60), 0)
font = pygame.font.SysFont("Aharoni", 50, True, False)
text = font.render("RESET", True, white)
screen.blit(text, (630, 410))
pygame.display.update()
clock.tick(150)
pygame.quit()