ulvis.paste.net

Paste Search Dynamic
Recent pastes
pygame
  1. import pygame
  2. from pygame.locals import *
  3. import random
  4. import os
  5. #import time
  6.  
  7. # set up asset folders
  8. game_folder = os.path.dirname(__file__)
  9. img_folder = os.path.join(game_folder, 'img')
  10.  
  11. WIDTH = 480
  12. HEIGHT = 600
  13. FPS = 60
  14.  
  15. # define colors
  16. WHITE = (255, 255, 255)
  17. BLACK = (0, 0, 0)
  18. RED = (255, 0, 0)
  19. GREEN = (0, 255, 0)
  20. BLUE = (0, 0, 255)
  21. YELLOW = (255, 255, 0)
  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("Shmup!")
  28. clock = pygame.time.Clock()
  29.  
  30. player_img = pygame.image.load(os.path.join(img_folder, 'pig2.png')).convert_alpha()
  31. screen_img = pygame.image.load(os.path.join(img_folder, 'ciel.jpg')).convert ()
  32. ennemy_img = pygame.image.load(os.path.join(img_folder, 'p3_front.png')).convert_alpha()
  33. iss_img = pygame.image.load(os.path.join(img_folder, 'ISS.png')).convert_alpha()
  34. star_img = pygame.image.load(os.path.join(img_folder, 'star.png')).convert_alpha()
  35.  
  36. right_event = USEREVENT
  37. pygame.time.set_timer(right_event, 10000)
  38.  
  39. class Star(pygame.sprite.Sprite):
  40.     def __init__(self):
  41.         pygame.sprite.Sprite.__init__(self)
  42.         self.image = star_img
  43.         self.rect = self.image.get_rect()
  44.         self.rect.center = (0, HEIGHT / 2)
  45.  
  46.     def update(self):
  47.         self.rect.x += 1
  48.         if self.rect.left > WIDTH:
  49.             self.rect.right = 0
  50.             """
  51.       if self.rect.left < WIDTH / 2:
  52.           self.rect.y -= 1
  53.       if self.rect.left > WIDTH / 2:
  54.           self.rect.y += 1"""
  55.  
  56.  
  57.        
  58.        
  59.  
  60. class Iss(pygame.sprite.Sprite):
  61.     def __init__(self):
  62.         pygame.sprite.Sprite.__init__(self)
  63.         self.image = iss_img
  64.         self.rect = self.image.get_rect()
  65.         self.rect.center = (WIDTH / 2, HEIGHT / 2)
  66.  
  67.     def update(self):
  68.         self.rect.x += 2
  69.         if self.rect.left > WIDTH:
  70.             self.rect.right = -1000
  71.  
  72.  
  73.            
  74. class Player(pygame.sprite.Sprite):
  75.     def __init__(self):
  76.         pygame.sprite.Sprite.__init__(self)
  77.         self.image = player_img
  78.         self.rect = self.image.get_rect()
  79.         self.rect.centerx = WIDTH / 2
  80.         self.rect.bottom = HEIGHT - 10
  81.         self.speedx = 0
  82.         self.speedy = 0
  83.  
  84.     def update(self):
  85.         self.speedx = 0
  86.         self.speedy = 0
  87.         keystate = pygame.key.get_pressed()
  88.         if keystate[pygame.K_LEFT]:
  89.             self.speedx = -8
  90.         if keystate[pygame.K_RIGHT]:
  91.             self.speedx = 8
  92.         if keystate[pygame.K_UP]:
  93.             self.speedy = -8
  94.         if keystate[pygame.K_DOWN]:
  95.             self.speedy = 8
  96.         self.rect.x += self.speedx
  97.         self.rect.y += self.speedy
  98.         if self.rect.right > WIDTH:
  99.             self.rect.right = WIDTH
  100.         if self.rect.left < 0:
  101.             self.rect.left = 0
  102.         if self.rect.bottom > HEIGHT:
  103.             self.rect.bottom = HEIGHT
  104.         if self.rect.top < 0:
  105.             self.rect.top = 0
  106.  
  107. class Mob(pygame.sprite.Sprite):
  108.     def __init__(self):
  109.         pygame.sprite.Sprite.__init__(self)
  110.         self.image = ennemy_img
  111.         self.rect = self.image.get_rect()
  112.         self.rect.x = random.randrange(WIDTH - self.rect.width)
  113.         self.rect.y = random.randrange(-100, -40)
  114.         self.speedy = random.randrange(1, 4)
  115.         self.speedx = random.randrange(-3, 3)
  116.  
  117.     def update(self):
  118.         self.rect.x += self.speedx
  119.         self.rect.y += self.speedy
  120.         if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 20:
  121.             self.rect.x = random.randrange(WIDTH - self.rect.width)
  122.             self.rect.y = random.randrange(-100, -40)
  123.             self.speedy = random.randrange(1, 4)
  124.  
  125. all_sprites = pygame.sprite.Group()
  126. mobs = pygame.sprite.Group()
  127. spaceship = pygame.sprite.Group()
  128. iss = Iss ()
  129. player = Player()
  130. star = Star()
  131. spaceship.add(star)
  132. spaceship.add(iss)
  133. all_sprites.add(player)
  134. for i in range(8):
  135.     m = Mob()
  136.     all_sprites.add(m)
  137.     mobs.add(m)
  138.  
  139. # Game loop
  140. running = true
  141. while running:
  142.     # keep loop running at the right speed
  143.     clock.tick(FPS)
  144.     # Process input (events)
  145.     for event in pygame.event.get():
  146.         # check for closing window
  147.         if event.type == pygame.QUIT:
  148.             running = false
  149.         elif event.type == right_event :
  150.             print(right_event)
  151.  
  152.            
  153.            
  154.  
  155.  
  156.     # Update
  157.     all_sprites.update()
  158.     spaceship.update()
  159.     mobs.update()
  160.    
  161.  
  162.  
  163.  
  164.     # Draw / render
  165.     screen.blit(screen_img,(0,0))
  166.     all_sprites.draw(screen)
  167.     spaceship.draw(screen)
  168.     mobs.draw(screen)
  169.     # *after* drawing everything, flip the display
  170.     pygame.display.flip()
  171.  
  172. pygame.quit()
Parsed in 0.035 seconds