属实是小有成就感,用Python自制一个天天酷跑小游戏,整体效果还是挺不错的!

童年的回忆,用Python写了一个天天酷跑小游戏!


这次就写一个天天酷跑吧!

天天酷跑.jpg

写出来的效果图就是这样了
下面就更新一下全部的代码吧
还是老样子先定义

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

最后,如果你对Python感兴趣,想要学习Python,希望可以帮到你,一起加油!以上是给大家分享的Python全套学习资料,都是我自己学习时整理的:

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

图片

图片

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,还有环境配置的教程,给大家节省了很多时间。

图片

三、全套PDF电子书

书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

图片

四、入门学习视频全套

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

图片

图片

五、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

图片

图片

 **学习资源已打包,需要的小伙伴可以戳这里:【学习资料】

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
天天酷跑》是一款非常受欢迎的跑酷游戏,它在移动设备上广受欢迎。如果你想使用Python来制作一个类似的小游戏,可以使用Pygame库来实现。 Pygame一个专门用于游戏开发Python库,它提供了丰富的功能和工具,可以帮助你创建2D游戏。下面是一个简单的示例,展示了如何使用Pygame创建一个基本的跑酷游戏: ```python import pygame import random # 初始化Pygame pygame.init() # 设置游戏窗口大小 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("天天酷跑") # 加载玩家角色图片 player_image = pygame.image.load("player.png") player_rect = player_image.get_rect() player_rect.centerx = screen_width // 2 player_rect.bottom = screen_height - 10 # 加载障碍物图片 obstacle_image = pygame.image.load("obstacle.png") obstacle_rect = obstacle_image.get_rect() obstacle_rect.centerx = random.randint(0, screen_width) obstacle_rect.bottom = 0 # 设置游戏时钟 clock = pygame.time.Clock() # 游戏循环 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 更新玩家位置 player_rect.x += 5 # 更新障碍物位置 obstacle_rect.y += 3 if obstacle_rect.top > screen_height: obstacle_rect.centerx = random.randint(0, screen_width) obstacle_rect.bottom = 0 # 绘制游戏场景 screen.fill((255, 255, 255)) screen.blit(player_image, player_rect) screen.blit(obstacle_image, obstacle_rect) # 刷新屏幕 pygame.display.flip() # 控制游戏帧率 clock.tick(60) # 退出游戏 pygame.quit() ``` 这只是一个简单的示例,你可以根据自己的需求进行扩展和修改。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值