2014-01-30 94 views
0
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秒內點擊廣場,廣場的顏色會發生變化。Python點擊遊戲,更新分數

上面的代碼除了每次畫一個新的方塊以外,用戶都可以點擊它5次,他們的分數將會增加5.任何有關如何讓分數增加一個的建議?提前致謝。

回答

2

希望我只能使用評論,但我缺乏聲譽。

我沒有翻看所有的代碼,但是你的描述使得它聽起來像一個簡單的標誌將能夠幫助你。

一旦你認識到它已被點擊,增加分數並設置布爾值。

因此,在本質你會想沿着你將要清除該標誌爲false,一旦另一塊已經出現的

if clicked == false 
    score = score+1 
    clicked = true 

線的東西。

+1

@ user2849651如果這回答了你的問題,你可以標記此爲經批准的答案嗎? – TheOneWhoPrograms

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()