gpt4 book ai didi

python - 创建一个计算按钮被点击次数的系统

转载 作者:行者123 更新时间:2023-12-05 04:21:55 24 4
gpt4 key购买 nike

<分区>

使用其他人的代码,我想改进它并制作我自己的游戏版本,该游戏可以计算按钮被点击的次数,计时器只有 20 秒。我想让它有点类似于“cookie clicker”,没有任何升级系统。我一直在寻求帮助,但我走到了死胡同,到目前为止,这是我的代码:

import pygame
import button


pygame.init()

#create game window
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Clicker Game")

#game variables
game_paused = True
menu_state = "main"

#define fonts
font = pygame.font.SysFont("arialblack", 40)

#define colours
TEXT_COL = (0, 0, 0)

#load button images
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()

#create button instances
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))

#game loop
run = True
while run:

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

#check if game is paused
if game_paused == True:
#check menu state
if menu_state == "main":
#draw pause screen buttons
if resume_button.draw(screen):
game_paused = False
if quit_button.draw(screen):
run = False
#check if the options menu is open
if menu_state == "options":
#draw the different options buttons
if back_button.draw(screen):
menu_state = "main"
else:
draw_text("Press SPACE to pause", font, TEXT_COL, 160, 250)

#event handler
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()

按钮:

import pygame

#button class
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
#get mouse position
pos = pygame.mouse.get_pos()

#check mouseover and clicked conditions
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

#draw button on screen
surface.blit(self.image, (self.rect.x, self.rect.y))

return action

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com