2013-07-24 30 views
0

我已經完成了一個測驗程序..任何人都可以幫助我如何在用戶點擊錯誤的答案時顯示分數?也因爲我已經使用隨機函數,誰能告訴我如何避免重複兩個選項,因爲我有這個問題以及..如何在pygame中更新分數

import sys, pygame,random 
from pygame.locals import * 

#set up screen size 
size1 = width, height = 600,600 

# set up pygame, the window, and the mouse cursor 
pygame.init() 
pygame.mixer.init() 
mainClock = pygame.time.Clock() 
screen = pygame.display.set_mode(size1) 
pygame.display.set_caption('Know World Capitals') 

#set up colors 
WHITE=255,255,255 
BLACK = 35, 35, 35 
GREY=215,215,215 
ORANGE=246,173,70 

#screen color 
background = pygame.image.load("bckgrnd.jpg") 
backgroundRect = background.get_rect() 
background2 = pygame.image.load("bckgrnd2.jpg") 
background2Rect = background2.get_rect() 

#set up fonts 
font1 = pygame.font.SysFont('ORW Gothic L',48) 
font2 = pygame.font.SysFont('ORW Gothic L',22) 
font3 = pygame.font.SysFont('ORW Gothic L',30) 
#load game sounds 
wow= pygame.mixer.Sound('wow.wav') 
oops= pygame.mixer.Sound('lose.mp3') 

#load first set of pics 
a = pygame.image.load("b.jpg") 
aRect = a.get_rect() 
aRect.topleft=20,200  

b = pygame.image.load("b.jpg") 
bRect = b.get_rect() 
bRect.topleft=20,230 

c= pygame.image.load("b.jpg") 
cRect = c.get_rect() 
cRect.topleft=20,260 

d = pygame.image.load("b.jpg") 
dRect = d.get_rect() 
dRect.topleft=20,290 

#load 2nd set of pics 
f = pygame.image.load("a.jpg") 
fRect = f.get_rect() 
fRect.topleft=20,200  

g = pygame.image.load("a.jpg") 
gRect = g.get_rect() 
gRect.topleft=20,230 

h= pygame.image.load("a.jpg") 
hRect = h.get_rect() 
hRect.topleft=20,260 

j= pygame.image.load("a.jpg") 
jRect = j.get_rect() 
jRect.topleft=20,290 

#load screen1 pic 
e = pygame.image.load("fg2.png") 
eRect = e.get_rect() 
eRect.topleft=175,100 

#mouse pics 
p=Rect(20, 200,215,30) 
r=Rect(20,230, 215,30) 
s=Rect(20,260, 215,30) 
t=Rect(20,290, 215,30) 
u=Rect(545,475,70,31) 

#a list of the 51 countries 
S=['Jordan','Kazhakstan','Vietnam','Azerbaijan','Hungary','Libya','Seychelles','Cuba','Columbia','Venezuela','Madagascar','Israel','Qatar','Syria','Cyprus','Bahrain','Bulgaria','Belgium','Egypt','Barbados','Jamaica','Canada','Argentina','Chile','Peru','Honduras','Iceland','Saudi Arabia','Turkey','Thailand','Japan','India','Phillipines','Croatia','Greece','Romania','Finland','Russia','Lebanon','Uzbekistan','Bhutan','Bangladesh','Macedonia','Latvia','Ukraine','Slovakia','Norway','Poland','Lithuania','Mauritius','Kenya'] 
#a list of the 51 capitals 
C=['Amman','Akmola','Hanoi','Baku','Budapest','Tripoli','Tripoli','Havana','Bogota','Caracas','Antananarivo','Jerusalem','Doha','Damascus','Nicosia','Manama','Sofia','Brussels','Cairo','Bridgetown','Kingston','Ottawa','Buenos Aires','Santiago','Lima','Tegucigalpa','Reykjavik','Riyadh','Ankara','Bangkok','Tokyo','New Delhi','Manila','Zagreb','Athens','Bucharest','Helsinki','Moscow','Beirut','Tashkent','Thimphu','Dhaka','Skopje','Riga','Kiev','Bratislava','Oslo','Warsaw','Vilnius','Port Louis','Nairobi'] 

#get random place form a list 
def getRanP(myList): 
# This function returns a random state or capitol from the passed list. 
key = random.randint(0, len(myList) - 1) 
place=myList[key] 
return place 

#the game ends if this function is called 
def terminate(): 
pygame.quit() 
sys.exit() 

#checks if player wants to quit the game 
def waitForPlayerToPressKey(): 
    while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      terminate() 
     elif event.type == KEYDOWN: 
      if event.key == ord('w'): # pressing escape quits 
       terminate() 
      return 

#sets up grey colored text when given the text,font,surface and the x,y cordinates as attributes 
def drawText1(text, font, surface, x, y): 
w = font.render(text, 1, WHITE) 
v = w.get_rect() 
v.topleft = (x, y) 
surface.blit(w,v) 

#creates black-colored text when given the text,font,surface and the x,y cordinates as attributes 
def drawText2(text, font, surface, x, y): 
w = font.render(text, 1, WHITE) 
v = w.get_rect() 
v.topleft = (x, y) 
surface.blit(w,v) 

#game screens 
#set-up first screen 
def screen1(): 
    screen.blit(background2, background2Rect) 
pygame.mixer.music.load("www.ogg") 
    pygame.mixer.music.play(0) 
    pygame.display.flip() 
waitForPlayerToPressKey() 

#draw screen if player chooses a correct state 
def screen3(cS,cC): 
screen.blit(background, backgroundRect) 
drawText1('Y o u g o t i t !...,',font1,screen,175,120) 
drawText1(cC,font3,screen,250,210) 
drawText2('i s t h e C a p i t a l o f  ',font2,screen,125,245) 
drawText1(cS,font3,screen,325,245) 
drawText1('P r e s s  a n y  k e y  t o C o n t i n u e . . .',font2,screen,150,330) 
drawText2('O r P r e s s  W  t o Q u i t',font2,screen,165,385) 
pygame.display.flip() 
waitForPlayerToPressKey() 

#draw screen if player chooses the wrong state 
def screen4(cS,cC): 
screen.blit(background, backgroundRect) 
drawText1('Oops!...,',font1,screen,175,120) 
drawText1(cC,font3,screen,250,210) 
drawText2('i s t h e C a p i t a l o f  ',font2,screen,125,245) 
drawText1(cS,font3,screen,325,245) 
drawText1('P r e s s  a n y  k e y  t o Q u i t . . .',font2,screen,150,330) 
pygame.display.flip() 
waitForPlayerToPressKey() 


#check if player has clicked on the right or wrong state 
def check(v,cap,s,cS): 
waitForPlayerToPressKey() 
if C.index(cap)==S.index(s): 
    #play sound and draw screen3 if player has clicked on the right state 
    pygame.mixer.music.load("wow.ogg") 
      pygame.mixer.music.play(0) 
    screen3(cS,cap) 
    changeCol() 

else: 
    #play sound and draw screen4 if player has clicked on the wrong state 
    pygame.mixer.music.load("lose.ogg") 
      pygame.mixer.music.play(0) 
    screen4(cS,cap) 
    terminate()  



#main game screen      
def changeCol(): 
pygame.mixer.music.load("next.ogg") 
    pygame.mixer.music.play(0) 
    capitol=getRanP(C) 
n=C.index(capitol) 
y = random.randint(0, 4) 
key=n 
x=random.randint(0,5) 
S[n]=S[C.index(capitol)] 
cS=S[n] 

screen.blit(background, backgroundRect) 
drawText2(capitol+' is the capital of ?', font1, screen,35,90) 
#draw game-block choises on screen 
screen.blit(a,aRect) 
screen.blit(b,bRect) 
screen.blit(c,cRect) 
screen.blit(d,dRect) 
drawText1('C l i c k ',font2,screen,250,410) 
drawText1('O n t h e S t a t e t h a t H a s t h e a b o v e C a p i t a l',font2,screen,90,440) 
drawText1('t h e n P r e s s a n y k e y t o C o n t i n u e . . . ',font2,screen,140,470) 

#draw choises 

#randomly position the correct state in any of the choise blocks 
if y==0: 
    rdS1=S[n] 
    rdS2=S[random.randint(0,17)] 
    rdS3=S[random.randint(17,34)] 
    rdS4=S[random.randint(34,50)] 
    drawText1(rdS1, font2, screen,25,205) 
    drawText1(rdS2, font2, screen,25,235) 
    drawText1(rdS3, font2, screen,25,265) 
    drawText1(rdS4, font2, screen,25,295) 
    pygame.display.update()  
elif y==1: 
    rdS1=S[random.randint(0,17)] 
    rdS2=S[n] 
    rdS3=S[random.randint(17,34)] 
    rdS4=S[random.randint(34,50)] 
    drawText1(rdS1, font2, screen,25,205) 
    drawText1(rdS2, font2, screen,25,235) 
    drawText1(rdS3, font2, screen,25,265) 
    drawText1(rdS4, font2, screen,25,295) 
    pygame.display.update()   
elif y==2: 
    rdS1=S[random.randint(0,17)] 
    rdS2=S[random.randint(17,34)] 
    rdS3=S[n] 
    rdS4=S[random.randint(34,50)] 
    drawText1(rdS1, font2, screen,25,205) 
    drawText1(rdS2, font2, screen,25,235) 
    drawText1(rdS3, font2, screen,25,265) 
    drawText1(rdS4, font2, screen,25,295) 
    pygame.display.update() 
else: 
    rdS1=S[random.randint(0,17)] 
    rdS2=S[random.randint(17,34)] 
    rdS3=S[random.randint(34,50)] 
    rdS4=S[n] 
    drawText1(rdS1, font2, screen,25,205) 
    drawText1(rdS2, font2, screen,25,235) 
    drawText1(rdS3, font2, screen,25,265) 
    drawText1(rdS4, font2, screen,25,295) 
    pygame.display.update() 
while True: 
    #check which button the player clicks 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: sys.exit() 
      elif event.type == MOUSEBUTTONDOWN: 
      #return the poistion of the mouse 
      z=pygame.mouse.get_pos() 
      #when the player clicks on a button,change color of the button to white, check if the state the player has chosen is right or wrong & then ask him another question 
      if (p.collidepoint(z)): 
       screen.blit(f,fRect) 
       pygame.display.update() 
       check(n,capitol,rdS1,cS) 
      elif (r.collidepoint(z)): 
       screen.blit(g,gRect) 
       pygame.display.update() 
       check(n,capitol,rdS2,cS) 
      elif (s.collidepoint(z)): 
       screen.blit(h,hRect) 
       pygame.display.update() 
       check(n,capitol,rdS3,cS) 
      elif (t.collidepoint(z)): 
       screen.blit(j,jRect)  
       pygame.display.update() 
        check(n,capitol,rdS4,cS) 


#main game loop 
screen1() 
changeCol()     
+0

什麼「樂譜」你指的是? – sloth

+0

如果有人得到一個正確的答案,他得到一分..我想計算用戶得到的正確答案,直到他得到一個錯誤,並希望他得到一個錯誤的答案時顯示他的分數。 – Punith

回答

2

爲了防止重複的問題,我會用另一種解決方案,您的國家/資本名單。 而不是創建兩個列表中我會創建這樣一個字典:此得到不同的順序,你無需重複選項運行程序每次多米尼克

使用的意見後

d = {'Jordan':'Amman', 
    'Kazhakstan':'Akmola', 
    'Vietnam':'Hanoi', 
    'Azerbaijan':'Baku', 
    'Hungary':'Budapest'} 

編輯:

import random 

countries = list(d) 
random.shuffle(countries) 

for country in countries: 
    # Here your game code... 
    print("Capital of {0} is {1}".format(country, d[country])) 
+0

*你會得到每次運行程序時另一個命令*:這是不正確的。 (即使你目前使用的是當前版本的python實現,但你不能依賴它。) – sloth

+0

也許這取決於所使用的python版本?我每次都會收到另一個訂單..但是我發佈了random.shuffle解決方案,因爲我不確定依靠這個.. – joente

+0

@Dominic,這是他的第二個問題的答案,因爲通過混洗字典循環解決了問題重複使用隨機解決方案獲得的選項。 – joente

1

這只是一個使用字典和生成器產生隨機資本的例子。

import random 
d = {'Amman':'Jordan', 
    'Akmola':'Kazhakstan', 
    'Hanoi':'Vietnam', 
    'Baku':'Azerbaijan', 
    'Budapest':'Hungary'} 

def gen_capital(d): 
    "Generator that yields a random capital" 

    # Get the capitals 
    capitals = list(d) 

    # Shuffle the capitals in a random order 
    random.shuffle(capitals) 

    # Yield all the capitals in the list one by one 
    for capital in capitals: 
     yield capital 

def get_answers(countries, correct): 
    # Choose 3 random answers 
    answers = random.sample(countries, 3) 

    # Make sure the right answer is in the answer list 
    if not correct in answers: 
     answers[random.randint(0, 2)] = correct 
    return answers 

random_capital = gen_capital(d) 
countries = list(d.values()) 

在你的代碼可以使用這樣的事情

try: 
    capital = next(random_capital) 
except StopIteration: 
    pass # All capitals are asked.. 

下一頁問下一個資金,都使用get_answers(countries, d[capital])

注獲得隨機順序的答案:我用3個可能的答案但我認爲如何在4個答案中改變這一點很明顯。

舉個例子/測試,您可以通過循環,看看如何生成一個隨機的資本,與隨機的答案沒有重複:

for capital in random_capital: 
    print("{0} is the capital of ? ({1})".format(
     capital, ", ".join(get_answers(countries, d[capital])))) 

# Budapest is the capital of ? (Hungary, Vietnam, Jordan) 
# Baku is the capital of ? (Jordan, Azerbaijan, Vietnam) 
# Akmola is the capital of ? (Kazhakstan, Azerbaijan, Jordan) 
# Hanoi is the capital of ? (Vietnam, Jordan, Azerbaijan) 
# Amman is the capital of ? (Hungary, Kazhakstan, Jordan)