使用python制作小鸟游戏

话不多说,先来看看效果:

 分为9个模板:

一:安装pygame模块

在命令提示符中输入pip install pygame就可;

如果想要高速下载可以上网找国内支持下载的链接再用pip install pygame -i (链接);(推荐清华大学的链接)

二:熟悉pygame的常用模块

输入www.pygame.org进入官网

 三:pygame的基本使用

先来搭建一个小球撞屏幕的游戏:

效果如下:

 这一部分比较简单直接上代码:

 

 四:游戏分析

思考方向:

根据物理的参照物的知识,我们可以用管道的移动造成小鸟移动的视觉效果,

小鸟只需要上下移动,而管道就固定的向左以固定的速度移动,移到游戏屏幕左侧边缘消失,再从游戏屏幕右侧即可;

五:搭架主框架

先将背景图移到游戏所在文件夹;

 

 

 

 六:创建小鸟类

 

 

 

 

 

 七:创建管道类

 八:计算得分

九:碰撞检测

 

 

 附上源代码:

import pygame,sys

class Bird: #定义小鸟类

    def __init__(self): #定义初始化方法

        self.birdRect = pygame.Rect(65,50,50,50) #设置小鸟的矩形边框大小

        self.birdStatus = [pygame.image.load('1.png'),

        pygame.image.load('2.png'),

        pygame.image.load('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: #定义管道类

    def __init__(self): #定义初始化方法

        self.wallx = 400

        self.pineUp = pygame.image.load('top.png')

        self.pineDown = pygame.image.load('bottom.png')

    def UpdatePipeline(self): #定义移动方法

        self.wallx -= 5 

        if self.wallx < -80:

            global score

            score +=1

            self.wallx = 400

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,(242,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

    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) #设置背景色

    background = pygame.image.load('background.png') #加载背景

    Bird = Bird() #创造小鸟类的实例

    Pipeline = Pipeline() #创造管道类的实例

    score = 0 #得分

    

    while True:

        clock.tick(60) #每秒执行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

        #screen.fill(color) #填充背景色

        screen.blit(background,(0,0)) #添加新背景 (会将原来背景色覆盖掉)

        screen.blit(Pipeline.pineUp,(Pipeline.wallx,-300))

        screen.blit(Pipeline.pineDown,(Pipeline.wallx,500))

        Pipeline.UpdatePipeline() #调用函数

        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()   #更新背景 (也可以用pygame.display.update()) 

        if checkDead():

            getResult()

    pygame.quit()

 

 

 

 

 

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值