python 打地鼠小游戏

给大家分享一段AI自动生成的代码(在这个游戏中,玩家需要在有限时间内打中尽可能多的出现在地图上的地鼠),由于我现在用的这个电脑没有安装sublime或pycharm等工具,所以还没有测试,有兴趣的朋友可以跑一下试试。

注意,在代码的第28行,“mole_image = pygame.image.load("mole.png").convert_alpha()”需要先准备一个png格式的图片,并将图片路径完整准确复制到代码中。

import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置游戏窗口尺寸
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
window_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))

# 设置游戏标题
pygame.display.set_caption("打地鼠")

# 设置游戏背景色
BACKGROUND_COLOR = (255, 255, 255)

# 设置地鼠的大小
MOLE_SIZE = 80

# 设置地鼠出现的时间间隔(单位:毫秒)
MOLE_INTERVAL = 1000

# 设置游戏时长(单位:秒)
GAME_DURATION = 30

# 加载地鼠的图片
mole_image = pygame.image.load("mole.png").convert_alpha()

# 定义地鼠类
class Mole:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.visible = False
        self.timer = 0

    # 在游戏窗口上绘制地鼠
    def draw(self):
        if self.visible:
            window_surface.blit(mole_image, (self.x, self.y))

    # 更新地鼠的状态
    def update(self, delta_time):
        self.timer -= delta_time
        if self.timer <= 0:
            self.visible = not self.visible
            self.timer = MOLE_INTERVAL

# 创建地鼠列表
moles = []
for i in range(3):
    for j in range(3):
        x = 100 + i * 200
        y = 100 + j * 200
        moles.append(Mole(x, y))

# 初始化游戏计时器
game_timer = pygame.time.Clock()
game_start_time = pygame.time.get_ticks()
game_end_time = game_start_time + GAME_DURATION * 1000

# 初始化得分
score = 0

# 游戏主循环
while True:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        # 处理鼠标事件
        if event.type == pygame.MOUSEBUTTONDOWN:
            for mole in moles:
                if mole.visible:
                    mouse_x, mouse_y = pygame.mouse.get_pos()
                    if mouse_x >= mole.x and mouse_x < mole.x + MOLE_SIZE and mouse_y >= mole.y and mouse_y < mole.y + MOLE_SIZE:
                        mole.visible = False
                        score += 1

    # 更新游戏状态
    current_time = pygame.time.get_ticks()
    delta_time = current_time - game_timer.tick(60)

    for mole in moles:
        mole.update(delta_time)

    # 在游戏窗口上绘制游戏元素
    window_surface.fill(BACKGROUND_COLOR)

    for mole in moles:
        mole.draw()

    # 在游戏窗口上绘制得分
    font = pygame.font.SysFont(None, 48)
    text_surface = font.render("得分:" + str(score), True, (0, 0, 0))
       # 绘制得分
    text = font.render("得分: " + str(sum([mole.points for mole in all_moles])), True, BLACK)
    screen.blit(text, (10, 10))

    # 刷新屏幕
    pygame.display.flip()

    # 控制帧率
    clock.tick(FPS)

# 退出pygame
pygame.quit()

相关文章

怎么安装python的pygame库文件

怎么安装python的pygame库文件?点击“开始菜单”,搜索程序“cmd”,鼠标右键,选择“以管理员身份运行”。推荐:《Python教程》输入代码“pip install pygame”,选择“剪...

python安装 pygame

通过安装的PyCharm,在里面启动安装在终端里面输入pip install pygame,安装成功...

零基础学习编程:用Python和Pygame实现2048游戏

预计阅读时间:30分钟简介编程是一门强大的工具,可以帮助我们解决问题、创造创新,并提升我们的思维能力。对于那些零基础的人来说,学习编程可能会感到有些困惑。本教程将带领您逐步学习编程的基础知识,并通过使...

「Python系列」python几个重要模块的安装(二)

一、 python的pygame的安装:安装地址:https://www.cnblogs.com/charliedaifu/p/9938542.htmlpyagme包下载地址:https://down...

Python3+pygame实现的坦克大战

一、显示效果二、代码1. 说明几乎所有pygame游戏,基本都遵循一定的开发流程,大体如下:初始化pygame创建窗口while循环检测以及处理事件(鼠标点击、按键等)更新UI界面2. 代码创建一个m...

pymunk,一个超酷的 Python 库!

大家好,今天为大家分享一个超酷的 Python 库 - pymunk。Github地址:https://github.com/viblo/pymunkPymunk是一个基于Chipmunk物理引擎的P...