0

我做了一个测验程序..当用户点击错误答案时,谁能帮我显示分数?也因为我使用了随机函数,任何人都可以告诉我如何避免重复两个选项,因为我也有这个问题..

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

2 回答 2

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]))
于 2013-07-24T14:50:00.800 回答
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)
于 2013-07-29T08:59:59.800 回答