Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i make something happen for every item in a list with a delay?

I'm trying to make a memory game where there a four colors, and you have to match the pattern while the program keeps adding +1 to the list.

right now i cant figure out how to get the the game to Display the pattern. and then wait for player input to end the game or add another color to the pattern.

This is all i can come up with:

def addlist():
    if playerpattern == pattern:
        pattern.append(random.randint(1, 4)) 

def listcheck():
    if playerpattern == pattern:
        for i in pattern:
            if i == 1:
                topleft.color = (255, 0, 0)
            else:
                topleft.color = (100, 0, 0)
            if i == 2:
                topright.color = (0, 0, 255)
            else:
                topright.color = (0, 0, 175)
            if i == 2:
                bottomleft.color = (0, 255, 0)
            else:
                bottomleft.color = (0, 175, 0)
            if i ==4:
                bottomright.color = (255, 255, 0)
            else:
                bottomright.color = (175, 175, 0)

pattern = []
playerpattern = []
playing = True

while playing:
    clock.tick(10)
    print(pattern)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            playing = False
        if event.type == MOUSEBUTTONDOWN:
            mouseclick()

    mousehover()
    addlist()
    listcheck()
    draw()

pygame.quit()

Basically when the game starts I want the game to light up pattern[0] (one of the 4 squares).

Then when the player gets it right display pattern[0] and then pattern[1] with a delay and then repeat until its over. Thanks for helping

like image 916
A.J. Avatar asked Nov 06 '22 16:11

A.J.


1 Answers

pygame has a timer functionality pygame.time.set_timer().

Define an event id (7 is just any number, you can define this number)

myEventID = pygame.USEREVENT+7

Start the timer with an certain interval time e.g. 1 second (1000 milliseconds)

pygame.time.set_timer(myEventID, 1000) # 1000 milliseconds interval

Get the event notification every time when the time span has expired:

for event in pygame.event.get():

    if event.type == myEventID:
        # [...]

Stop the timer by passing 0 to the milliseconds argument:

pygame.time.set_timer(myEventID, 0)

Add a game state variable, which indicates the current state of the game and modify this variable in the timer. The state is a integral number, has a specific meaning e.g.

gameState = 0
while playing: 

    for event in pygame.event.get():

        if event.type == myEventID:
            # change the game state - this is just an example
            if gameState == 0:
                gameState = 1 


    if gameState == 0:

        # do default

    elif gameState == 1:

        # do something else

    elif gameState == 2:

       # do something completely different

    else:

       # another case 
like image 165
Rabbid76 Avatar answered Nov 14 '22 01:11

Rabbid76