自学一周python做的一个小游戏《大球吃小球》

需求

1,显示一个窗口。
2,我们要做到的功能有鼠标点击屏幕生成小球。
3,生成的小球大小随机,颜色随机,向随机方向移动,速度也随机。
4,大的球碰到小球时可以吃掉小球,吃掉后会变大。
5,球碰到边界会弹回去。

思路

思路很简单
1,这个游戏我们使用python的pygame,先生成一个带有背景颜色固定大小的窗口
2,建一个颜色类,用来生成随机颜色
3,建一个球类用于生成随机的各样小球
4,建主方法,调用颜色和球生成小游戏
5,打包

第一步 生成窗口

我们需要导入pygame模块,如果你用的是PyCharm的话点击下面这个代码,PyCharm会自动下载pygame模块

import pygame

如果你没用PyCharm的话就直接使用命令导入

pip install pygame

导入成功后我们建一个窗口对象

screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('大球吃小球')
screen.fill((224, 224, 224))
pygame.display.flip()
running = True
# 开启一个事件循环处理发生的事件
while running:
        # 从消息队列中获取事件并对事件进行处理
    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             running = False

800和600是窗口的长宽,大球吃小球是窗口的标题,(224,224,224)代表窗口颜色是RGB格式的颜色表达式下面是用来监听事件的后面会用到,现在开始运行

if __name__=="__main__":
    main()

在这里插入图片描述
很好,现在第一步完成了

第二步 建一个颜色类

@unique
class Color(Enum):
    red = (255, 0, 0)
    @staticmethod
    def random_color():
        """获得随机颜色"""
        r = randint(0, 255)
        g = randint(0, 255)
        b = randint(0, 255)
        return r, g, b

颜色类,比较简单,生成三个随机值就是一个随机的颜色了,这也就是刚才上面说的RGB值

第三步 球类

球类复杂一些,需要传入位置坐标,半径,移动距离和颜色
还要有吃,移动,和生成三个方法。代码如下

class Ball():

    def __init__(self, x, y, reduis, sx, sy, color=Color.red):
        self._sy = sy
        self._x = x
        self._y = y
        self._reduis = reduis
        self._sx = sx
        self._color = color
        self._alive = True

    def move(self, screen):
        self._x += self._sx
        self._y += self._sy
        if self._x - self._reduis <= 0 or self._x + self._reduis >= screen.get_width():
            self._sx = -self._sx
        if self._y - self._reduis <= 0 or self._y + self._reduis >= screen.get_height():
            self._sy = -self._sy

    def eat(self, other):
        if self._alive and other._alive and other != self:
            dx = self._x - other._x
            dy = self._y - other._y
            distance = sqrt(dx ** 2 + dy ** 2)
            print(distance)
            if distance < int(self._reduis) + int(other._reduis) and int(self._reduis) > int(other._reduis):
                other._alive = False
                self._reduis = self._reduis + int(other._reduis * 0.146)

    def draw(self, screen):
        pygame.draw.circle(screen, self._color, (self._x, self._y), self._reduis, 0)
        

移动和吃这两个方法的逻辑不难,我就不在这说了,不懂的可以在面评论或私信。关于属性在init里有个alive存活代表这小球是否存活的属性,需要大家注意
我就说下生成draw这个方法吧
circle这个方法的参数分别是screen窗口对象,颜色,球的坐标,球的半径,和是否填充

第四步 在主方法里调用并编写点击生成和反弹方法

代码如下:

    balls = [] #
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption('大球吃小球')
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:#
                x, y = event.pos#
                radius = randint(10, 100)#
                if x - radius < 0:#
                    x = radius#
                if y - radius < 0:#
                    y = radius#
                sx, sy = randint(-10, 10), randint(-10, 10)#
                color = Color.random_color()#
                ball = Ball(x, y, radius, sx, sy, color)#
                # 将球添加到列表容器中
                balls.append(ball)#
        screen.fill((224, 224, 224))#
        for ball in balls:#
            if ball._alive:#
                ball.draw(screen)#
            else:#
                balls.remove(ball)#
        pygame.display.flip()
        pygame.time.delay(50)#
        for ball in balls:#
            ball.move(screen)#
            # 检查球有没有吃到其他的球
            for other in balls:#
                ball.eat(other)#

我在更改代码的部分后面加了#,表示区分
首先我们声明一个balls用来做装球的容器,然后在事件监听部分加上对鼠标点击事件的监听ifx-radius是为了防止在界面边缘点击时生成的球超出边界,然后将生成的球的对象放入容器balls里遍历容器,判断是否存活,若存活则生成,若已死则移除容器。
将窗口设置为50毫秒刷新一次,最后再次遍历判断球有没有吃其他球,现在运行。
在这里插入图片描述大家可以看到效果已经出来了,现在还差最后一步。打包

打包

打包工具我用的是Pyinstaller需要先安装一下

pip install Pyinstaller

然后打开pycharm底部的terminal面板输入

pyinstaller -F  xyx.py

回车就行了
如果没有pycharm的话就在命令窗口进入到项目目录下,再输入这个命令回车就行了,找到dist的exe双击就运行了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值