当前位置:网站首页>[pyGame] can the little dinosaur on chrome be played with code? It looks like fun~
[pyGame] can the little dinosaur on chrome be played with code? It looks like fun~
2022-06-10 23:59:00 【Programmer pear】
Preface
author :“ Programmer pear ”
** The article brief introduction **: This article is mainly about the use of Pygame Module to write a small dinosaur game code La ~
** The source code of the article is available for free : To thank everyone who pays attention to me, the source code of each article is nothing
Compensation sharing drop
Complete material for all articles + The source code is collected at the end of the text
Fans white whoring source code welfare , Please move to CSDN Community
Text

See if there is a familiar little cute acridine ?
About this dinosaur , I think many people know ~
This little bug Maybe it's just a lovely design of the developer , This thing is a colored egg , And you can also play as a game , Just press
Space bar . After pressing the space bar , As expected, something magical happened : The little dinosaur ran happily .

The operation of the game is very simple , Just continue to press the spacebar to jump , Keep avoiding obstacles, cactus . As the dinosaurs moved forward
run , The road ahead is constantly refreshing , The score in the upper right corner is also increasing .
This game , Count as Minimalist Super Mario 了 , Maybe it's a little surprise for people like me who have been interrupted by surfing the Internet .
The dinosaur will run faster and faster , Except for the cactus running on the ground (?), Soon there will be birds flying in the sky , Game idiots like me ,
The highest record is more than ten seconds . Hee hee , Today, Xiaobian will imitate this little dinosaur game for you , I hope you like it ~

Running environment
The environment used in this article :Python3.6、Pycharm Community Edition 、Pygame What comes with the game module will not be displayed .
pip install -i https://pypi.douban.com/simple/ + Module name Code display
import cfg
import sys
import random
import pygame
from modules import *
'''main'''
def main(highest_score):
# Game initialization
pygame.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption(' Little dinosaur game ')
# Import all sound files
sounds = {}
for key, value in cfg.AUDIO_PATHS.items():
sounds[key] = pygame.mixer.Sound(value)
# Game start screen
GameStartInterface(screen, sounds, cfg)
# Define some necessary elements and variables in the game
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
# The main cycle of the game
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)
# -- Add cloud randomly
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 cactus randomly / Flying dragon
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))))
# -- Update game elements
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
# -- collision detection
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)
# -- Draw game elements to the screen
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)
# -- Update screen
pygame.display.update()
clock.tick(cfg.FPS)
# -- Is the game over
if dino.is_dead:
break
# Game ending screen
return GameEndInterface(screen, cfg), highest_score
'''run'''
if __name__ == '__main__':
highest_score = 0
while True:
flag, highest_score = main(highest_score)
if not flag: breakEffect display
This is a color version of the little dinosaur game : The space starts the game 、 The space jumps up 、 And bird cactus obstacles ~
1) Game interface

2) Game begins

3) Game over

summary
Hee hee , This little dinosaur , Are you in love ?
If you like , Please help us with the triple ~,=
This is very important for Xiaobian at present . I wish you all. , Make a fortune , Universiade , A healthy body . Hee hee , Old rules Source code Find me Naza
Follow Xiaobian for more wonderful content ! Remember to click on the portal
If you need to package the complete source code + Free sharing of materials ~ Just call me !! Portal

边栏推荐
- Unity 脚本无法显示C#源码的中文注释 或者VS创建的脚本没有C#源码的注释
- 2022 college entrance examination quantitative paper | please answer the questions for quantitative candidates
- Is the financial management of qiniu school reliable and safe
- LabVIEW performs a serial loopback test
- C# Tryparse的用法
- HyperLeger Fabric安装
- LabVIEW用VISA Read函数来读取USB中断数据
- LabVIEW uses the visa read function to read USB interrupt data
- 希尔排序
- 【AI出牌器】第一次见这么“刺激”的斗地主,胜率高的关键因素竟是......
猜你喜欢

【Pygame小游戏】这款“打地鼠”小游戏要火了(来来来)

自制APP连接OneNET---实现数据监控和下发控制(MQTT)

easyrecovery15操作简单方便的数据恢复工具
![[appearance detection artifact] come on, please show me your unique skill (is this appearance worthy of the audience?)](/img/c4/b571bec5fe0cd2968e078a8231d0bd.png)
[appearance detection artifact] come on, please show me your unique skill (is this appearance worthy of the audience?)

Apple CMS collection station source code - building tutorial - attached source code - new source code - development documents

判等问题:如何确定程序的判断是正确的?
![[AI card player] for the first time to see such an](/img/7b/f1965a74733e152dcc542e465112b0.png)
[AI card player] for the first time to see such an "exciting" landowner, the key factor for a high winning rate is

【颜值检测神器】来,请拿出你们的绝活(这颜值,对得起观众么?)

SystemVerilog(十)-用户自定义类型

都说验证码是爬虫中的一道坎,看我只用五行代码就突破它。
随机推荐
【Pygame小游戏】激荡大脑思维,一起来玩转奇思妙想“24点”叭~(超赞滴)
LabVIEW编程规范
【Pygame小游戏】这款经典的炸弹人超能游戏上线,你爱了嘛?(附源码)
LabVIEW用VISA Read函数来读取USB中断数据
Four ways to add names to threads in the thread pool
Error report of curl import postman
Is the financial management of qiniu school reliable and safe
【Pygame合集】滴~穿越童年游戏指南 请查收:这里面有你玩过的游戏嘛?(附五款源码自取)
LabVIEW 禁止其他可多核心处理的应用程序在所有核心上执行
LabVIEW中创建毫秒时间标识
Flowable process deployment
LabVIEW用高速数据流盘
[pyGame] when the "coolest snake in history" arrives, fun will play it (no money for fun)
LabVIEW prohibits other multi-core processing applications from executing on all cores
【Pygame合集】回忆杀-“童年游戏”,看看你中几枪?(附五款源码自取)
What is the workflow of dry goods MapReduce?
How to handle the database query error with Emoji expression in Typecho- Xingze V Club
Dark horse headlines - Tencent's salary system reform has caused controversy; Intel expanded the recruitment of female engineers nationwide; Black horse is 100% employed really
Create millisecond time id in LabVIEW
Why many new websites are not included by search engines
