栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

童年记忆第三弹!!!用python写一个天天酷跑

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

童年记忆第三弹!!!用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]

写背景

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)=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("发生了碰撞!")

#总结
以上就是植物大战僵尸的源码过程,大家可以写一下玩一玩
我是白白,一个喜欢学习喜欢编程的年轻人
想学习python的可以关注私信我哦~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/219545.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号