金狮镖局 Design By www.egabc.com

感觉上次写的植物大战僵尸与俄罗斯方块的反应还不错,这次这个文章就更有动力了
这次就写一个天天酷跑吧

基于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啦,这就是这个天天酷跑的全部代码啦,有问题可以留言,我看到都会回的。

标签:
Python写天天酷跑,Python天天酷跑

金狮镖局 Design By www.egabc.com
金狮镖局 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com

评论“基于Python实现天天酷跑功能”

暂无基于Python实现天天酷跑功能的评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。