SHARE
TWEET

avancée 22/11 23:59

a guest Nov 22nd, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Pygame template - skeleton for a new pygame project
  2. import pygame
  3. import random
  4.  
  5. WIDTH = 800
  6. HEIGHT = 800
  7. FPS = 30
  8. TIMER = 0
  9. Xstep = 8
  10. Ystep = 7
  11.  
  12. # define colors
  13. WHITE = (255, 255, 255)
  14. BLACK = (0, 0, 0)
  15. RED = (255, 0, 0)
  16. GREEN = (0, 255, 0)
  17. BLUE = (0, 0, 255)
  18.  
  19. def RandomColor():
  20.     tuple01 = (WHITE,RED,GREEN,BLUE)
  21.     return(tuple01[random.randint(0,3)])
  22.  
  23. # initialize pygame and create window
  24. pygame.init()
  25. pygame.mixer.init()
  26. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  27. pygame.display.set_caption("My Game")
  28. clock = pygame.time.Clock()
  29.  
  30. all_sprites = pygame.sprite.Group()
  31. class Player(pygame.sprite.Sprite):
  32.     def __init__(self):
  33.         pygame.sprite.Sprite.__init__(self)
  34.         self.image = pygame.Surface((40, 40))
  35.         self.image.fill(GREEN)
  36.         self.rect = self.image.get_rect()
  37.         self.rect.center = (WIDTH / 2, HEIGHT / 2)
  38.        
  39.     def update(self):
  40.         global Xstep
  41.         global Ystep
  42.         global TIMER
  43.  
  44.         if TIMER == 20:
  45.             TIMER = 0
  46.         else:
  47.             TIMER += 1
  48.  
  49.         #movements    
  50.         self.rect.x += Xstep
  51.         self.rect.y += Ystep
  52.         if self.rect.right > WIDTH:
  53.             Xstep =- Xstep
  54.         if self.rect.left <= 0:
  55.             Xstep =- Xstep
  56.         if self.rect.top <= 0:
  57.             Ystep =- Ystep
  58.         if self.rect.bottom > HEIGHT:
  59.             Ystep =- Ystep
  60.         if TIMER == 10:    
  61.             self.image.fill(RandomColor())
  62.             print(TIMER)
  63.  
  64.            
  65. greensqr = Player()
  66. all_sprites.add(greensqr)
  67.  
  68. # Game loop
  69. running = True
  70. while running:
  71.     # keep loop running at the right speed
  72.     clock.tick(FPS)
  73.     # Process input (events)
  74.     for event in pygame.event.get():
  75.         # check for closing window
  76.         if event.type == pygame.QUIT:
  77.             running = False
  78.  
  79.     # Update
  80.     all_sprites.update()
  81.  
  82.     # Draw / render
  83.     screen.fill(BLACK)
  84.     all_sprites.draw(screen)
  85.     # *after* drawing everything, flip the display
  86.     pygame.display.flip()
  87.  
  88. pygame.quit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top