python小游戏——跑酷小恐龙代码开源

简介: python小游戏——跑酷小恐龙代码开源

一.效果呈现

二.主代码

'''
Function:
Author:
    Charles
'''
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

三.cfg


'''配置文件'''

import os



'''屏幕大小'''

SCREENSIZE = (600, 150)

'''FPS'''

FPS = 60

'''音频素材路径'''

AUDIO_PATHS = {

   'die': os.path.join(os.getcwd(), 'resources/audios/die.wav'),

   'jump': os.path.join(os.getcwd(), 'resources/audios/jump.wav'),

   'point': os.path.join(os.getcwd(), 'resources/audios/point.wav')

}

'''图片素材路径'''

IMAGE_PATHS = {

   'cacti': [

       os.path.join(os.getcwd(), 'resources/images/cacti-big.png'),

       os.path.join(os.getcwd(), 'resources/images/cacti-small.png')

   ],

   'cloud': os.path.join(os.getcwd(), 'resources/images/cloud.png'),

   'dino': [

       os.path.join(os.getcwd(), 'resources/images/dino.png'),

       os.path.join(os.getcwd(), 'resources/images/dino_ducking.png')

   ],

   'gameover': os.path.join(os.getcwd(), 'resources/images/gameover.png'),

   'ground': os.path.join(os.getcwd(), 'resources/images/ground.png'),

   'numbers': os.path.join(os.getcwd(), 'resources/images/numbers.png'),

   'ptera': os.path.join(os.getcwd(), 'resources/images/ptera.png'),

   'replay': os.path.join(os.getcwd(), 'resources/images/replay.png')

}

'''背景颜色'''

BACKGROUND_COLOR = (235, 235, 235)

BLACK = (0, 0, 0)

WHITE = (255, 255, 255)



四.README

# Introduction

https://mp.weixin.qq.com/s/PnvcSBe0Va3GVIodGIjYRg


# Environment

```

OS: Windows10

Python: Python3.5+(have installed necessary dependencies)

```


# Usage

```

Step1:

pip install -r requirements.txt

Step2:

run "python Game7.py"

```


# Game Display

![giphy](demonstration/running.gif)




相关文章
|
1天前
|
机器学习/深度学习 人工智能 算法
机械视觉:原理、应用及Python代码示例
机械视觉:原理、应用及Python代码示例
|
1天前
|
存储 缓存 算法
优化Python代码性能的7个技巧
在日常的Python开发中,优化代码性能是一个重要的课题。本文介绍了7个实用的技巧,帮助开发者提高Python代码的执行效率,包括利用生成器表达式、使用适量的缓存、避免不必要的循环等。通过本文的指导,读者可以更好地理解Python代码性能优化的方法,提升自身的编程水平。
|
3天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
21 0
|
3天前
|
人工智能 Python
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
8 0
|
3天前
|
Linux 网络安全 开发工具
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
10 0
|
1月前
|
文字识别 Python
python代码运行报错:No module named 'aliyunsdkcore'
用python调用阿里云图片OCR识别,使用的是阿里云官方给的传本地图片文件进行检测的代码,运行报错:No module named 'aliyunsdkcore'。在pycharm python软件包和终端里安装aliyunsdkcore这个模块都失败了。
|
5月前
|
Python
python 股票数据分析、绘制K线图、价格走势图、收益率计算 完整代码+数据 可直接运行
python 股票数据分析、绘制K线图、价格走势图、收益率计算 完整代码+数据 可直接运行
94 0
python 股票数据分析、绘制K线图、价格走势图、收益率计算 完整代码+数据 可直接运行
|
5月前
|
数据可视化 数据挖掘 Python
python pandas 宝可梦数据分析可视化实战 课程设计 完整代码+数据 可直接运行
python pandas 宝可梦数据分析可视化实战 课程设计 完整代码+数据 可直接运行
55 0
|
5月前
|
计算机视觉 Python
使⽤Python实现天⽂影像图像处理和分析 完整代码数据 可直接运行
使⽤Python实现天⽂影像图像处理和分析 完整代码数据 可直接运行
28 0
|
7月前
|
存储 Java 测试技术
为什么 Python 代码在函数中运行得更快?
为什么 Python 代码在函数中运行得更快?