Guest

expelledboy

By: a guest on Jun 7th, 2009  |  syntax: Python  |  size: 2.44 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. # written by Anthony Jackson (expelledboy)
  2. # published on http://expelledboy.wordpress.com/
  3.  
  4. import pygame
  5. import pygame.locals as pg
  6. from pygame import Rect
  7.  
  8. # screen size
  9. x, y = size = 400, 400
  10.  
  11. # colours we will be using
  12. white = (0, 0, 0)
  13. black = (255, 255, 255)
  14. grey = (200, 200, 200)
  15. green = (0, 255, 0)
  16.  
  17. # setup the screen
  18. pygame.init()
  19. screen = pygame.display.set_mode(size)
  20. screen.fill(grey)
  21. pygame.display.update()
  22.  
  23. # ball atributes
  24. ball = Rect(x/2, y/2, 20, 20)
  25. vel = [0.0, 0.0]
  26. elasticity = 0.6
  27. power = 0.6
  28. gravity = 0.5
  29.  
  30. # main loop
  31. running = True
  32. go_u = go_d = go_r = go_l = False
  33. clock = pygame.time.Clock()
  34.  
  35. while running:
  36.    
  37.     # limit the fps
  38.     clock.tick(25)
  39.    
  40.     # check for relavent key events
  41.     for event in pygame.event.get():
  42.         if event.type == pygame.QUIT: running = False
  43.        
  44.         elif event.type == pg.KEYDOWN:
  45.             if event.key == pg.K_ESCAPE: running = False
  46.             elif event.key == pg.K_UP: go_u = True
  47.             elif event.key == pg.K_DOWN: go_d = True
  48.             elif event.key == pg.K_RIGHT: go_r = True
  49.             elif event.key == pg.K_LEFT: go_l = True
  50.        
  51.         elif event.type == pg.KEYUP:
  52.             if event.key == pg.K_UP: go_u = False
  53.             elif event.key == pg.K_DOWN: go_d = False
  54.             elif event.key == pg.K_RIGHT: go_r = False
  55.             elif event.key == pg.K_LEFT: go_l = False
  56.    
  57.     # erase and store the previous ball position
  58.     screen.fill(grey, ball)
  59.     old_pos = ball.move(0, 0)
  60.    
  61.     # ajust velocities
  62.     vel[1] += gravity
  63.     if go_u: vel[1] -= power
  64.     if go_d: vel[1] += power
  65.     if go_r: vel[0] += power
  66.     if go_l: vel[0] -= power
  67.    
  68.     # move the ball according to its velocity
  69.     ball.move_ip(vel)
  70.    
  71.     # stop the ball going off the screen
  72.     if ball.bottom > y:
  73.         ball.bottom = y
  74.         vel[1] = -abs(vel[1]) * elasticity
  75.     if ball.top < 0:
  76.         ball.top = 0
  77.         vel[1] = abs(vel[1]) * elasticity
  78.     if ball.right > x:
  79.         ball.right = x
  80.         vel[0] = -abs(vel[0]) * elasticity
  81.     if ball.left < 0:
  82.         ball.left = 0
  83.         vel[0] = abs(vel[0]) * elasticity
  84.    
  85.     # draw the new ball position and update the screen
  86.     pygame.draw.ellipse(screen, green, ball)
  87.     pygame.display.update(ball.union(old_pos))
  88.  
  89. # this is a game right, then we /need/ a quit message...
  90. print 'My game not good enough for you aye.. then piss off! XD'