基于Python实现天天酷跑功能
作者:Gtieguo 发布时间:2022-09-30 01:18:33
标签:Python,天天酷跑
感觉上次写的植物大战僵尸与俄罗斯方块的反应还不错,这次这个文章就更有动力了
这次就写一个天天酷跑吧
写出来的效果图就是这样了
下面就更新一下全部的代码吧
还是老样子先定义
import pygame,sys
import random
写一下游戏配置
width = 1200 #窗口宽度
height = 508 #窗口高度
size = width, height
score=None #分数
myFont=myFont1=None #字体
surObject=None #障碍物图片
surGameOver=None #游戏结束图片
bg=None #背景对象
role=None #人物对象
object=None #障碍物对象
objectList=[] #障碍物对象数组
clock=None #时钟
gameState=None #游戏状态(0,1)表示(游戏中,游戏结束)
写人物
class Role: #人物
def __init__(self,surface=None,y=None):
self.surface=surface
self.y=y
self.w=(surface.get_width())/12
self.h=surface.get_height()/2
self.currentFrame=-1
self.state=0 #0代表跑步状态,1代表跳跃状态,2代表连续跳跃
self.g=1 #重力加速度
self.vy=0 #y轴速度
self.vy_start=-20 #起跳开始速度
def getRect(self):
return (0,self.y+12,self.w,self.h)
写障碍物
class Object: #障碍物
def __init__(self,surface,x=0,y=0):
self.surface=surface
self.x=x
self.y=y
self.w=surface.get_width()
self.h=surface.get_height()
self.currentFrame=random.randint(0,6)
self.w = 100
self.h = 100
def getRect(self):
return (self.x,self.y,self.w,self.h)
def collision(self,rect1,rect2):
#碰撞检测
if (rect2[0]>=rect1[2]-20) or (rect1[0]+40>=rect2[2])or (rect1[1]+rect1[3]<rect2[1]+20) or (rect2[1]+rect2[3]<rect1[1]+20):
return False
return True
写背景
class Bg: #背景
def __init__(self,surface):
self.surface=surface
self.dx=-10
self.w=surface.get_width()
self.rect=surface.get_rect()
def initGame():
global bg,role,clock,gameState,surObject,surGameOver,score,myFont,myFont1,objectList
#分数初始化
score=0
#初始化
objectList=[]
#加载字体
myFont=pygame.font.Font("./freesansbold.ttf",32)
myFont1=pygame.font.Font("./freesansbold.ttf",64)
# 创建时钟对象 (可以控制游戏循环频率)
clock = pygame.time.Clock()
#初始化游戏状态
gameState=0
#游戏背景
surBg=pygame.image.load("image/bg.bmp").convert_alpha()
bg=Bg(surBg)
#结束画面
surGameOver=pygame.image.load("image/gameover.bmp").convert_alpha()
#人物图片
surRole=pygame.image.load("image/role.png").convert_alpha()
role=Role(surRole,508-85)
#障碍物图片
surObject=pygame.image.load("image/object.png").convert_alpha()
def addObject():
global surObject,object,objectList,object
rate=4
#是否生成障碍物
if not random.randint(0,300)<rate:
return
y=random.choice([height-100,height-200,height-300,height-400])
object=Object(surObject,width+40,y)
objectList.append(object)
def updateLogic():
global gameState,score
#键盘事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
#空格键跳跃
if gameState==0:
if event.key==pygame.K_SPACE:
if role.state==0:
role.state=1
role.vy=role.vy_start
elif role.state==1:
role.state=2
role.vy=role.vy_start
elif gameState==1:
if event.key==pygame.K_SPACE:
#重新开始游戏
initGame()
if gameState==0:
#背景的移动
bg.dx+=10
if bg.dx==1200:
bg.dx=0
#人物的移动
if role.state==0:
role.currentFrame+=1
if role.currentFrame==12:
role.currentFrame=0
else:
role.y+=role.vy
role.vy+=role.g
if role.y>=508-85:
role.y=508-85
role.state=0
#障碍物的移动
addObject()
for object in objectList:
object.x-=10 #障碍物移动
# 障碍物超出屏幕,移除障碍物
if object.x+object.w<=0:
objectList.remove(object)
score+=10 #避开障碍物,加10分
print("移除了一个目标")
#碰撞检测
if object.collision(role.getRect(),object.getRect()):
if(object.currentFrame==6):
objectList.remove(object)
score+=100 #吃金币加100分
print(score)
print("吃了一个金币")
else:
gameState=1 #游戏失败
print("发生了碰撞!")
ok啦,这就是这个天天酷跑的全部代码啦,有问题可以留言,我看到都会回的。
来源:https://blog.csdn.net/Gtieguo/article/details/112168246
0
投稿
猜你喜欢
- 临时表与内存表内存表,指的是使用Memory引擎的表,建表语法是create table … engine=memory。这种 表的数据都保
- 这两天在做小程序调取地图的时候遇到一个问题,如果用户第一次拒绝了位置权限请求。那么就不会再次唤起授权弹出。需要我们引导用户去开启。具体做法如
- 星期五写了个分类信息的小东东!在数据库里只有ip地址,一般访客不太清楚IP地址来源于哪个城市.如果在表里多一个列保存城市又没有真实性可言.如
- 数据平台 Kaggle 近日发布了 2017 机器学习及数据科学调查报告,这也是 Kaggle 首次进行全行业调查。调查共收到超过 1600
- 前言本文是在GitHub上看到一个大牛总结的前端常见面试题,很多问题问的都很好,很经典、很有代表性。上面没有答案,我就整理了一下,从网上找了
- 建议用pycharm,使用比较方便,并且可以直接编辑html文件import timelocatime = time.strftime(&q
- 本文实例讲述了Python实现螺旋矩阵的填充算法。分享给大家供大家参考,具体如下:afanty的分析:关于矩阵(二维数组)填充问题自己动手推
- 继上一篇单向链表,单线链表可以进一步扩展为环,如下图所示:特点:1、第一个节点称为头部节点,最后一个节点称为尾部节点2、每个节点都单方面的指
- 具体代码如下所示:#coding=utf8from urllib import requestimport reimport urllib,
- 业务场景:前后端分离需要对接数据接口。接口测试是在postman做的,今天才开始和前端对接,由于这是我第一次做后端接口开发(第一次嘛,问题比
- 本文实例讲述了Python文件及目录操作的方法。分享给大家供大家参考。具体分析如下:在python中对文件及目录的操作一般涉及多os模块,o
- 在使用python做爬虫的过程中,有些页面的的部分数据是通过js异步加载的,js调用接口的请求中有时还带有些加密的参数很难破解无法使用req
- MySQL不允许SELECT FROM后面指向用作UPDATE的表,有时候让人纠结。当然,有比创建无休止的临时表更好的办法。本文解释如何UP
- sqlserver2008不支持关键字limit ,所以它的分页sql查询语句将不能用MySQL的方式进行,幸好sqlserver2008提
- 语法JavaScript的语法和Java语言类似,每个语句以;结束,语句块用{...}。但是,JavaScript并不强制要求在每个语句的结
- 1.列表(List)元组是由一对方括号构成的序列。列表创建后,可以根据自己的需要改变他的内容>>> list=[1,2,3
- 前言opencv中封装了一个专门用于求解cv::Mat均值的函数,即cv::mean(&cv::Mat),该函数会得到Mat中各个通
- 编程的生活愈发不容易了,工作越来越难找,说多了都是泪还是给大家贡献些代码比较实际。python3 链接数据库需要下载名为pymysql的第三
- Python Assert 为何不尽如人意?Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛
- 需求描述最近在写一个图像标注小工具,其中需要用到一个缩略图列表,来查看文件夹内的图片文件。这里整理一个基于QListWidget实现的版本,