首页 > 编程语言 >The Imitate Google Dinosaur for the python <--CSDN @PythonWIN-->

The Imitate Google Dinosaur for the python <--CSDN @PythonWIN-->

时间:2024-06-09 19:58:40浏览次数:18  
标签:Google join -- getcwd Imitate score path os resources

-  This is a small software based on pygame:

        -We have used the well-known Python language

-I am currently developing Imitate Google Dinosaur version 1.1 and using Python as my editor.

Now let me explain in detail its functions:

        -Firstly, control the up arrow and space bar. The Jumping of Little Dinosaurs

        -Secondly, the lower bound controls the crouching of the little dinosaur

-About Display:

        -The one on the left after hi above represents the highest score in history, and the one on the right represents it. How many points are there now?

        -Failed, there is a special sound effect

        -Background movement

               -----Follow me for Chinese version(关注我,查看中文版)

--------------------------------------------------------------------------------------

First, let's talk about an auxiliary package we made ourselves:

Chinese annotation:

cfg.py:
'''配置文件'''
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)

This is a very simple path package-----We will use this later

        The Chinese annotation is used above, and I will write another English annotation below

English annotation:

'''configuration file'''
import os


'''Screen size'''
SCREENSIZE = (600, 150)
'''FPS'''
FPS = 60
'''Audio material path'''
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 Material Path'''
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'''
BACKGROUND_COLOR = (235, 235, 235)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

--------------------------------------------------------------------------------------

Enter the main program

Import it into the main program

import cfg

Define a main program and initialize:

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

Enter the main program:

# 游戏主循环
	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 to its main interface:(important)

return GameEndInterface(screen, cfg), highest_score

--------------------------------------------------------------------------------------
Final cycle

if __name__ == '__main__':
	highest_score = 0
#Think about the rest for yourself

This is all the program!!!

--------------------------------------------------------------------------------------

If you want the full program.

        -Please add my QQ or WeChat:
                My QQ is '2693803445'
                My WeChat is 'creategreater'

标签:Google,join,--,getcwd,Imitate,score,path,os,resources
From: https://blog.csdn.net/PythonWIM/article/details/139564982

相关文章

  • The Table Pet for the python <--CSDN @PythonWIN-->
    - ThisisasmallsoftwarebasedonpyQT5:    -Wehaveusedthewell-knownPythonlanguage-IamcurrentlydevelopingdesktoppetSVIPversion1.1andusingPythonasmyeditor.Nowletmeexplainindetailitsfunctions:    -First,Anim......
  • Django API开发实战:前后端分离、Restful风格与DRF序列化器详解
    系列文章目录Django入门全攻略:从零搭建你的第一个Web项目DjangoORM入门指南:从概念到实践,掌握模型创建、迁移与视图操作DjangoORM实战:模型字段与元选项配置,以及链式过滤与QF查询详解DjangoORM深度游:探索多对一、一对一与多对多数据关系的奥秘与实践跨域问题与Django解决......
  • 鸿蒙创意开发2.0
    #鸿蒙期末作业#补救,基于鸿蒙开发的鸿蒙创意软件的外观已经完善了,由于上一篇内容质量太低,这期作者尽量把重点解说一下。本次开发创意型计算器1.创建一个计算器按钮先上代码://如果只有一个component显示不了,声明预览@Preview//因为没有设置高宽所以组件就一点//声明组件@......
  • 拿捏红黑树(C++)
    文章目录前言一、红黑树介绍二、插入操作三、验证红黑树四、红黑树与AVL性能比较与应用五、总体代码总结前言我们之前介绍了一种AVL的高阶数据结构,在本篇文章中,我们将会介绍一种与AVL旗鼓相当的数据结构–红黑树。我们并且会对它的部分接口进行模拟实现一、红黑树......
  • Kafka 主题 CLI 教程
    KafkaTopicsCLI,即kafka-topics用于创建、删除、描述或更改Kafka中的主题。请确保您已预先启动Kafka 如何创建Kafka主题?要创建Kafka主题,我们需要提供必需的参数:如果是Kafkav2.2+,请使用Kafka主机名和端口,例如,localhost:9092如果是旧版本的Kafka,请使用Zook......
  • 第三个计划表出炉
    2024年6月9号编写从2024年6月9号到2029年12月31号计划:1、2025年底必须结婚2、2026年考虑孩子的事3、2027年考虑是否要继续待北京4、2028年,如果27年工作不变,继续维持。如果有变动,尽量保持稳定5、2029年要考虑更多关于孩子的事理财计划:1、2024年年底全部撤出,至少目前主要账号成正......
  • BUUCTF爱因斯坦(杂项)
    开局一张图 顺手看了眼详细内容,果然  用010editor看了下应该是包含文件了,用binwalk试了下果然 binwalk-emisc2.jpg得到一个压缩包看了看010editor,也不是伪加密,暴力浅试了试也不对这个密码愣是没想到 行百里者半九十,最后把前面没用到的this_is_not_password输进......
  • python 字典是不是线程安全的
    Python字典(dict)对象本身不是线程安全的。在多线程环境下,对同一个字典对象的读写操作需要额外的同步机制来确保线程安全性。如果需要在多线程环境下使用线程安全的字典,可以使用collections.Counter对象,它是线程安全的,或者使用threading.local,它提供了线程局部存储的功能。另外......
  • 简单工厂模式( Simple Factory Pattern )
    简单工厂模式(SimpleFactoryPattern),在工厂类中对象决定创建出哪一种产品类的实例。这些产品类都实现了相同的接口,或者继承了相同的父类。结构图Factory(工厂角色):它是核心,负责实现创建所有实例的内部逻辑。在工厂类中,提供了一个静态方法,可以直接被外界直接调用,以创建具体产品......
  • 03-软件使用素材资源链接
    目录页1.设计类资源导航1.1.设计素材网站1.2.图形处理1.2.1.PPT图形素材1.3.影视动画1.设计类资源导航1.1.设计素材网站设计素材类免费下载网站名优缺点备注站长素材分享综合设计素材免费下载的平台(字体、音频、PPT、PSD素材)http://sc......