Python 我已经开始在Pygame中编写一个Flappy Bird游戏,我';我被卡住了怎么显示鸟和竹子之间的碰撞?

Python 我已经开始在Pygame中编写一个Flappy Bird游戏,我';我被卡住了怎么显示鸟和竹子之间的碰撞?,python,pygame,collision,Python,Pygame,Collision,所以我对Pygame还很陌生,我一直在研究如何在flappy bird游戏中展示鸟和竹子之间的碰撞,以及如果它没有与竹子发生碰撞,如何继续运行它。 我还没有写完整的代码,所以请告诉我要写什么来显示冲突(有步骤,为什么要用它?在pygame中),还有一个源代码,在冲突后屏幕显示游戏结束。(可选) (提前感谢:) 导入pygame pygame.init() 窗宽=820 窗高=480 win=pygame.display.set_模式((窗口宽度、窗口高度)) pygame.display.set

所以我对Pygame还很陌生,我一直在研究如何在flappy bird游戏中展示鸟和竹子之间的碰撞,以及如果它没有与竹子发生碰撞,如何继续运行它。 我还没有写完整的代码,所以请告诉我要写什么来显示冲突(有步骤,为什么要用它?在pygame中),还有一个源代码,在冲突后屏幕显示游戏结束。(可选) (提前感谢:)

导入pygame
pygame.init()
窗宽=820
窗高=480
win=pygame.display.set_模式((窗口宽度、窗口高度))
pygame.display.set_标题(“Flappy Bird游戏”)
birdload=[pygame.image.load('C:/Users/Vinod/Desktop/Flappy Bird/flapbird.png')]
bgimg=pygame.image.load('C:/Users/Vinod/Desktop/Flappy Bird/bg.jpg'))
clock=pygame.time.clock()
向上=‘向上’
向下
左
右=‘右’
白色=(255,255,255)
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
暗绿色=(0155,0)
暗光线=(40,40,40)
黄色=(255,255,0)
橙色=(255,165,0)
BGCOLOR=黑色
布朗=(90,39,41)
分数=0
def showstartscreen():
全球基本方
FPS=15
BASICFONT=pygame.font.font('freesansbold.ttf',18)
titleFont=pygame.font.font('freesansbold.ttf',100)
titleSurf1=titleFont.render('Flappy Bird!',True,白色,黄色)
titleSurf2=titleFont.render('Flappy Bird!',True,橙色)
度1=0
度数2=0
尽管如此:
win.fill(BGCOLOR)
rotatedSurf1=pygame.transform.rotate(titleSurf1,degrees1)
rotatedRect1=rotatedSurf1.get_rect()
rotatedRect1.center=(窗宽/2,窗高/2)
win.blit(rotatedSurf1,rotatedRect1)
rotatedSurf2=pygame.transform.rotate(titleSurf2,degrees2)
rotatedRect2=rotatedSurf2.get_rect()
rotatedRect2.center=(窗宽/2,窗高/2)
win.blit(rotatedSurf2,rotatedRect2)
drawPressKeyMsg()
pygame.display.update()
时钟滴答声(FPS)
度1+=3#每帧旋转3度
度2+=7#每帧旋转7度
def drawPressKeyMsg():
pressKeySurf=BASICFONT.render('按一个键播放'),True,DARKGRAY)
pressKeyRect=pressKeySurf.get_rect()
按KeyRect.topleft=(WINDOWWIDTH-200,WINDOWHEIGHT-30)
win.blit(按KeySurf,按KeyRect)
def terminate():
pygame.quit()
类鸟(对象):
定义初始(自、x、y、宽度、高度、终点):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.end=结束
self.path=[self.x,self.end]
self.flyCount=0
self.vel=3
self.hitbox=(self.x+17,self.y+2,31,57)
self.visible=True
def抽签(自我,赢):
如果self.flyCount+1>=27:
self.flyCount=0
如果不是(自立):
win.blit(birdload[self.flyCount//3],(self.x,self.y))
self.flyCount+=1
其他:
win.blit(birdload[0],(self.x,self.y))
self.hitbox=(self.x+17,self.y+11,29,52)
#pygame.draw.rect(赢,(255,0,0),self.hitbox,2)
def命中(自身):
self.isJump=False
self.jumpCount=10
self.x=100
self.y=410
self.flyCount=0
font1=pygame.font.SysFont('comicsans',100)
text=font1.render('-5',1,(255,0,0))
blit(text,(250-(text.get_width()/2),200))
pygame.display.update()
i=0
当我<200时:
pygame。时间。延迟(10)
i+=1
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
i=201
pygame.quit()
def飞行(自我):
如果self.vel>0:
如果self.x+self.velself.path[0]:
self.x+=self.vel
其他:
self.vel=self.vel*-1
self.flyCount=0
def bambol():
bamb0=pygame.draw.rect(赢,布朗,(310,0,28,140))#左,上,宽,高
pygame.display.flip()
bamb1=pygame.draw.rect(赢,布朗,(31023028250))
pygame.display.flip()
bamb2=pygame.draw.rect(赢,布朗,(410,0,28100))#左,上,宽,高
pygame.display.flip()
bamb3=pygame.draw.rect(赢,布朗,(41016028320))#左,上,宽,高
pygame.display.flip()
bamb4=pygame.draw.rect(赢,布朗,(510,028110))#左,上,宽,高
pygame.display.flip()
bamb5=pygame.draw.rect(赢,布朗,(51018028300))#左,上,宽,高
pygame.display.flip()
bamb6=pygame.draw.rect(赢,布朗,(600,028240))#左,上,宽,高
pygame.display.flip()
bamb7=pygame.draw.rect(赢,布朗,(60032028160))#左,上,宽,高
pygame.display.flip()
bamb8=pygame.draw.rect(赢,布朗,(700,028120))#左,上,宽,高
pygame.display.flip()
bamb9=pygame.draw.rect(赢,布朗,(70020028280))#左,上,宽,高
pygame.display.flip()
bamb10=pygame.draw.rect(赢,布朗,(770,028190))#左,上,宽,高
pygame.display.flip()
bamb11=pygame.draw.rect(赢,布朗,(7702502830))#左,上,宽,高
pygame.display.flip()
类(对象):
竹
def collision():
如果bird.x==sticks.x和bird.y==sticks.y:
打印(“游戏结束”)
gameover()
尽管如此:
如果选中ForkeyPress():
pygame.event.get()#清除事件队列
import pygame
pygame.init()

WINDOWWIDTH = 820
WINDOWHEIGHT = 480
win = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption("The Flappy Bird Game")

birdload = [pygame.image.load('C:/Users/Vinod/Desktop/Flappy Bird/flapbird.png')]

bgimg = pygame.image.load('C:/Users/Vinod/Desktop/Flappy Bird/bg.jpg')

clock=pygame.time.Clock()

UP = 'up'
DOWN ='down'
LEFT ='left'
RIGHT = 'right'

WHITE =(255, 255, 255)
BLACK = (0, 0 ,0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
DARKGREEN = (0, 155,0)
DARKGRAY = (40, 40, 40)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
BGCOLOR = BLACK
BROWN = (90, 39, 41)

score = 0



def showstartscreen():
    global BASICFONT, FPS
    FPS = 15
    BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
    titleFont = pygame.font.Font('freesansbold.ttf', 100)
    titleSurf1 = titleFont.render('The Flappy Bird!', True, WHITE, YELLOW)
    titleSurf2 = titleFont.render('The Flappy Bird!', True, ORANGE)
    degrees1 = 0
    degrees2 = 0
    while True:
        win.fill(BGCOLOR)
        rotatedSurf1= pygame.transform.rotate(titleSurf1, degrees1)
        rotatedRect1 = rotatedSurf1.get_rect()
        rotatedRect1.center= (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
        win.blit(rotatedSurf1, rotatedRect1)
        
        rotatedSurf2 = pygame.transform.rotate(titleSurf2, degrees2)
        rotatedRect2 = rotatedSurf2.get_rect()
        rotatedRect2.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
        win.blit(rotatedSurf2,rotatedRect2)
        
        drawPressKeyMsg()
        
        pygame.display.update()
        clock.tick(FPS)
        degrees1 += 3 #rotate 3 degrees each frame
        degrees2 += 7 #rotate by 7 degrees per frame
        
def drawPressKeyMsg():
    pressKeySurf = BASICFONT.render('Press a key to play.', True, DARKGRAY)
    pressKeyRect = pressKeySurf.get_rect()
    pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
    win.blit(pressKeySurf, pressKeyRect)
    

def terminate():
    pygame.quit()
    
class bird(object):
    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x, self.end]
        self.flyCount = 0
        self.vel = 3
        self.hitbox = (self.x + 17, self.y + 2, 31, 57)
        self.visible = True
        
    def draw(self, win):
        if self.flyCount + 1 >= 27:
            self.flyCount = 0

        if not(self.standing):
            win.blit(birdload[self.flyCount//3], (self.x,self.y))
            self.flyCount +=1
        else:
            win.blit(birdload[0], (self.x, self.y))
        self.hitbox = (self.x + 17, self.y + 11, 29, 52)
        #pygame.draw.rect(win, (255,0,0), self.hitbox,2)
        
    def hit(self):
        self.isJump = False
        self.jumpCount = 10
        self.x = 100
        self.y = 410
        self.flyCount = 0
        font1 = pygame.font.SysFont('comicsans', 100)
        text = font1.render('-5', 1, (255,0,0))
        win.blit(text, (250 - (text.get_width()/2),200))
        pygame.display.update()
        i = 0
        while i < 200:
            pygame.time.delay(10)
            i += 1
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    i = 201
                    pygame.quit()
                    
    def fly(self):
        if self.vel > 0:
            if self.x + self.vel < self.path[1]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.flyCount = 0
        else:
            if self.x - self.vel > self.path[0]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.flyCount = 0
                    
    
def bamboo():
    bamb0 = pygame.draw.rect(win, BROWN,(310, 0, 28, 140))#left, top, width, height
    pygame.display.flip()
    bamb1 = pygame.draw.rect(win, BROWN, (310, 230 , 28, 250))
    pygame.display.flip()
    bamb2 = pygame.draw.rect(win, BROWN,(410, 0, 28, 100))#left, top, width, height
    pygame.display.flip()
    bamb3 = pygame.draw.rect(win, BROWN,(410, 160, 28,320))#left, top, width, height
    pygame.display.flip()
    bamb4 = pygame.draw.rect(win, BROWN,(510, 0, 28,110))#left, top, width, height
    pygame.display.flip()
    bamb5 = pygame.draw.rect(win, BROWN,(510, 180, 28,300))#left, top, width, height
    pygame.display.flip()
    bamb6 = pygame.draw.rect(win, BROWN,(600, 0, 28,240))#left, top, width, height
    pygame.display.flip()
    bamb7 = pygame.draw.rect(win, BROWN,(600, 320, 28,160))#left, top, width, height
    pygame.display.flip()
    bamb8 = pygame.draw.rect(win, BROWN,(700, 0, 28,120))#left, top, width, height
    pygame.display.flip()
    bamb9 = pygame.draw.rect(win, BROWN,(700, 200, 28,280))#left, top, width, height
    pygame.display.flip()
    bamb10 = pygame.draw.rect(win, BROWN,(770, 0, 28,190))#left, top, width, height
    pygame.display.flip()
    bamb11 = pygame.draw.rect(win, BROWN,(770, 250, 28,230))#left, top, width, height
    pygame.display.flip()
    
    
class sticks(object):
    bamboo()
    
    
    def collision():
        if bird.x == sticks.x and bird.y == sticks.y:
            print("Game Over")
            gameover()
            while True:
                if checkForKeyPress():
                    pygame.event.get() # clear event queue
                    return
        else:
            True()
class Bamboo:

    def __init__( self, x, y, width, height ):
        self.image  = pygame.Surface( ( width, height ) )
        self.rect   = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        # currently the image is a brown rectangle
        self.image.fill( BROWN )
        
    def draw( self, surface ):
        surface.blit( self.image, self.rect )
        
    def move( self, dx, dy ):
        self.rect.move_ip( dx, dy )
        
    def collidesWith( self, other_rect ):
        result = self.rect.colliderect( other_rect )
        return result
### Create the list of Bamboo            
all_bamboo = []     # list to hold the final objects

# List of initial locations and sizes
initial_locs = [ (310, 0, 28, 140),  (310, 230 , 28, 250), (410, 0, 28, 100),
                 (410, 160, 28,320), (510, 0, 28,110),     (510, 180, 28,300),
                 (600, 0, 28,240),   (600, 320, 28,160),   (700, 0, 28,120),
                 (700, 200, 28,280), (770, 0, 28,190),     (770, 250, 28,230) ]
                 
# loop through the locations, making Bamboo, appending to list                 
for location in initial_locs:
    x, y, width, height = location                  # unpack location
    new_bamboo = Bamboo( x, y, width, height )      # create a Bamboo
    all_bamboo.append( new_bamboo )                 # move to list
### Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
            pass   # TODO - handle mouse-clicks
        elif ( event.type == pygame.KEYUP ):
            pass   # TODO - handle player-input

    ### Update the window
    window.fill( WHITE )         # fill the background

    # draw the sprites
    player_bird.draw( window )   # draw player

    for bamboo in all_bamboo:    # draw all bamboo
        bamboo.draw( screen )

    pygame.display.flip()        # send the updates to the screen

    ### Check for collisions & move bamboo
    bird_rect = pygame.Rect( bird.hitbox )        # location of bird
    for bamboo in all_bamboo:                     # for each stick of bamboo
        if ( bamboo.collidesWith( bird_rect ) ):  # does it collide?
            # Bird hit bamboo, now what?
            bird.hit()                            # ouch!
        
        # move the bamboo 1 pixel left
        # so it looks like the bird moves forward
        bamboo.move( -1, 0 )

    clock.tick(60)

pygame.quit()