嗨喽~大家好呀,这里是魔王呐 !
这个小恐龙熟悉叭,相信你在摸鱼得时候玩过它~
这是谷歌流量器中很有名的彩蛋:当你网络出现问题时,就会出现一个“小恐龙游戏”。
当然你如果想要直接进行游戏,可以在地址栏输入:chrome://dino
那么今天我们得目的就是:用python来制作一个仿制的“小恐龙游戏”!
Python版本:3.6.4
pygame模块;以及一些python自带的模块。
如果安装python第三方模块:
win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install pygame) 回车
在pycharm中点击Terminal(终端) 输入安装命令
Game7.py
import cfg
import sys
import random
import pygame
from modules.sprites.scene import *
from modules.sprites.obstacle import *
from modules.sprites.dinosaur import *
from modules.interfaces.gameend import GameEndInterface
from modules.interfaces.gamestart import GameStartInterface
需要源码、素材可直接查看文章下方推广加助理小姐姐V免费获取呐~
'''main'''
def main(highest_score):
pygame.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption('T-Rex Rush —— 青灯教育 小恐龙')
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.py
import os
SCREENSIZE = (600, 150)
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)
需要源码、素材可直接查看文章下方推广加助理小姐姐V免费获取呐~
人的一生就像在攀登高峰,勤奋是你踏实稳健的双脚,
信念是你指引前行的向导,勇敢是你孜孜追寻的恒心。
开心日到了,愿你站稳双脚,确定方向,向着你的理想巅峰勇敢前行,
不用怕,未来就在你的脚下。
—— 心灵鸡汤
本文章到这里就结束啦~感兴趣的小伙伴可以复制代码去试试哦
对啦!!记得三连哦~ 另外,欢迎大家阅读我往期的文章呀~