Advertisement
Guest User

Pygame fix

a guest
Mar 15th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import sys
  2. import pygame as pg
  3.  
  4.  
  5. pg.init()
  6. DISPLAYSURF = pg.display.set_mode((400,300))
  7. FPS = 30
  8. fpsClock = pg.time.Clock()
  9. pg.display.set_caption('Bouncing Cat')
  10.  
  11. WHITE = (255,255,255)
  12. GREEN = (0,255,0)
  13. BLUE = (0,0,255)
  14. RED = (255,0,0)
  15. BLACK = (0,0,0)
  16.  
  17. ##cat = pg.image.load('cat.png')
  18. cat = pg.Surface((50,50)).convert()
  19. cat.fill(RED)
  20. cat_rect = cat.get_rect(topleft=(200,150))
  21. speed = [5,5]
  22.  
  23. while True:
  24.     DISPLAYSURF.fill(WHITE)
  25.     for event in pg.event.get():
  26.         if event.type == pg.QUIT:
  27.             pg.quit()
  28.             sys.exit()
  29.         elif event.type == pg.KEYDOWN:
  30.             if event.key == pg.K_ESCAPE:
  31.                 pg.quit()
  32.                 sys.exit()
  33.  
  34.     if cat_rect.right >= 400 or cat_rect.left <= 0:
  35.         speed[0] *= -1
  36.     if cat_rect.bottom >= 300 or cat_rect.top <= 0:
  37.         speed[1] *= -1
  38.  
  39.     cat_rect.x += speed[0]
  40.     cat_rect.y += speed[1]
  41.  
  42.     DISPLAYSURF.blit(cat, cat_rect)
  43.     pg.display.update()
  44.     fpsClock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement