import pygame
from pygame.locals import *
import random
import time
pygame.init()
randomNumber = random.randint(1,600)
randomNumber2 = random.randint(1,600)
x = 0
text = ""
squareCount = 0
beenHere = 0
# colours = (red, green, blue)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
LBLUE = (0, 123, 255)
colour = RED
# Make a window appear
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Square Chase | Score: 0")
screen.fill(LBLUE)
pygame.display.flip()
pygame.time.set_timer(USEREVENT + 1, 1500)
done = False
clock = pygame.time.Clock()
while done == False:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
done = True
if event.type == USEREVENT + 1:
screen.fill(LBLUE)
randomNumber = random.randint(1,625)
randomNumber2 = random.randint(1,420)
mySquare = pygame.draw.rect(screen,colour,(randomNumber,randomNumber2,50,50),5)
squareCount = squareCount + 1
if squareCount == 50:
done == true
pygame.display.flip()
if event.type == pygame.MOUSEBUTTONDOWN:
y, z = pygame.mouse.get_pos()
is_inside = mySquare.collidepoint(y, z)
if is_inside and colour == GREEN:
x = x+1
text = str(x)
pygame.display.set_caption("Square Chase | Score " + text)
colour = RED
elif is_inside:
x = x+1
text = str(x)
pygame.display.set_caption("Square Chase | Score " + text)
colour = GREEN
clock.tick(20)
pygame.quit()
我的目标是创建一个游戏,用户必须点击正方形才能增加分数。他们有50次机会这样做。如果他们在1.5秒内点击正方形,则方块的颜色会发生变化。
以上代码的工作区别于每次绘制一个新的方块,用户可以点击它说5次,他们的分数会上升5个。有关如何让分数增加一个的任何建议吗?提前谢谢。
答案 0 :(得分:2)
希望我可以使用评论,但我缺乏声誉。
我没有浏览你的所有代码,但是你的描述听起来像一个简单的旗帜就能帮助你。
一旦您识别出已单击,则增加分数并设置布尔值。
所以从本质上讲,你会想要一些符合
的内容if clicked == false
score = score+1
clicked = true
一旦出现另一个块,您将希望将该标志清除为假。
答案 1 :(得分:0)
如果在成功按键后出现一个新的广场会不会更好?这样游戏就会更有活力。
很容易改变这一点。不是每隔1.5秒使用一次调用的用户事件,而是将clock.tick()返回的值相加,当它们的总和大于1500毫秒时,创建一个新的矩形。这是一种更好的方法,因为您可以调用创建新方块的方法并在成功按键后立即重置计时器。
一些代码可以帮助您入门:
def new_rect(screen,colour):
screen.fill(LBLUE)
randomNumber = random.randint(1,625)
randomNumber2 = random.randint(1,420)
mySquare = pygame.draw.rect(screen,colour,(randomNumber,randomNumber2,50,50),5)
return mySquare
done = False
clock = pygame.time.Clock()
timer_var = 0
while done == False:
if(timer_var > 1500):
timer_var = 0
mySquare = new_rect(screen,color)
squareCount = squareCount + 1
if squareCount == 50:
done == true
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
y, z = pygame.mouse.get_pos()
is_inside = mySquare.collidepoint(y, z)
if is_inside:
x = x+1
text = str(x)
pygame.display.set_caption("Square Chase | Score " + text)
timer_var = 0
new_rect(screen,colour)
squareCount = squareCount + 1
if squareCount == 50:
done == true
if colour == GREEN:
colour = RED
else:
colour = GREEN
timer_var += clock.tick(20)
pygame.quit()