python 打地鼠小游戏

liftword1个月前 (05-24)技术文章36

给大家分享一段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系列」python几个重要模块的安装(二)

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

Python3+pygame实现的坦克大战

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

下雨天:女神说晚上有星星就跟我约会?Python带你绘制满天星河

导语"带你去看星星好吗?” “好”这句话真的是特别的浪漫,漫天星光,是你,余生是你,都是你~所有的晦暗都留给过往从遇见你开始,凛冬散尽,星河长明古往今来,带你去看星星这句话就是浪漫的代表。你是...

同一台电脑如何共存多个版本Python?

同一台电脑可以安装多个版本Python吗?可以共存!当前电脑安装了Python3.8.8 32位,为了调用大漠插件dll,但是我又想安装较新版本的64位Python3.12.0,就涉及到多个Pytho...