说到google chrome,很多人都会想到它标志性的断网小游戏——chrome dino,今日,我们利用python还原并将代码开源,欢迎随时取用。话不多说,直接进入正题
第一部分:配置环境 编译器:pycharm社区版2024.1 插件:pygame
导入所用库,没有的可以去下载,具体方法不多赘述,网上有
1 2 3 import pygameimport sysimport random
第二部分:设置基础变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 pygame.init() screen_width = 800 screen_height = 400 screen = pygame.display.set_mode((screen_width, screen_height)) white = (255 , 255 , 255 ) black = (0 ,0 ,0 ) green = (0 ,255 ,0 ) clock = pygame.time.Clock() font = pygame.font.Font(None , 50 )
接下来的步骤很重要,先放代码后讲解操作
1 2 3 4 5 6 7 8 9 10 11 try : dino_image = pygame.image.load('dino.png' ).convert_alpha() cactus_image = pygame.image.load('cactus.png' ).convert_alpha() scale_factor = 0.4 dino_image = pygame.transform.scale(dino_image, (int (dino_image.get_width() * scale_factor), int (dino_image.get_height() * scale_factor))) cactus_image = pygame.transform.scale(cactus_image, (int (cactus_image.get_width() * scale_factor), int (cactus_image.get_height() * scale_factor))) except pygame.error as e: print (f"Cannot load image: {e} " ) sys.exit()
有经验的老同志可能看出来了,第2,3行图片并没有,所以需要从网上找到恐龙和仙人掌图片后分别重命名为dino.png 和cactus.png,对应代码中的第2 3行,并且把图片和代码源文件放置在同一文件夹里面!!!!要不然系统找不到!!!!如果嫌烦在本文结尾评论或者在B站私信我,我会将图片发送
第三部分:加载角色类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 class Dino (pygame.sprite.Sprite): def __init__ (self ): super (Dino, self).__init__() self.image = dino_image self.rect = self.image.get_rect() self.rect.x = 100 self.rect.y = screen_height - self.rect.height - 50 self.gravity = 0.5 self.velocity_y = 0 self.jumping = False self.jump_height = -18 def update (self ): if self.jumping: self.velocity_y += self.gravity self.rect.y += self.velocity_y if self.rect.bottom >= screen_height - self.rect.height: self.velocity_y = 0 self.jumping = False else : self.velocity_y = 0 def jump (self ): if not self.jumping: self.jumping = True self.velocity_y = self.jump_height class Obstacle (pygame.sprite.Sprite): def __init__ (self ): super (Obstacle, self).__init__() self.image = cactus_image self.rect = self.image.get_rect() self.rect.x = screen_width self.rect.y = screen_height - self.rect.height - 55 self.speed = 2.7 def update (self ): self.rect.x -= self.speed if self.rect.x < -self.rect.width: self.kill() dino = Dino() obstacles = pygame.sprite.Group() score = 0 game_over = False
这一步中跳跃高度是我调教过的,可以自己更改,也可以搭配我提供的图片使用
第四部分:游戏主循环 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: dino.jump() dino.update() obstacles.update() if random.randint(1 , 120 ) == 1 and not game_over: obstacles.add(Obstacle()) if random.randint (1 ,120 ) == 2 and not game_over: obstacles.add = 1 if pygame.sprite.spritecollide(dino, obstacles, False ): game_over = True screen.fill(white) screen.blit(dino.image, dino.rect) for obstacle in obstacles: screen.blit(obstacle.image, obstacle.rect) score_text = font.render(f"Score: {int (score)} " , True , black) screen.blit(score_text, (10 , 10 )) pygame.display.flip() clock.tick(60 ) if not game_over: score += 0.1 if game_over: screen.fill(black) over_font = pygame.font.Font(None , 70 ) over_text = over_font.render("Game Over" , True , white) over_rect = over_text.get_rect(center=(screen_width // 2 , screen_height // 2 )) screen.blit(over_text, over_rect) pygame.display.flip() pygame.time.wait(3000 ) running = False pygame.quit() sys.exit()
这一部分需要注意的是:障碍物生成频率和帧率是我调教过的,不要乱改!!!不要乱改!!!改了以后就会鬼畜一样几十个卡在一起出现,别问我怎么知道的。
尾声 以上就是我的最新python作品的代码开源和介绍了,本文和代码为我原创,您可以拿去自用,但这是创作者的心血!!所以如果您要摘编请与我联系(一初中生的文章有啥好转载的,对吧【滑稽】),视频介绍请见哔哩哔哩,预计7月上旬发布(账号:Terry—zhang同学,本网站有链接)在此感谢您的观看,欢迎评论。