小恐龙 py小游戏

简介: 6小恐龙

6小恐龙

玩法 上下控制起跳躲避

源代码如下:

import cfg
import sys
import random
import pygame
from modules import *

'''main'''
def main(highest_score):

# 游戏初始化
pygame.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption('九歌')
# 导入所有声音文件
sounds = {}
for key, value in cfg.AUDIO_PATHS.items():
    sounds[key] = pygame.mixer.Sound(value)
# 游戏开始界面
GameStartInterface(screen, sounds, cfg)
# 定义一些游戏中必要的元素和变量
score = 0
score_board = Scoreboard(cfg.IMAGE_PATHS['numbers'], position=(534, 15), bg_color=cfg.BACKGROUND_COLOR)
highest_score = highest_score
highest_score_board = Scoreboard(cfg.IMAGE_PATHS['numbers'], position=(435, 15), bg_color=cfg.BACKGROUND_COLOR, is_highest=True)
dino = Dinosaur(cfg.IMAGE_PATHS['dino'])
ground = Ground(cfg.IMAGE_PATHS['ground'], position=(0, cfg.SCREENSIZE[1]))
cloud_sprites_group = pygame.sprite.Group()
cactus_sprites_group = pygame.sprite.Group()
ptera_sprites_group = pygame.sprite.Group()
add_obstacle_timer = 0
score_timer = 0
# 游戏主循环
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
                dino.jump(sounds)
            elif event.key == pygame.K_DOWN:
                dino.duck()
        elif event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
            dino.unduck()
    screen.fill(cfg.BACKGROUND_COLOR)
    # --随机添加云
    if len(cloud_sprites_group) < 5 and random.randrange(0, 300) == 10:
        cloud_sprites_group.add(Cloud(cfg.IMAGE_PATHS['cloud'], position=(cfg.SCREENSIZE[0], random.randrange(30, 75))))
    # --随机添加仙人掌/飞龙
    add_obstacle_timer += 1
    if add_obstacle_timer > random.randrange(50, 150):
        add_obstacle_timer = 0
        random_value = random.randrange(0, 10)
        if random_value >= 5 and random_value <= 7:
            cactus_sprites_group.add(Cactus(cfg.IMAGE_PATHS['cacti']))
        else:
            position_ys = [cfg.SCREENSIZE[1]*0.82, cfg.SCREENSIZE[1]*0.75, cfg.SCREENSIZE[1]*0.60, cfg.SCREENSIZE[1]*0.20]
            ptera_sprites_group.add(Ptera(cfg.IMAGE_PATHS['ptera'], position=(600, random.choice(position_ys))))
    # --更新游戏元素
    dino.update()
    ground.update()
    cloud_sprites_group.update()
    cactus_sprites_group.update()
    ptera_sprites_group.update()
    score_timer += 1
    if score_timer > (cfg.FPS//12):
        score_timer = 0
        score += 1
        score = min(score, 99999)
        if score > highest_score:
            highest_score = score
        if score % 100 == 0:
            sounds['point'].play()
        if score % 1000 == 0:
            ground.speed -= 1
            for item in cloud_sprites_group:
                item.speed -= 1
            for item in cactus_sprites_group:
                item.speed -= 1
            for item in ptera_sprites_group:
                item.speed -= 1
    # --碰撞检测
    for item in cactus_sprites_group:
        if pygame.sprite.collide_mask(dino, item):
            dino.die(sounds)
    for item in ptera_sprites_group:
        if pygame.sprite.collide_mask(dino, item):
            dino.die(sounds)
    # --将游戏元素画到屏幕上
    dino.draw(screen)
    ground.draw(screen)
    cloud_sprites_group.draw(screen)
    cactus_sprites_group.draw(screen)
    ptera_sprites_group.draw(screen)
    score_board.set(score)
    highest_score_board.set(highest_score)
    score_board.draw(screen)
    highest_score_board.draw(screen)
    # --更新屏幕
    pygame.display.update()
    clock.tick(cfg.FPS)
    # --游戏是否结束
    if dino.is_dead:
        break
# 游戏结束界面
return GameEndInterface(screen, cfg), highest_score

'''run'''
if name == '__main__':

highest_score = 0
while True:
    flag, highest_score = main(highest_score)
    if not flag: break

le/details/124330889

目录
相关文章
|
Python
Python版飞机大战游戏的设计(五)-----英雄登场
Python版飞机大战游戏的设计(五)-----英雄登场
124 0
Python写个“点球大战”小游戏
玩家的选择通过input输入获取,电脑的选择我们可以用random随机数模块来产生
|
Python
python小游戏——贪吃蛇游戏2.0版本の得分功能实现
python小游戏——贪吃蛇游戏2.0版本の得分功能实现
242 0
|
Python
python小游戏——跑酷小恐龙代码开源
python小游戏——跑酷小恐龙代码开源
880 0
python小游戏——跑酷小恐龙代码开源
|
Python
python小游戏——怀念经典坦克大战代码
python小游戏——怀念经典坦克大战代码
544 0
python小游戏——怀念经典坦克大战代码
python小玩意——猜拳游戏
python小玩意——猜拳游戏
python小玩意——猜拳游戏
|
存储 Python
python小游戏————坦克大战
python小游戏————坦克大战
python小游戏————坦克大战
|
Python
用Python制作七夕表白神器,让你成功概率提高99.9%
用Python制作七夕表白神器,让你成功概率提高99.9%
236 0
|
人工智能 C# 图形学
为了练习自己的Python基础语法,我用pygame写了一个打砖块闯关的游戏
这里的BOSS战是我自己想的,打不动的砖块实在是没有什么意思,于是这个版本我更新了两场BOSS战,我们终于可以打BOSS(其实目前只是会动的大砖块)了。我打算实现道具,并为第一个BOSS加上技能,同时为游戏添加BGM,最后为了给我其他没学过Python的同学测评,我将其编译成了。那是一个百无聊赖的夜晚,我回顾了自己自大学以来的经历,感觉生活十分平淡,学习了许多计算机知识,但是似乎什么东西都没有用上。游戏诞生的喜悦,使我改变了最开始只是简单复现的计划,我打算写进去更多的东西,更完整地复现功能,最好还能有点。
273 0
为了练习自己的Python基础语法,我用pygame写了一个打砖块闯关的游戏
|
程序员 Linux API
Python 程序员过中秋Python+pygame 制作拼图小游戏(附源码:5源码)
了解Python 程序员过中秋Python+pygame 制作拼图小游戏(附源码:5源码)。
282 0