python小游戏——怀念经典坦克大战代码

♥️作者:小刘在这里

♥️每天分享云计算网络运维课堂笔记,努力不一定有回报,但一定会有收获加油!一起努力,共赴美好人生!

♥️夕阳下,是最美的,绽放,愿所有的美好,再疫情结束后如约而至。

目录

一.效果呈现

二,主代码

三.cfg

四.README


一.效果呈现

 

二,主代码

'''
Function:
    经典坦克大战小游戏
Author:
    Charles
微信公众号:
    Charles的皮卡丘
'''
import os
import cfg
import pygame
from modules import *


'''主函数'''
def main(cfg):
    # 游戏初始化
    pygame.init()
    pygame.mixer.init()
    screen = pygame.display.set_mode((cfg.WIDTH, cfg.HEIGHT))
    pygame.display.set_caption(cfg.TITLE)
    # 加载游戏素材
    sounds = {}
    for key, value in cfg.AUDIO_PATHS.items():
        sounds[key] = pygame.mixer.Sound(value)
        sounds[key].set_volume(1)
    # 开始界面
    is_dual_mode = gameStartInterface(screen, cfg)
    # 关卡数
    levelfilepaths = [os.path.join(cfg.LEVELFILEDIR, filename) for filename in sorted(os.listdir(cfg.LEVELFILEDIR))]
    # 主循环
    for idx, levelfilepath in enumerate(levelfilepaths):
        switchLevelIterface(screen, cfg, idx+1)
        game_level = GameLevel(idx+1, levelfilepath, sounds, is_dual_mode, cfg)
        is_win = game_level.start(screen)
        if not is_win: break
    is_quit_game = gameEndIterface(screen, cfg, is_win)
    return is_quit_game


'''run'''
if __name__ == '__main__':
    while True:
        is_quit_game = main(cfg)
        if is_quit_game:
            break

三.cfg

# Introduction
https://mp.weixin.qq.com/s/1xXULpT36P7LTO5HDbjptg

# Environment
```
OS: Windows10
Python: Python3.5+(have installed necessary dependencies)
```

# Usage
```
Step1:
pip install -r requirements.txt
Step2:
run "python Game5.py"
```

# Game Display
![giphy](demonstration/running.gif)

四.README

# Introduction
https://mp.weixin.qq.com/s/1xXULpT36P7LTO5HDbjptg

# Environment
```
OS: Windows10
Python: Python3.5+(have installed necessary dependencies)
```

# Usage
```
Step1:
pip install -r requirements.txt
Step2:
run "python Game5.py"
```

# Game Display
![giphy](demonstration/running.gif)

♥️关注,就是我创作的动力

♥️点赞,就是对我最大的认可

♥️这里是小刘,励志用心做好每一篇文章,谢谢大家

  • 30
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 45
    评论
import cfg import sys import random import pygame from 期末作业.小恐龙跑酷.modules import GameStartInterface, Scoreboard, Dinosaur, Ground, Cloud, Cactus, Ptera, \ GameEndInterface '''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_c​​olor=cfg.BACKGROUND_COLOR) highest_score = highest_score highest_score_board = 记分牌(cfg.IMAGE_PATHS['numbers'], position=(435, 15), bg_c​​olor=cfg.BACKGROUND_COLOR, is_highest=True) dino = Dinosaur(cfg.IMAGE_PATHS['dino']) ground = Ground(cfg.IMAGE_PATHS['ground'], position=(0, cfg.SCREENSIZE[1])) 云精灵组= 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) 如果 random_value >= 5 且 random_value <= 7: cactus_sprites_group.add(Cactus(cfg.IMAGE_PATHS['cacti']))否则: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 如果score_timer > (cfg.FPS // 12): score_timer = 0 score += 1 score = min(score, 99999) 如果score > highest_score: highest_score = score if score % 100 == 0: sounds['point'].play() if score % 1000 == 0: ground.speed -= 1 对于 cloud_sprites_group 中的项目:item.speed -= 1 对于 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''' ifname == ' main ': highest_score = 0 while True: flag, highest_score = main(highest_score) if not flag: break运行注解代码
06-03

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小刘在C站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值