【python】43_用pygame制作乌龟吃鱼游戏

1. pygame的介绍

Pygame是跨平台Python模块,专为电子游戏设计,包含图像、声音。允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚。

一个游戏循环(也可以称为主循环)就做下面这三件事:

  1. 处理事件
  2. 更新游戏状态
  3. 绘制游戏状态到屏幕上
    在这里插入图片描述
    Pygame常用模块:
    在这里插入图片描述
    图片素材处理:
    在这里插入图片描述

2. 乌龟吃鱼代码解析

游戏规则:
1). 假设游戏场景为范围(x,y)为0<=x<=10,0<=y<=10
2). 游戏生成1只乌龟和10条鱼,它们的移动方向均随机
3). 乌龟的最大移动能力为2(它可以随机选择1还是2移动),
鱼儿的最大移动能力是1当移动到场景边缘,自动向反方向移动 4). 乌龟初始化体力为100(上限), 乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20, 鱼暂不计算体力 5). 当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束

首先需要定义两个类,乌龟类鱼类
乌龟类:
在这里插入图片描述
鱼类:
在这里插入图片描述
然后再根据这两个类,实例化出来游戏对象:
在这里插入图片描述
在游戏中,我们要用键盘控制乌龟的移动:
在这里插入图片描述
最后是游戏的逻辑处理
在这里插入图片描述

3.撕葱吃热狗游戏

根据上面的代码解析,我们自己写一个撕葱吃热狗游戏。

import random
import pygame

#背景图的 宽、高
width = 1088
height = 610

class SiCong(object):
    """
    SiCong类:
    属性:体力值power、坐标(x、y)
    方法:移动move、吃热狗eat
    """
    def __init__(self):
        self.power = 100 #一开始默认为100
        self.x = random.randint(50,width-50)
        self.y = random.randint(50,height-50)

    def eat(self):
        print('吃到热狗了,分数+1')

    def move(self,new_x,new_y):
        #对图案移动到边缘时坐取余处理
        self.x = new_x % width
        self.y = new_y % height


class HotDog(object):
    """
    HotDog类:
    属性:坐标(x,y)
    方法:move()
    """
    def __init__(self):
        self.x = random.randint(50,width-50)
        self.y = random.randint(50,height-50)

    def move(self):
        move_value = [-10]
        #设置热狗只能水平向左移动
        new_x = self.x + random.choice(move_value)
        # new_y = self.y + random.choice(move_value)
        #对x取余时处理当热狗图案走到了边缘坐标时,出了左边从右边回来
        self.x = new_x%width
        # self.y = new_y%height

def main():
    pygame.init()
    #显示游戏界面
    screen = pygame.display.set_mode((width,height))
    #设置游戏界面的标题
    pygame.display.set_caption('思聪吃热狗')
    #设置游戏背景 bg时是游戏背景的对象
    bg = pygame.image.load('D:\\1911PythonCode\\day10_面向对象三大特性\\img\\bg.jpg').convert()
    #convert()是转换图片格式的
    #实例化两个图案的对象
    sicong_pic = pygame.image.load('D:\\1911PythonCode\\day10_面向对象三大特性\\img\\sicong.png').convert_alpha()
    hotdog_pic = pygame.image.load('D:\\1911PythonCode\\day10_面向对象三大特性\\img\\hot-dog.png').convert_alpha()

    #获取人物及热狗的宽度和高度,返回给人物与热狗计算初始坐标,否则有可能会在屏幕上看不到图案
    # 50,50 = hotdog_pic.get_width(),hotdog_pic.get_height()
    # 50,50 = sicong_pic.get_width(),sicong_pic.get_height()
    # #用global关键字声明局部变量为全部变量,以方便在两个类中调用这些变量
    # global 50,50,50,50

    #加载游戏音乐
    pygame.mixer.music.load('D:\\1911PythonCode\\game_music.mp3')
    #设置游戏音乐的播放
    pygame.mixer.music.play(loops=0,start=(0.0))   #不循环播放,并且从0.0s开始播放

    #分数一开始默认为0
    scoreCount = 0
    #系统设置字体的类型和大小
    font = pygame.font.SysFont('华文隶书',25)   #def SysFont(name, size, bold=0, italic=0, constructor=None)  无法直接设置颜色
    #设置字体的颜色
    #def render(self, text, antialias(是否要平滑显示), color, background=None)
    score = font.render('分数:%s' %(scoreCount),True,(0,0,0))   #(0,0,0)

    #创建一个Clock对象,跟踪游戏运行时间
    fspClock = pygame.time.Clock()

    #创建体力值显示

    #创建sicong和hotdog
    sicong = SiCong()
    hotdogs = [HotDog() for item in range(10)]


    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print('game over')
                exit(0)
            elif event.type == pygame.KEYDOWN:
                #只要有键盘的按下,体力值-1
                sicong.power -= 1
                if event.key == pygame.K_UP:
                    sicong.move(sicong.x,sicong.y-10)
                elif event.key == pygame.K_DOWN:
                    sicong.move(sicong.x,sicong.y+10)
                elif event.key == pygame.K_LEFT:
                    sicong.move(sicong.x-10,sicong.y)
                elif event.key == pygame.K_RIGHT:
                    sicong.move(sicong.x+10,sicong.y)


        # 绘制背景和分数
        screen.blit(bg, (0, 0))  # 从(0,0)开始绘制背景图
        screen.blit(score, (510, 20))
        # 在屏幕上绘制sicong和hotdog
        screen.blit(sicong_pic, (sicong.x, sicong.y))
        for hd in hotdogs:
            screen.blit(hotdog_pic, (hd.x, hd.y))
            #热狗初始默认向左移动
            hd.move()

        # 更新内容到游戏窗口
        pygame.display.update()
        # 每秒更新10帧
        fspClock.tick(10)

        #判断游戏是否结束:当人物体力值为0或者热狗数量为0时,游戏结束
        if sicong.power == 0:
            print('Game Over:sicong power is 0')
            exit(1)
        if len(hotdogs) == 0:
            print('Game Over:hotdog count is 0')
            exit(2)

        #判断人物是否吃到热狗
        for hd in hotdogs:
            #当人物与热狗图像的坐标相差在50以内,认为吃到
            if 0<sicong.x -hd.x<50 and 0<sicong.y -hd.y<50:
                #增加任务的能量值
                sicong.eat()
                #移除被吃掉的热狗
                hotdogs.remove(hd)
                #增加得分
                scoreCount += 10
                #重新设置得分信息
                score = font.render('分数:%s' % (scoreCount), True, (0, 0, 0))  # (0,0,0)


if __name__ == '__main__':
    main()

运行结果如下:
在这里插入图片描述
最后游戏的结束方式有几种:能量值用完或者热狗吃完。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值