[Tutor] How to create delay in game

Nicholas Perito nperito at gmail.com
Wed Nov 22 02:03:07 CET 2006


Well, that was easy! Thanks for the tip and the links--a lot of good info
there.

Cheers,

Nic

On 11/21/06, Michael Brunton-Spall <mib at mibgames.co.uk> wrote:
>
> Of all the weird things, I'm currently working for Scripture Union, i
> never
> knew they had made a python package for learning python, anyway...
>
> add
> import time
>
> where you want to delay for a while,
> time.sleep(0.1)
> that will sleep for 0.1 seconds, (or try to).
>
> the livewires package is a wrapper around pygame, it might be worth
> looking at
> pygame.org to see the more powerful, underlying package.
>
> The livewires packages have worksheets to go with the packages, teaching
> games
> programing.  Reference sheet T(Time) had this information in it. might be
> worth checking those out if your sticking with livewires.  find them at
> http://www.livewires.org.uk/python/pdfsheets.html
>
> Michael Brunton-Spall
>
> On Tuesday 21 November 2006 03:02, Nicholas Perito wrote:
> > Hello,
> >
> > I new to programming and this is my first post. I'm learning Python via
> > "Python Programming for the Absolute Beginner" by Michael Dawson. I've
> > written a pong game  into which I want to insert a delay between sprite
> > initializations. Right now, if an "Atom" sprite reaches the bottom of
> > the screen, the next Atom sprite appears immediately. Instead, when the
> > sprite reaches the bottom, I want the message "Two (or One) atoms left!"
> > to appear for it's lifetime before the next Atom sprite appears. I've
> > tried to create a delay by making it crunch numbers, but that hasn't
> > worked well.
> >
> > Anyway, the code is below, and the place where I want to create a delay
> > is marked. Oh, and constructive criticism of the code is welcome--I'm
> > not too sensitive.
> >
> > Thanks a lot to anyone who can help!
> >
> > (It's called 'Atom Pong' because I had a cool graphic)
> >
> > # Atom Pong
> > # Nic Perito 11/06
> >
> > import random
> > from livewires import games, color
> >
> > SCREEN_WIDTH = 1000
> > SCREEN_HEIGHT = 700
> > THE_SCREEN = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT)
> > life = 3
> >
> > class Atom(games.Sprite):
> >     """ Atom image """
> >
> >     image = games.load_image("atom.jpg")
> >     def __init__(self, screen, x, y, dx, dy):
> >         """ Initialize atom object. """
> >         self.init_sprite(screen = screen, x = x, y = y,
> >                              dx = dx, dy = dy, image = Atom.image)
> >
> >     def moved(self):
> >         """ Reverse a velocity component if edge of screen reached. """
> >         global life
> >         dx, dy = self.get_velocity()
> >         self.rotate_by(3)
> >         if self.get_right() > SCREEN_WIDTH:
> >             self.set_right(SCREEN_WIDTH)
> >             self.set_velocity(-dx, dy)
> >         if self.get_left() < 0:
> >             self.set_left(0)
> >             self.set_velocity(-dx, dy)
> >         if self.get_top() < 0:
> >             self.set_velocity(dx, -dy)
> >         if self.get_bottom() > SCREEN_HEIGHT:
> >             self.destroy()
> >             life -= 1
> >             if life == 0:
> >                 self.game_over()
> >
> >             if life == 2:
> >                 games.Message(screen = self.screen,
> >                       x = 150, y = 20,
> >                       text = "Two atoms left!", size = 40, color =
> > color.yellow,
> >                       lifetime = 200)
> >                 #HERE'S WHERE I WANT A DELAY
> >                 Atom(self.screen, x = (SCREEN_WIDTH - 76), y = 76,
> >                         dx = 6, dy = 5)
> >
> >             if life == 1:
> >                 games.Message(screen = self.screen,
> >                       x = 150, y = 45,
> >                       text = "One atom left!", size = 40, color =
> > color.yellow,
> >                       lifetime = 200)
> >                 #HERE'S WHERE I WANT A DELAY
> >                 Atom(self.screen, x = 76, y = 76,
> >                         dx = 6, dy = 6)
> >
> >     def handle_deflect(self):
> >         """ Deflect atom. """
> >         dx, dy = self.get_velocity()
> >         self.set_velocity(dx + random.randrange(2,4), -dy)
> >
> >     def game_over(self):
> >         """ End the game. """
> >         # destroy all game objects except the Text object (player's
> score)
> >         for game_object in self.screen.all_objects():
> >             if not isinstance(game_object, games.Text):
> >                 game_object.destroy()
> >
> >         # show 'Game Over' for 250 mainloop() cycles
> >         games.Message(screen = self.screen,
> >                       x = SCREEN_WIDTH/2, y = 400,
> >                       text = "Game Over", size = 90, color = color.red,
> >                       lifetime = 500, after_death = self.screen.quit)
> >
> > class Bonus(games.Sprite):
> >     """ Atom bonus image """
> >     image = games.load_image("bonus.jpg")
> >     def __init__(self, screen, x, y, dx, dy):
> >         """ Initialize atom object. """
> >         self.init_sprite(screen = screen, x = x, y = y,
> >                              dx = dx, dy = dy, image = Bonus.image)
> >
> >     def moved(self):
> >         """ Reverse a velocity component if edge of screen reached. """
> >         dx, dy = self.get_velocity()
> >         self.rotate_by(-3)
> >         if self.get_right() > SCREEN_WIDTH or self.get_left() < 0:
> >             self.set_velocity(-dx, dy)
> >         if self.get_top() < 0:
> >             self.set_velocity(dx, -dy)
> >         if self.get_bottom() > SCREEN_HEIGHT:
> >             self.destroy()
> >     def handle_deflect(self):
> >         """ Deflect atom. """
> >         dx, dy = self.get_velocity()
> >         self.set_velocity(dx, -dy)
> >
> >
> > class Paddle(games.Sprite):
> >     """
> >     A paddle controlled by player to 'deflect' atoms.
> >     """
> >     image = games.load_image("paddle.jpg", transparent = False)
> >
> >     def __init__ (self, screen, x, y):
> >         """ Initialize paddle object. Create a Text object for player's
> > score. """
> >         self.init_sprite(screen = screen, x = x, y = y, image =
> > Paddle.image)
> >         self.score_value = 0
> >         self.score_text = games.Text(screen = self.screen, x =
> > SCREEN_WIDTH/2, y = 20,
> >                                      text = "Score: 0", size = 25, color
> > = color.white)
> >
> >     def moved(self):
> >         """ Move paddle to mouse x position. """
> >         x, y = self.get_pos()
> >
> >         if self.screen.is_pressed(games.K_RIGHT):
> >             x += 9
> >
> >         if self.screen.is_pressed(games.K_LEFT):
> >             x -= 9
> >
> >         self.move_to(x,y)
> >
> >
> >         if self.get_left() < 0:
> >             self.set_left(0)
> >         if self.get_right() > SCREEN_WIDTH:
> >             self.set_right(SCREEN_WIDTH)
> >         self.check_for_impact()
> >
> >     def check_for_impact(self):
> >         """ Check if paddle hits atoms. """
> >         for atom in self.overlapping_objects():
> >             self.handle_deflect()
> >             atom.handle_deflect()
> >
> >     def handle_deflect(self):
> >         """ Increase and display score. """
> >
> >         bonus = []
> >         for i in range(0, 5000, 50):
> >             bonus.append(i)
> >         self.score_value += 10
> >         self.score_text.set_text("Score: " + str(self.score_value))
> >         if self.score_value in bonus:
> >             Bonus(self.screen, x = random.randrange(800)+ 76, y = 76,
> >                   dx = random.randrange(10)+2, dy = random.randrange(3)+
> 2)
> >
> >             games.Message(screen = self.screen,
> >                       x = SCREEN_WIDTH/2, y = 300,
> >                       text = "BONUS!", size = 90, color = color.red,
> >                       lifetime = 50)
> >
> >
> >
> >
> >
> >
> >
> > def main():
> >     my_screen = THE_SCREEN
> >     my_screen.mouse_visible(False)
> >     background_image = games.load_image("black.jpg", transparent =
> False)
> >     my_screen.set_background(background_image)
> >
> >
> >     Atom(screen = my_screen, x = 76, y = 76,
> >           dx = 6, dy = 5)
> >
> >
> >     Paddle(screen = my_screen, x = SCREEN_WIDTH/2, y = 690)
> >
> >
> >     my_screen.mainloop()
> >
> > # start program
> > main()
> >
> >
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061122/3babdea9/attachment.htm 


More information about the Tutor mailing list