Guest User

Pygame Tappy Plane Game

a guest
Apr 17th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.60 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. # Define some colors
  5. black    = (   0,   0,   0)
  6. white    = ( 255, 255, 255)
  7. red      = ( 255,   0,   0)
  8. background_image = pygame.image.load("background.png")
  9. player_y = 20
  10. y_speed = 0
  11. obj_speed = 0.5
  12. sky = 0
  13. player_x = 50
  14.  
  15. skyChange = False
  16. # This class represents the ball        
  17. # It derives from the "Sprite" class in Pygame
  18. class Block(pygame.sprite.Sprite):
  19.      
  20.     # Constructor. Pass in the color of the block,
  21.     # and its x and y position
  22.     def __init__(self, width, height):
  23.         # Call the parent class (Sprite) constructor
  24.         pygame.sprite.Sprite.__init__(self)
  25.  
  26.         # Create an image of the block, and fill it with a color.
  27.         # This could also be an image loaded from the disk.
  28.         self.image = pygame.image.load("rock.png")
  29.  
  30.         # Fetch the rectangle object that has the dimensions of the image
  31.         # image.
  32.         # Update the position of this object by setting the values
  33.         # of rect.x and rect.y
  34.         self.rect = self.image.get_rect()
  35.         self.rect.x -= 0.5
  36.        
  37. class Player(pygame.sprite.Sprite):
  38.     def __init__(self, width, height):
  39.         pygame.sprite.Sprite.__init__(self)
  40.         self.image = pygame.image.load("player.png")
  41.         self.rect=self.image.get_rect()
  42. class Wall(pygame.sprite.Sprite):
  43.     def __init__(self, width, height):
  44.         pygame.sprite.Sprite.__init__(self)
  45.         self.image = pygame.image.load("Wall_of_Flesh.png")
  46.         self.rect=self.image.get_rect()
  47.        
  48. # Initialize Pygame
  49. pygame.init()
  50.  
  51. # Set the height and width of the screen
  52. screen_width=800
  53. screen_height=400
  54. screen=pygame.display.set_mode([screen_width,screen_height])
  55. pygame.display.set_icon(pygame.image.load("gameicon.png"))
  56.  
  57.  
  58. # This is a list of 'sprites.' Each block in the program is
  59. # added to this list. The list is managed by a class called 'Group.'
  60. block_list = pygame.sprite.Group()
  61. player_list = pygame.sprite.Group()
  62. wof_list = pygame.sprite.Group()
  63. # This is a list of every sprite. All blocks and the player block as well.
  64. all_sprites_list = pygame.sprite.Group()
  65.  
  66. for i in range(10):
  67.     # This represents a block
  68.     block = Block(20, 15)
  69.  
  70.     # Set a random location for the block
  71.     block.rect.x = random.randrange(400,2000)
  72.     block.rect.y = random.randrange(161,180)
  73.      
  74.     # Add the block to the list of objects
  75.     block_list.add(block)
  76.     all_sprites_list.add(block)
  77.      
  78.      
  79.  
  80. # Create a red player block
  81. player = Player(300, 300)
  82. all_sprites_list.add(player)
  83. player_list.add(player)
  84.  
  85. wof = Wall(144,400)
  86. all_sprites_list.add(wof)
  87. wof_list.add(player)
  88.  
  89. #Loop until the user clicks the close button.
  90. done = False
  91.  
  92. # Used to manage how fast the screen updates
  93. clock = pygame.time.Clock()
  94.  
  95. score = 0
  96.  
  97. # -------- Main Program Loop -----------
  98. while done==False:
  99.     for event in pygame.event.get(): # User did something
  100.         if event.type == pygame.QUIT: # If user clicked close
  101.             done = True # Flag that we are done so we exit this loop
  102.         if event.type == pygame.KEYDOWN: #user press down a key
  103.             if event.key == pygame.K_UP:
  104.                 y_speed -= 3
  105.             if event.key == pygame.K_DOWN:
  106.                 y_speed += 3
  107.         if event.type == pygame.KEYUP:
  108.             if event.key == pygame.K_UP:
  109.                 y_speed += 3
  110.             if event.key == pygame.K_DOWN:
  111.                 y_speed -= 3
  112.  
  113.     # Clear the screen
  114.  
  115.     player.rect.y = player_y
  116.     player_y += y_speed
  117.     player.rect.x = player_x
  118.  
  119.     wof.rect.x = (player_x - 400)
  120.     wof.rect.y = (player_y - 450)
  121.    
  122.     # See if the player block has collided with anything.
  123.     player_hit_list = pygame.sprite.spritecollide(block, player_list, True)
  124.     player_hit_list = pygame.sprite.spritecollide(wof, player_list, True)
  125.     block_hit_list = pygame.sprite.spritecollide(wof, block_list, True)
  126.     if player in player_hit_list:
  127.         print("uded")
  128.     # Check the list of collisions.
  129.  
  130.  
  131.    
  132.     # Draw all the spites
  133.     while sky == 0:
  134.         screen.blit(pygame.image.load("background.png"), [0,0])
  135.         all_sprites_list.draw(screen)
  136.         if player_y < 10:
  137.             sky = 1
  138.     while sky == 1:
  139.         player_y = 390
  140.         screen.blit(pygame.image.load("sky.png"), [0,0])
  141.         player_list.draw(screen)
  142.         wof_list.draw(screen)
  143.  
  144.     for block in block_list:
  145.         block.rect.x -= 0.1
  146.     player_x += 1
  147.  
  148.      
  149.  
  150.        
  151.     # Limit to 20 frames per second
  152.     clock.tick(60)
  153.  
  154.     # Go ahead and update the screen with what we've drawn.
  155.     pygame.display.flip()
  156.  
  157. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment