当前位置:   article > 正文

python pygame实现的flappy bird小游戏(简陋版)_python pygame flappy bird简易版代码

python pygame flappy bird简易版代码
import pygame
import sys
import random

class Bird(object):
    """定义一个小鸟类"""
    def __init__(self):
        self.birdRect = pygame.Rect(65,50,50,50)
        """定义初始化方法"""
        self.birdStatus = [pygame.image.load("C:/Users/张小生/PycharmProjects/untitled2/flappyBird/assets/1.png"),
                           pygame.image.load("C:/Users/张小生/PycharmProjects/untitled2/flappyBird/assets/2.png"),
                           pygame.image.load("C:/Users/张小生/PycharmProjects/untitled2/flappyBird/assets/dead.png")]
        self.status = 0
        self.birdX = 120
        self.birdY = 350
        self.jump = False
        self.jumpSpeed = 10
        self.gravity = 5
        self.dead = False
    def birdUpdate(self):#定义移动方法
        if self.jump: #小鸟跳跃状态
            self.jumpSpeed -=1
            self.birdY -= self.jumpSpeed
        else:
            self.gravity += 0.2
            self.birdY += self.gravity
        self.birdRect[1] = self.birdY

class Pipeline(object):#定义管道类
    def __init__(self):#初始化方法
        self.wallx = 400
        self.upy = -300
        self.downy = 500
        self.pineUp = pygame.image.load("C:/Users/张小生/PycharmProjects/untitled2/flappyBird/assets/top.png")
        self.pineDown = pygame.image.load("C:/Users/张小生/PycharmProjects/untitled2/flappyBird/assets/bottom.png")

    def updatePipline(self): #定义移动方法
        self.wallx -= 5
        if self.wallx < -80:
            global score #得分
            score += 1
            self.upy = -random.randint(150,350)
            self.downy = random.randint(400, 550)
            self.wallx = 400


def createMap():#创建地图

    screen.blit(background, (0,0))
    #显示管道
    screen.blit(Pipeline.pineUp,(Pipeline.wallx,Pipeline.upy))
    screen.blit(Pipeline.pineDown, (Pipeline.wallx, Pipeline.downy))
    Pipeline.updatePipline()
    #显示小鸟,先判断小鸟的状态
    if Bird.dead:
        Bird.status = 2
    elif Bird.jump:
        Bird.status = 1
    screen.blit(Bird.birdStatus[Bird.status],(Bird.birdX,Bird.birdY))
    Bird.birdUpdate()
    #在屏幕是显示得分状况
    screen.blit(font.render('Score:' + str(score),-1,(255,255,255)),(100,50))
    pygame.display.flip()

#判断小鸟是否死亡的函数
def checkDead():
    upRect = pygame.Rect(Pipeline.wallx,-300,Pipeline.pineUp.get_width(),Pipeline.pineUp.get_height())
    downRect = pygame.Rect(Pipeline.wallx, 500, Pipeline.pineDown.get_width(), Pipeline.pineDown.get_height())
    #检测矩形碰撞
    if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
        Bird.dead = True
    #边界检测
    if not 0 < Bird.birdRect[1] < height:
        Bird.dead = True
        return True
    else:
        return False


#玩家最终得分
def getResult():
    final_text1 = "Game Over"
    final_text2 = "Your final score is :" + str(score)
    ft1_font = pygame.font.SysFont("Arial",70)
    ft1_surf = font.render(final_text1,1,(244,3,36))
    ft2_font = pygame.font.SysFont("Arial", 50)
    ft2_surf = font.render(final_text2, 1, (253, 177, 6))

    screen.blit(ft1_surf , [screen.get_width() / 2  - ft1_surf.get_width()/2,100])
    screen.blit(ft2_surf , [screen.get_width() / 2  - ft2_surf.get_width()/2,200])
    pygame.display.update()

if __name__ == '__main__': #主程序
    pygame.init()#初始化
    #初始化字体
    pygame.font.init()
    font = pygame.font.SysFont(None,50)


    size = width,height = 400,650
    screen = pygame.display.set_mode(size) #设置窗口
    clock = pygame.time.Clock() # 设置时钟
    # color = (255,255,255)
    # screen.fill(color)
    Bird = Bird()
    Pipeline = Pipeline()
    score = 0

    while True:
        clock.tick(45) #每秒执行60次

        #轮询事件检测
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
                Bird.jump = True
                Bird.gravity = 5
                Bird.jumpSpeed = 10
        background = pygame.image.load("C:/Users/张小生/PycharmProjects/untitled2/flappyBird/assets/background.png")
        #检测小鸟是否死亡
        if checkDead():
            getResult()
        else:
            createMap()  # 创建地图
        # screen.blit(background,(0,0))
        # pygame.display.flip ()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

把图片地址换一下就OK了网盘,提取码:2sb8

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/141311
推荐阅读
相关标签
  

闽ICP备14008679号