クイズ プログラムを作成しました。ユーザーが間違った答えをクリックしたときにスコアを表示する方法を教えてもらえますか?? また、私はランダム関数を使用しているので、私もその問題を抱えているので、2つのオプションが繰り返されるのを避ける方法を誰か教えてもらえますか..
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()