Python For Looping, Function Problems
I'm trying to create a little Guess The Flag game using Pygame and Python,
but there's a problem with some for looping and and functions I've
created. I'm relatively new to Python, and sometimes, I get very confused.
Anyway, I'm in a testing part of my game and I've got four countries:
China, Canada, USA, and France. I've put made them into separate defined
functions, put them in a list and used the random module to shuffle the
list. I've put it in a for loop, and I am trying to get it to work
desiredly.
It properly shuffles the list and picks the function, but there are
repeats of the same function and it never ends.
Here is my code:
def pick_country():
global list1
main_font = "brain flower euro.ttf"
font1 = pygame.font.Font((main_font), 50)
font2 = pygame.font.Font((main_font), 40)
list1 = [france1, canada1, china1, usa1]
random.shuffle(list1)
for cur_country in list1:
cur_country()
pygame.display.update()
def france1():
screen.fill((255, 255, 255))
country = 'FRANCE'
flag = 'france_flag.jpeg'
flag1 = pygame.image.load(flag).convert()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
pick_country()
screen.blit(flag1, (300, 350))
pygame.display.update()
def canada1():
name = ""
main_font = "brain flower euro.ttf"
font1 = pygame.font.Font((main_font), 50)
country = 'CANADA'
flag = 'canada_flag.jpeg'
black = 0, 0, 0
flag1 = pygame.image.load(flag).convert()
while True:
for evt in pygame.event.get():
if evt.type == KEYDOWN:
if evt.unicode.isalpha():
name += evt.unicode
elif evt.key == K_SPACE:
name += " "
elif evt.key == K_BACKSPACE:
name = name[:-1]
elif evt.type == QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
screen.blit(flag1, (200, 25))
pygame.draw.line(screen, black, (200, 115), (0, 115), (6))
pygame.draw.line(screen, black, (600, 115), (800, 115), (6))
block = font1.render(name, True, black)
rect = block.get_rect(center = (400, 600))
screen.blit(block, rect)
pygame.draw.rect(screen, black, (275, 550, 250, 100), (2))
if name == "canada":
pick_country()
pygame.display.update()
def usa1():
screen.fill((255, 255, 255))
country = 'USA'
flag = 'usa_flag.jpeg'
flag1 = pygame.image.load(flag).convert()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
pick_country()
screen.blit(flag1, (225, 300))
pygame.display.update()
def china1():
screen.fill((255, 255, 255))
country = 'CHINA'
flag = 'china_flag.jpeg'
flag1 = pygame.image.load(flag).convert()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
pick_country()
screen.blit(flag1, (300, 350))
pygame.display.update()
pick_country()
Don't ask about Canada; it's my main country testing area.
Before you start with 'what have you tried', I have tried the following:
1: Taking the 'list and shuffle' part of the script and putting it outside
the function, at the top of the screen. 1's Error: It doesn't know what
the functions in the list are (yet).
2: Taking the 'list and shuffle' part of the script and putting it outside
the function, after the country functions, yet before the actual
pick_country(). 2's Error: It chooses the country at first, but it
obviously never goes another one.
Any help? Please!
No comments:
Post a Comment