wan玩得好手游 » 手机游戏 » 基于Pygame的酷跑小游戏

基于Pygame的酷跑小游戏

来源:互联网 编辑:wan玩得好手游小编 更新:2024-04-05 13:59:16 人气:

一。布景介绍

    2013年9月16日腾讯天美工作室推出了一款基于安卓平台的酷跑小游戏,那就是曾经风靡一时的《天天酷跑》,这款游戏在前几年一度获得过年度最佳游戏,笔者在学校期间也曾经为之疯狂。今天就给大家介绍一下如何用pygame做个简单的酷跑小游戏。下图是游戏截屏。

二。游戏介绍

    游戏过程非常简单,西装男在一条路上狂奔,美女挥舞着拳头想把男士打下来,而玩家要做的就是在合适的时机按下空格键,西装男会完成一次跳跃,帮忙西装男躲避女士的攻击,随着游戏的进行,男士奔驰的速度越来越快,直到被美女击中挂掉为之。

三。游戏思路

游戏代码部分由Dinosaur.py、main.py、obstacle.py及scene.py这四个文件组成,分别代表男士、游戏主文件、女士和场景。整个游戏主要基于Pygame库进行开发,各模块均用函数进行封装,以增强复用性,主函数下所示:

def main():

# 初始化

pygame.init()

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("急速狂奔")

clock = pygame.time.Clock()

# 得分

score = 0

# 加载一些素材

jump_sound = pygame.mixer.Sound("./music/jump.wav")

jump_sound.set_volume(6)

die_sound = pygame.mixer.Sound("./music/die.wav")

die_sound.set_volume(6)

pygame.mixer.init()

# pygame.mixer.music.load("./music/bg_music.mp3")

pygame.mixer.music.load("./music/Two Steps From Hell - Victory.mp3")

pygame.mixer.music.set_volume(1)

pygame.mixer.music.play(-1)

font = pygame.font.Font('./font/simkai.ttf', 20)

# 实例化

dinosaur = Dinosaur(WIDTH, HEIGHT)

scene = Scene(WIDTH, HEIGHT)

plants = pygame.sprite.Group()

pteras = pygame.sprite.Group()

# 产生障碍物事件

GenPlantEvent = pygame.constants.USEREVENT + 0

pygame.time.set_timer(GenPlantEvent, 1500)

GenPteraEvent = pygame.constants.USEREVENT + 1

pygame.time.set_timer(GenPteraEvent, 5000)

# 游戏是否结束了

running = True

# 是否可以产生障碍物flag

flag_plant = False

flag_ptera = False

t0 = time.time()

# 主循环

num=0


while running:

for event in pygame.event.get():

if event.type == QUIT:

sys.exit()

pygame.quit()

if event.type == GenPlantEvent:

flag_plant = True

if event.type == GenPteraEvent:

if score > 50:

flag_ptera = True

key_pressed = pygame.key.get_pressed()

if key_pressed[pygame.K_SPACE]:

dinosaur.is_jumping = True

jump_sound.play()



screen.fill(BACKGROUND)

time_passed = time.time() - t0

t0 = time.time()

# 场景

scene.move()

scene.draw(screen)

# 小恐龙

dinosaur.is_running = True

if dinosaur.is_jumping:

dinosaur.be_afraid()

dinosaur.jump(time_passed)

dinosaur.draw(screen)

# 障碍物-植物

if random.random()

plant = Plant(WIDTH, HEIGHT)

plants.add(plant)

flag_plant = False

for plant in plants:

plant.move()

if dinosaur.rect.left > plant.rect.right and not plant.added_score:

score += 1

plant.added_score = True

if plant.rect.right

plants.remove(plant)

continue

plant.draw(screen)

# 障碍物-飞龙

if random.random()

if len(pteras) > 1:

continue

ptera = Ptera(WIDTH, HEIGHT)

pteras.add(ptera)

flag_ptera = False

for ptera in pteras:

ptera.move()

if dinosaur.rect.left > ptera.rect.right and not ptera.added_score:

score += 5

ptera.added_score = True

if ptera.rect.right

pteras.remove(ptera)

continue

ptera.draw(screen)

# 碰撞检测

if pygame.sprite.spritecollide(dinosaur, plants, False) or pygame.sprite.spritecollide(dinosaur, pteras, False):

die_sound.play()

running = False

# 显示得分

score_text = font.render("Score: "+str(score), 1, (0, 0, 0))

screen.blit(score_text, [10, 10])

pygame.display.flip()

clock.tick(60)

res = show_gameover(screen)

return res

除了代码文件外,游戏文件还包含音乐、字体、图片等文件,这只是一个简单的酷跑小游戏,更多玩法,还希望大家能多多开发~~~

最后,更多干货欢迎大家关注“320科技工作室”的微信公众号~~


欢迎玩家到【 wan玩得好手游】查看最新变态版手游攻略,只需要在百度输入【 wan玩得好手游】就可以浏览最新上线送满vip的变态手游攻略了,更多有关手游的攻略和资讯,敬请关注玩得好手游网!

标签: 基于Pygame的酷跑小游戏
玩得好手游为玩家们提供非常多最新好玩的手游下载,热门的手机app下载,常用安卓应用软件下载,热门手游排行榜发现更多好游戏,还为玩家推荐近期热门手游攻略,手游公益服。 简繁切换 繁體中文