You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Pygame开发20秒按钮点击计数游戏遇阻,求技术支持

20秒点击计数游戏开发受阻,求推进思路

我参考他人代码做优化,想开发一款类似《Cookie Clicker》但没有升级系统的20秒按钮点击计数游戏,现在卡壳没法继续推进,以下是我已经完成的代码:

主游戏代码

import pygame
import button


pygame.init()

# 创建游戏窗口
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("点击计数游戏")

# 游戏变量
game_paused = True
menu_state = "main"

# 定义字体
font = pygame.font.SysFont("arialblack", 40)

# 定义颜色
TEXT_COL = (0, 0, 0)

# 加载按钮图片
resume_img = pygame.image.load("Resume.jpg").convert_alpha()
quit_img = pygame.image.load("Quit.jpg").convert_alpha()
back_img = pygame.image.load('Back.jpg').convert_alpha()

# 创建按钮实例
resume_button = button.Button(304, 200, resume_img, 1)
quit_button = button.Button(304, 300, quit_img, 1)
back_button = button.Button(332, 450, back_img, 1)


def draw_text(text, font, text_col, x, y):
  img = font.render(text, True, text_col)
  screen.blit(img, (x, y))

# 游戏循环
run = True
while run:

  screen.fill((255, 255, 255))

  # 检查游戏是否暂停
  if game_paused == True:
    # 检查菜单状态
    if menu_state == "main":
      # 绘制暂停界面按钮
      if resume_button.draw(screen):
        game_paused = False
      if quit_button.draw(screen):
        run = False
    # 检查是否打开选项菜单
    if menu_state == "options":
      # 绘制不同的选项按钮
      if back_button.draw(screen):
        menu_state = "main"
  else:
    draw_text("按SPACE键暂停", font, TEXT_COL, 160, 250)

  # 事件处理
  for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_SPACE:
        game_paused = True
    if event.type == pygame.QUIT:
      run = False

  pygame.display.update()

pygame.quit()

Button类代码

import pygame

# 按钮类
class Button():
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        self.clicked = False

    def draw(self, surface):
        action = False
        # 获取鼠标位置
        pos = pygame.mouse.get_pos()

        # 检查鼠标悬停和点击条件
        if self.rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True

        if pygame.mouse.get_pressed()[0] == 0:
            self.clicked = False

        # 在屏幕上绘制按钮
        surface.blit(self.image, (self.rect.x, self.rect.y))

        return action

内容的提问来源于stack exchange,提问作者Spireos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 17:15:43