如何用Python写一个Google:Dino Game小恐龙快跑

import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置游戏窗口大小
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 200

# 设置颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# 创建游戏窗口
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])

# 设置游戏标题
pygame.display.set_caption('Google Dino')

# 加载跳跃音效
jump_sound = pygame.mixer.Sound('jump.wav')

# 加载障碍物图像
cactus_img = pygame.image.load('cactus.png')
bird_img = pygame.image.load('bird.png')

# 定义 Dino 类
class Dino(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load('dino.png')
        self.rect = self.image.get_rect()
        self.rect.x = 50
        self.rect.y = SCREEN_HEIGHT - 70
        self.jump_speed = 10
        self.gravity = 1

    def jump(self):
        self.rect.y -= self.jump_speed

    def update(self):
        self.rect.y += self.gravity
        if self.rect.y >= SCREEN_HEIGHT - 70:
            self.rect.y = SCREEN_HEIGHT - 70

# 定义障碍物类
class Obstacle(pygame.sprite.Sprite):
    def __init__(self, img):
        super().__init__()
        self.image = img
        self.rect = self.image.get_rect()
        self.rect.x = SCREEN_WIDTH
        self.rect.y = SCREEN_HEIGHT - 90

    def update(self):
        self.rect.x -= 5

# 创建 Dino 对象
dino = Dino()

# 创建障碍物组
obstacle_group = pygame.sprite.Group()

# 设置游戏帧率
clock = pygame.time.Clock()

# 初始化分数和难度
score = 0
difficulty = 1

# 游戏主循环
running = True
while running:

    # 处理游戏事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and dino.rect.y == SCREEN_HEIGHT - 70:
                dino.jump()
                jump_sound.play()

    # 更新 Dino 对象
    dino.update()

    # 创建障碍物
    if random.randint(1, 100) <= difficulty:
        obstacle_type = random.randint(1, 2)
        if obstacle_type == 1:
            obstacle = Obstacle(cactus_img)
        else:
            obstacle = Obstacle(bird_img)
        obstacle_group.add(obstacle)

    # 更新障碍物
    for obstacle in obstacle_group:
        obstacle.update()
        if obstacle.rect.x <= -50:
            obstacle_group.remove(obstacle)
            score += 1
            if score % 10 == 0:
                difficulty += 1

    # 检测碰撞
    if pygame.sprite.spritecollide(dino, obstacle_group, False):
        running = False

    # 绘制游戏画面
    screen.fill(WHITE)
    pygame.draw.rect(screen, BLACK, [0, SCREEN_HEIGHT - 70, SCREEN_WIDTH, 2])
    screen.blit(dino.image, dino.rect)
    obstacle_group.draw(screen)

    # 绘制分数
    font = pygame.font.Font(None, 36)
    text = font.render('Score: ' + str(score), True, RED)
    screen.blit(text, [SCREEN_WIDTH - 120, 10])

    # 更新游戏画面
    pygame.display.update()

    # 控制游戏帧率
    clock.tick(30)

# 结束 Pygame
pygame.quit()

请注意,在运行此游戏之前,您需要准备一些图像和音频文件,并将其命名为 dino.pngcactus.pngbird.png 和 jump.wav

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以提供一个简单的小恐龙代码: print("欢迎来到小恐龙的世界!") name = input("请输入你的小恐龙的名字:") print("你的小恐龙叫作" + name + "!") age = input("请输入你的小恐龙的年龄:") print("你的小恐龙今年" + age + "岁!") color = input("请输入你的小恐龙的颜色:") print("你的小恐龙是" + color + "色的!") ### 回答2: 下面是一个Python的小恐龙代码: ``` import turtle # 设置窗口 win = turtle.Screen() win.title("小恐龙") win.bgcolor("white") win.setup(width=800, height=600) win.tracer(0) # 创建小恐龙 dinosaur = turtle.Turtle() dinosaur.shape("turtle") dinosaur.color("green") dinosaur.penup() dinosaur.setpos(-350, 0) dinosaur.pendown() # 定义行走函数 def move(): dx = 2 # 小恐龙每次移动的距离 dinosaur.forward(dx) # 判断小恐龙是否碰到边界 if dinosaur.xcor() > 350: dinosaur.setx(-350) # 回到起始位置 win.ontimer(move, 10) # 每10毫秒调用一次move函数 # 开始移动小恐龙 move() # 关闭窗口的函数 def close_window(): win.bye() # 监听键盘事件 win.listen() win.onkey(close_window, "q") turtle.done() ``` 以上代码借助`turtle`模块创建了一个恐龙,并让它在窗口内部左右移动。通过监听键盘事件,可以通过按下"q"键来关闭窗口。 ### 回答3: 使用Python一个恐龙代码可以通过turtle模块实现。首先,导入turtle模块: import turtle 然后,初始化窗口和乌龟对象: window = turtle.Screen() dino = turtle.Turtle() 接下来,设置乌龟的形状和颜色: dino.shape("turtle") dino.color("green") 然后,编乌龟移动的函数,让乌龟能够在屏幕上移动: def move(): dino.forward(100) dino.backward(50) 接下来,设置键盘事件来控制乌龟的移动: window.onkey(move, "Up") window.listen() 最后,使用turtle.mainloop()启动游戏循环,让乌龟能够响应键盘事件并移动: turtle.mainloop() 这段代码实现了一个简单的小恐龙,通过按上箭头键来控制它向前移动100个像素,再按下箭头键让它向后移动50个像素,循环不断。当然,你也可以根据自己的需求进一步扩展代码,给恐龙添加其他动作和交互功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值