这是我第一次写文章,和大家分享我的python程序。请大家多多点赞,觉得我写的好的话还可以关注一下我。后期我会继续多发一些文章的哦!

今天我要来介绍介绍我自己做的游戏——球球大作战!大家来看看吧!

---------------------------------------------------开始写代码了!---------------------------------------------------------

做代码前的小贴士:

首先要用cmd安装好pygame模块哦!

pip install pygame

一、初始化

首先要导入很重要的pygame,random和math模块(我用了as),初始化程序,做好界面,写上标题

# import pygame, random and math
import pygame as pg
import random as rd
import math# init program
pg.init()
# set screen
screen = pg.display.set_mode((1000, 500))
screen.fill((255, 255, 255))
# set title
pg.display.set_caption("球球大作战", "4.0")

初始化好了程序才能进行下一步。

二、函数、方法和类的定义

程序少不了函数与类。我们得先定义好函数circle,这样方便画圆圈。

# def circle
def circle(color, point, r, size):pg.draw.circle(screen, color, point, r, size)

接下来就要做球的类了。为了可以被吃,所以要用类(懂了吗?)

# class ball
class Ball():def __init__(self):self.color = (rd.randint(0, 255), rd.randint(0, 255), rd.randint(0, 255))self.x =rd.randint(0, 1000)self.y = rd.randint(0, 500)self.r = rd.randint(5, 15)self.size = rd.randint(5, 15)

然后设置好玩家操控的球和画球的列表。

# make a balllist
balllist = []
for i in range(600):balllist.append(Ball())# creat myball
myball = Ball()
myball.color = (0, 0, 0)
myball.x = 500
myball.y = 250
myball.size = 5
myball.speed = 10

这一段的最后要做好判断按下,吃球和生成。

这里要运用上math模块,还用了勾股定理!

每过十秒生成30个球,这样就“永远”也吃不完球了

为了更真实一点,我还加了一些其它代码。越吃得多,速度就越慢。这里推荐*=0.(),想要多少可以自己调。为什么要*=呢?因为/或//要么除不进或者除到零了就麻烦了。

# def check touch
# use the pythagorean theorem
def touch(myX, myY, fX, fY, myR, fR):distance = math.sqrt((myX - fX) ** 2 + (myY - fY) ** 2)if distance <= myR + fR:# just return Truereturn Trueelse:# return Falsereturn False# def foodDelivery
def foodDelivery():time = pg.time.get_ticks()# every 10 seconds put 30 foodsif time % 10000 >= 9000 and time % 10000 <= 9020:for i in range(30):balllist.append(Ball())# def draw
# use "Ball" and for range to append in the balllist
def draw():for ball in balllist:if touch(myball.x, myball.y, ball.x, ball.y, myball.size, ball.size):balllist.remove(ball)myball.size += 0.1# make the speed to be smaller than the last one# use the multiplier scale decreases and inverse proportional functionmyball.speed *= 0.992else:circle(ball.color, (ball.x, ball.y), ball.size, 0)circle(myball.color, (myball.x, myball.y), myball.size, 0)

这样这一段就做好了!

三、主程序运行

接下来必须得认真了,马上就要运行主程序了。

小贴士:fps(帧率)一定要控制好,否则电脑显卡和cpu可能会卡,还要写好防卡程序,可以避免程序忽然报错或者暂停退出。

# check fps, do not quit program
fps = pg.time.Clock()
# check quit and play program
while True:# do not make the fps so high# if the fps is high, the computer will ~"bomb!"fps.tick(60)event = pg.event.poll()if event.type == pg.QUIT:pg.quit()exit()

接下来要做球的移动了,要用pygame.key.get_pressed()来判断哦。

keys = pg.key.get_pressed()
# make the ball to move
# I use the "wasd"
# also can use up down right left
if keys[pg.K_w]:myball.y -= myball.speed

“wasd”上下左右都可以,注意x,y坐标轴的加减。

# check quit and play program
while True:# do not make the fps so high# if the fps is high, the computer will ~"bomb!"fps.tick(60)event = pg.event.poll()if event.type == pg.QUIT:pg.quit()exit()keys = pg.key.get_pressed()# make the ball to move# I use the "wasd"# also can use up down right leftif keys[pg.K_w]:myball.y -= myball.speedif keys[pg.K_a]:myball.x -= myball.speedif keys[pg.K_s]:myball.y += myball.speedif keys[pg.K_d]:myball.x += myball.speedif keys[pg.K_UP]:myball.y -= myball.speedif keys[pg.K_DOWN]:myball.y += myball.speedif keys[pg.K_LEFT]:myball.x -= myball.speedif keys[pg.K_RIGHT]:myball.x += myball.speed# the e is to update ball's xyelif keys[pg.K_e]:myball.x, myball.y = 500, 250

最后写上更新和画图的方法,整个程序就写好了!(别忘了加一次fill,这样易于更新)

while True:# do not make the fps so high# if the fps is high, the computer will ~"bomb!"fps.tick(60)event = pg.event.poll()if event.type == pg.QUIT:pg.quit()exit()keys = pg.key.get_pressed()# make the ball to move# I use the "wasd"# also can use up down right leftif keys[pg.K_w]:myball.y -= myball.speedif keys[pg.K_a]:myball.x -= myball.speedif keys[pg.K_s]:myball.y += myball.speedif keys[pg.K_d]:myball.x += myball.speedif keys[pg.K_UP]:myball.y -= myball.speedif keys[pg.K_DOWN]:myball.y += myball.speedif keys[pg.K_LEFT]:myball.x -= myball.speedif keys[pg.K_RIGHT]:myball.x += myball.speed# the e is to update ball's xyelif keys[pg.K_e]:myball.x, myball.y = 500, 250# draw and checkdraw()foodDelivery()# display programpg.display.update()screen.fill((255, 255, 255))

四、完整代码

最后奉上全部代码:

# import pygame, random and math
import pygame as pg
import random as rd
import math# init program
pg.init()
# set screen
screen = pg.display.set_mode((1000, 500))
screen.fill((255, 255, 255))
# set title
pg.display.set_caption("BallFight_Avaritia", "4.0")
# Chinese:pg.display.set_caption("球球大作战_无尽贪婪", "4.0")# def circle
def circle(color, point, r, size):pg.draw.circle(screen, color, point, r, size)# class ball
class Ball():def __init__(self):self.color = (rd.randint(0, 255), rd.randint(0, 255), rd.randint(0, 255))self.x =rd.randint(0, 1000)self.y = rd.randint(0, 500)self.r = rd.randint(5, 15)self.size = rd.randint(5, 15)# make a balllist
balllist = []
for i in range(600):balllist.append(Ball())# creat myball
myball = Ball()
myball.color = (0, 0, 0)
myball.x = 500
myball.y = 250
myball.size = 5
myball.speed = 10# def check touch
# use the pythagorean theorem
def touch(myX, myY, fX, fY, myR, fR):distance = math.sqrt((myX - fX) ** 2 + (myY - fY) ** 2)if distance <= myR + fR:# just return Truereturn Trueelse:# return Falsereturn False# def foodDelivery
def foodDelivery():time = pg.time.get_ticks()# every 10 seconds put 30 foodsif time % 10000 >= 9000 and time % 10000 <= 9020:for i in range(30):balllist.append(Ball())# def draw
# use "Ball" and for range to append in the balllist
def draw():for ball in balllist:if touch(myball.x, myball.y, ball.x, ball.y, myball.size, ball.size):balllist.remove(ball)myball.size += 0.1# make the speed to be smaller than the last one# use the multiplier scale decreases and inverse proportional functionmyball.speed *= 0.992else:circle(ball.color, (ball.x, ball.y), ball.size, 0)circle(myball.color, (myball.x, myball.y), myball.size, 0)# check fps, do not quit program
fps = pg.time.Clock()
# check quit and play program
while True:# do not make the fps so high# if the fps is high, the computer will ~"bomb!"fps.tick(60)event = pg.event.poll()if event.type == pg.QUIT:pg.quit()exit()keys = pg.key.get_pressed()# make the ball to move# I use the "wasd"# also can use up down right leftif keys[pg.K_w]:myball.y -= myball.speedif keys[pg.K_a]:myball.x -= myball.speedif keys[pg.K_s]:myball.y += myball.speedif keys[pg.K_d]:myball.x += myball.speedif keys[pg.K_UP]:myball.y -= myball.speedif keys[pg.K_DOWN]:myball.y += myball.speedif keys[pg.K_LEFT]:myball.x -= myball.speedif keys[pg.K_RIGHT]:myball.x += myball.speed# the e is to update ball's xyelif keys[pg.K_e]:myball.x, myball.y = 500, 250# draw and checkdraw()foodDelivery()# display programpg.display.update()screen.fill((255, 255, 255))

五、效果图

六、知识总结

通过这次编程,我们了解了如何使用pygame做游戏,也用到了许多模块和复杂的代码。

我们也从中学习到了random, math和pygame模块的运用方法,了解了pygme模块的厉害。

我们操控的球,就好像大白们和白衣天使,他们竭尽全力,与病毒(其他可以吃的球)战斗,让我们更加安全。

希望大家多多学习python,借助在家隔离的这段期间学习编程,分享程序,共克时艰,战胜疫情。

大家可以多多点赞哦~谢谢阅读!下次再见

彩蛋:(下期透露:极光迷宫)

python-pygame小游戏之球球大作战相关推荐

  1. 《动手学ROS2》3.4小游戏_小乌龟求偶大作战

    本系列教程作者:小鱼 公众号:鱼香ROS QQ交流群:139707339 教学视频地址:小鱼的B站 完整文档地址:鱼香ROS官网 版权声明:如非允许禁止转载与商业用途. 3.4 小游戏:小乌龟求偶大作 ...

  2. Cocos Creator 微信创意小游戏《五子大作战》团队专访

    2019 微信公开课 PRO:首批创意小游戏公布 1 月 9 日,以"同行 WITH US"为主题的微信公开课 PRO 在广州召开.公开课上,讲师孙春光发布了微信首批创意小游戏,包 ...

  3. python·pygame小游戏--中国象棋(原码附上,免费下载)

    大家好我是小豪,今天给大家带来的是pygame小游戏-中国象棋 因为看到博客上面很多上传了的中国象棋py文件,都是收费的.所以我大胆的上传个免费的-已经把原码上传了,感兴趣的可以去下载. pygame ...

  4. python pygame小游戏_python:利用pygame实现消消乐小游戏

    消消乐记分小游戏GUI界面 文件结构规划 定义config.py文件存储相关参数:包括界面的宽高,整个方格行列个数,总格数等等. 定义utils.py文件用于存放基础的类和函数:包括整个消除拼图类,游 ...

  5. python pygame小游戏素材图片_pygame 打飞机(小游戏)

    0.游戏的基本实现 ''' 游戏的基本实现 游戏的初始化:设置游戏窗口,绘制图像的初始位置,设定游戏时钟 游戏循环:设置刷新频率,检测用户交互,更新所有图像位置,更新屏幕显示 ''' 1.安装pyga ...

  6. pygame小游戏 海洋之神大冒险

    利用pygame自制小游戏. 海洋之神在漆黑的海底深处,利用自身的光勇敢前进!在海里收集鱼骨头,有些鱼骨头可以转化为武器,用来攻击敌人. 开始: 游戏开始的界面:  快通关啦! 结尾致敬超级马里奥,碰 ...

  7. c++游戏代码坦克大作战_一红一蓝多种模式的双人小游戏:红蓝大作战

    作者有话说:上次推荐的森林冰火人很多小伙伴后台找我要链接,或者搜索不到:首先声明下森林冰火人.同桌大作战都不是辣椒人游戏工作室研发的,小编也是微信小游戏双人栏目下搜索到的,如果想要玩双人小游戏的可以打 ...

  8. cocos creator开发微信小游戏(五)贪吃蛇大作战

    目录 小游戏介绍 小游戏cocos creator场景图 小游戏部分JS代码 开发中碰到的问题 工程及说明 小游戏介绍 贪吃蛇小游戏:贪吃蛇试玩(首次加载比较慢),类似贪吃蛇大作战的小游戏.当玩家的蛇 ...

  9. 新年Java小游戏之「年兽大作战」祝您笑口常开

    这个游戏加上编写文章,上班摸鱼时间加上回家的空闲时间,大概花了三天多. java写这玩应真的很痛苦,各种状态位,各种图片和逻辑判断,脑袋都快炸了.而且肯定没有前端的精致,效果一般,偶尔会有卡顿,各位就 ...

  10. python pygame小游戏_第一个python+pygame小游戏

    没有周队那么有情调,自己写故事做rpg,又没什么绘画功底,只能做这样的休闲棋类游戏.本来是用java写的,但里面绘图太麻烦了(或者说我不会多线程),又想起前几天看到的pygame,于是果断python ...

最新文章

  1. EL表达式介绍(1)
  2. Spring 自动装配 ‘byName’
  3. ifstat命令_统计网络接口活动状态的工具
  4. 在线Excel的前端组件、控件,实现web Excel
  5. 2022年6月25日PMP考试通关宝典-2
  6. usb调试助手_米卓同屏助手 | 刷短视频必备,一键打通“任督二脉”,双端
  7. 串口服务器通讯协议,串口服务器的组成和应用实例
  8. 计算四则表达式(中缀式转后缀式,然后计算结果)
  9. 人人都知“双十一”,几人仍记“光棍节”
  10. 使用R从Excel中删除带有空单元格的行
  11. 四位共阳极数码管显示函数_求各位大神指正,四位一体共阳极数码管数字钟程序,仿真能运行,实物就只显8个8,不动...
  12. 【随缘侃史】蹈舞求生许敬宗
  13. 安卓apk安装包腾讯云-乐固加固以及重新签名
  14. python统计元音字母个数_统计字符串中各元音字母(即A,E,I,O,U)的个数。
  15. 化工厂人员定位系统助力化工企业安全运行
  16. Redis—击穿、穿透、雪崩
  17. DEGUG修改BW表中数据以及修改更改日志
  18. 【Android】自定义View的位置参数
  19. 割完肉先疗伤(转自:沙牛家书)
  20. token值过期的处理

热门文章

  1. python五子棋_python 五子棋
  2. Multimix:从医学图像中进行的少量监督,可解释的多任务学习
  3. 现代 Web CI/CD 系统的搭建
  4. vue修改图片后实时更新
  5. 市场上卖的几十块的32Gu盘是如何达到的。小心JS
  6. android pin码 经典蓝牙_Android 蓝牙 pin 自动配 setPin()方法有时候无效
  7. 如何共享打印机? 按照提示做就可以了~
  8. Qt5如何添加信号和槽
  9. GPU服务器环境配置
  10. java-net-php-python-jspm中国平安保险公司员工积分管理系统计算机毕业设计程序