[Tutor] simon game issues

Dave Angel d at davea.name
Fri Sep 14 00:25:49 CEST 2012

On 09/13/2012 10:07 AM, Matthew Ngaha wrote:
> Hi guys. my Python tutorial set me a task to recreate a simon game
> using livewires.
> http://www.youtube.com/watch?v=agUABjGAJww
> this is quite long, i understand if you can't help right away
> summary:
> i create 4 squared shaped objects(red, blue, green, yellow) as
> instances for a class derived from the Sprite class
> i create 4 animation objects to represent the 4 colour shaped objects.
> the animations get brighter in colour so the user can understand which
> colour he has to follow
> the animations also light up when the user presses the keyboard key
> they are assigned too.
> they are instances for a class derived from the Animation class.
> i have also included 4 sound files, assigned to 1 animation object to
> be played each time that animation lights up
> My problems:
> everytime i want to play a sequence for the user to follow. i run into
> this problem. i create 1 animation say red, so the user can see the
> 1st colour he has to follow. then i put a timed delay time.sleep(1)
> before i display the next animation, say blue. i have tried many other
> timing methods to achieve this, but once there is some sort of timer,
> the whole screen pauses for the duration of the timed delay (but the
> sound files play simultaneously during that freeze) then after that,
> the sequence is displayed at the same time. not 1 after the other. so
> even though i put the timer after the creation of the 1st animation,
> the timer for some reason happens before it. is there a way i can get
> around this?
> I also start the game/program by displaying the 4 square objects
> (Sprite Class) before adding their animation. after the creation of
> the sprites i play the animation sequence. The problem is they play as
> soon as the game starts before the Sprites are even displayed, and
> also simultaneously (not in a sequnce) making it confusng. so after
> creating the sprites, i added a timer so the animations happens after
> the sprites are shown. But again because of this timer, which is meant
> to occur after the creation of the sprites, when i start the game, i
> get a blank screen for the duration of the timer((but the sound files,
> again play simultaneously during that time). After that i still don't
> get the sprites, i get their animatons instead, then finally the
> sprites are displayed. i dont understand, the Sprites are meant to be
> the 1st things on the screen, is there a solution?
> Getting user input:
> the last problem i have is when the user presses a key to select a
> colour. i have the script check for the key press, then return the
> colour to a function/method called def result(self, color): that
> determines if he got the right colour. if he does i increase score by
> 1. to check results i create 2 lists. one with all 4 colours:
> self.animation = [Pressed.dict_red, Pressed.dict_blue,
> Pressed.dict_green, Pressed.dict_yellow]
> then i create an empty list to append the sequence in the order i
> randomly played them, so i can check if the user got each colour:
> self.get_animation = [] < they append from the 1st list above and my
> result function uses it to check colours
> here's how it goes.
> if keyboard for colour red is pressed:
> create red animation:
> send results back to result method: self.game.result(red)
> my result method and its class:
> self.stage = 3       ..#say if we were on stage 3. i would play 3
> colours. stage 4 would be 4 colours
> self.counter = -1         .....#to correctly index list
> def result(self, COLOUR):
>     self.counter += 1    #count is now at 0
>     self.end_testing = self.stage        ..#this will count down from
> the stages value. once it reaches 0, there is no more colours to check
> for
>     if COLOUR in self.get_animation[self.counter]:
>         self.score.value += 1
>         self.end_testing -= 1
> even though i am using a counter here to count down: self.end_testing
> < i have used while and for loops also. My problem is none of these
> work because a keypress from the user, even if tapped, registers as
> multiple key presses, the self.score that i increment by 1 flies to
> about 40 from 1 key press. as a result my counter i use to index the
> list is way out of range. i tried to add a time.sleep(.03) as soon as
> a key is pressed, so it would register once. it managed to limit it to
> 4 presses, so self.score would equal 4 after 1 press, 8 after 2.. in
> that i started my counter at -4. then the code self.counter /= 4 would
> give me 0. after the next 4 presses, my counter would be at 4, the
> same code self.counter /= 4 would give me 1. after the next 4 presses,
> my counter would be at 8, the same code self.counter /= 4 would give
> me 2. this successfully fixed my indexing issue, but it needs A LOT of
> if statements and it is getting to confusing. isnt there a way to make
> 1 key press mean 1? and not 40? other wise how can i keep track of the
> score. any help? do i quit this exercise and blame it on the program
> just not following my instructions?

It would appear you're running some form of event loop, such as
wxpython, tkinter, pygame, or whatever.  If so, you have to do your
delays using the mechanism that system defines, not using sleep().  As
you've discovered, sleep() stops the whole thread, and that's not what
you want.  What you want is to set an event to fire at some measured
time in the future, so the actual waiting is done inside the event loop.



More information about the Tutor mailing list