python谷歌浏览器dino游戏,完整开源代码

观前提示:本文选自作者个人博客,为获得更好观感,请访问博主博客得到更好体验)

说到google chrome,很多人都会想到它标志性的断网小游戏——chrome dino,今日,我们利用python还原并将代码开源,欢迎随时取用。话不多说,直接进入正题

实现效果

 

第一部分:配置环境

编译器:pycharm社区版2024.1
插件:pygame

导入所用库,没有的可以去下载,具体方法不多赘述,网上有

import pygame
import sys
import random

第二部分:设置基础变量

# 初始化pygame
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)


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行,并且把图片和代码源文件放置在同一文件夹里面。如果嫌烦在本文结尾评论或者向我的邮箱 zhang@mrzxr.com  zhang@mrzxr.com 任意 发邮件索要图片。会魔法的同志可通过文章末尾到github仓库直接下载完整代码和图片。

 

第三部分:加载角色类

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

这一步中跳跃高度是我调教过的,可以自己更改,也可以搭配我提供的图片使用

 

第四部分:游戏主循环

# 游戏主循环
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: # 每120帧大约有1个障碍物生成
        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
pygame.quit()
sys.exit()

这一部分需要注意的是:障碍物生成频率和帧率是调教过的,建议不要改,改了以后就会鬼畜一样几十个卡在一起出现,别问我怎么知道的。

 

一些链接

原文链接(作者博客):

python新作——谷歌浏览器dino游戏 | 小章同学

 

github仓库:

GitHub - terryzhangxr/google-chrome-dino-game-python-code: This repo is about the google chrome dino game which is written by python,you can find the description at https://mrzxr.top/

本文完

 

好的,我可以为您介绍一个使用Python编写的简单恐龙游戏代码。这个代码模拟了谷歌浏览器离线时出现的小恐龙跳跃游戏。以下是一个基本的实现: ```python import pygame import random # 初始化pygame pygame.init() # 设置游戏窗口 WIDTH = 800 HEIGHT = 400 win = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Chrome Dino Game") # 定义颜色 WHITE = (255, 255, 255) BLACK = (0, 0, 0) # 加载恐龙图片 dino_img = pygame.image.load('dino.png') dino_width = dino_img.get_width() # 定义恐龙类 class Dino: def __init__(self): self.x = 50 self.y = HEIGHT - dino_img.get_height() - 30 self.vel_y = 0 self.is_jumping = False def jump(self): if not self.is_jumping: self.vel_y = -15 self.is_jumping = True def update(self): self.vel_y += 1 self.y += self.vel_y if self.y >= HEIGHT - dino_img.get_height() - 30: self.y = HEIGHT - dino_img.get_height() - 30 self.is_jumping = False self.vel_y = 0 def draw(self, win): win.blit(dino_img, (self.x, self.y)) # 定义障碍物类 class Obstacle: def __init__(self): self.x = WIDTH self.y = HEIGHT - 30 self.width = 20 self.height = 30 self.vel_x = 10 def update(self): self.x -= self.vel_x def draw(self, win): pygame.draw.rect(win, BLACK, (self.x, self.y, self.width, self.height)) # 主游戏函数 def game(): clock = pygame.time.Clock() run = True dino = Dino() obstacles = [] spawn_timer = 0 while run: clock.tick(30) # 设置帧率为30FPS for event in pygame.event.get(): if event.type == pygame.QUIT: run = False # 处理键盘输入 keys = pygame.key.get_pressed() if keys[pygame.K_SPACE] or keys[pygame.K_UP]: dino.jump() # 更新游戏元素 dino.update() spawn_timer += 1 if spawn_timer > 30: obstacles.append(Obstacle()) spawn_timer = 0 for obstacle in obstacles: obstacle.update() # 移除离开屏幕的障碍物 obstacles = [obstacle for obstacle in obstacles if obstacle.x + obstacle.width > 0] # 检测碰撞 for obstacle in obstacles: if (dino.x < obstacle.x + obstacle.width and dino.x + dino_width > obstacle.x and dino.y < obstacle.y + obstacle.height and dino.y + dino_img.get_height() > obstacle.y): run = False # 游戏结束 # 绘制游戏画面 win.fill(WHITE) dino.draw(win) for obstacle in obstacles: obstacle.draw(win) pygame.display.update() pygame.quit() # 运行游戏 game() ``` 这个代码实现了一个基本的恐龙游戏,包括以下特点: 1. 使用Pygame库来创建游戏窗口和处理图形。 2. 创建了Dino类来管理恐龙的行为,包括跳跃和移动。 3. 创建了Obstacle类来管理障碍物的生成和移动。 4. 实现了简单的碰撞检测,当恐龙碰到障碍物时游戏结束。 5. 恐龙可以通过按空格键或向上箭头键来跳跃。 要运行这个游戏,你需要安装Pygame库,并准备一张恐龙的图片(命名为dino.png)放在与Python脚本相同的目录下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值