Advertisement
The_Odd_Sheep

Stickman Skirmish V2

May 24th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.12 KB | None | 0 0
  1.  #!/usr/bin/python
  2. '''Still missing the best bit. Yes thats the bullets. but don't worry they're on their way'''
  3. '''Get the full project ZIP here https://www.dropbox.com/s/xvg5tv3lc2txuox/Stickman%20Skirmish%20V2.0.zip?dl=0'''
  4. '''Have fun!'''
  5.  
  6. import pygame
  7. from random import *
  8.  
  9. # -- Global constants
  10.  
  11. wall_list = []
  12. platform_hit_list = []
  13.  
  14. # Colors
  15. BLACK    = (   0,   0,   0)
  16. WHITE    = ( 255, 255, 255)
  17. BLUE     = (   0,   0, 255)
  18. GREY     = (  96,  96,  96)
  19.  
  20. # Screen dimensions
  21. SCREEN_WIDTH  = 1200
  22. SCREEN_HEIGHT = 800
  23.  
  24.  
  25.  
  26.  
  27. # This class represents the bar at the bottom that the player controls
  28. class Player(pygame.sprite.Sprite):
  29.     """ This class represents the bar at the bottom that the player controls. """
  30.  
  31.     # Constructor function
  32.     def __init__(self, x, y, image_init, nbreDeVie, nomDuPerso):
  33.         # Call the parent's constructor
  34.         super().__init__()
  35.  
  36.         global done
  37.         global player_image
  38.         global player2_image
  39.  
  40.         self.vie = nbreDeVie
  41.         self.nom = nomDuPerso
  42.  
  43.         # Set player
  44.         self.image = pygame.image.load(image_init)
  45.  
  46.         # Make our top-left corner the passed-in location.
  47.         self.rect = self.image.get_rect()
  48.         self.rect.y = y
  49.         self.rect.x = x
  50.  
  51.         # Set speed vector
  52.         self.change_x = 0
  53.         self.change_y = 0
  54.         self.blockcount = 0
  55.         self.jumpcount = 0
  56.         self.walls = None
  57.            
  58.  
  59.        
  60.     def subit_attaque(self, attaquant):
  61.  
  62.         taux_reussite = random()
  63.  
  64.         if taux_reussite > 0.2:
  65.             h=random()
  66.             if h<0.2:
  67.                 self.vie -= 2
  68.             else:
  69.                 self.vie -= 1
  70.        
  71.    
  72.  
  73.     def changespeed(self, x, y):
  74.         """ Change the speed of the player. """
  75.         self.change_x += x
  76.         self.change_y += y
  77.  
  78.     def update(self):
  79.         """ Update the player position. """
  80.        
  81.         self.blockcount=0
  82.        
  83.         # Move left/right
  84.         self.rect.x += self.change_x
  85.  
  86.         # Did this update cause us to hit a wall?
  87.         block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
  88.  
  89.         for block in block_hit_list:
  90.             # If we are moving right, set our right side to the left side of the item we hit*
  91.            
  92.             if self.change_x > 0:
  93.                 self.rect.right = block.rect.left
  94.                
  95.             else:
  96.                 # Otherwise if we are moving left, do the opposite.
  97.                 self.rect.left = block.rect.right
  98.  
  99.         # Move up/down
  100.         self.rect.y += self.change_y
  101.  
  102.         # Check and see if we hit anything
  103.        
  104.              
  105.         block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
  106.  
  107.         for block in block_hit_list:
  108.             # Reset our position based on the top/bottom of the object.
  109.             if self.change_y >= 0:
  110.                 self.rect.bottom = block.rect.top
  111.                 self.blockcount=1
  112.                
  113.             else:
  114.                 self.rect.top = block.rect.bottom
  115.                
  116.  
  117.    
  118.         """ Calculate effect of gravity. """
  119.         if self.change_y == 0:
  120.             self.change_y = 1
  121.         else:
  122.             self.change_y += .20
  123.  
  124.         # See if we are on the ground.
  125.         if self.rect.y >= SCREEN_HEIGHT: #+ (self.rect.height/4): "and self.change_y >= 0"
  126.             #self.change_y = 0
  127.             self.rect.y = SCREEN_HEIGHT #+ (self.rect.height/4)
  128.  
  129.     def jump(self):
  130.         """ Appeler quand le joueur saute """
  131.         if self.blockcount == 1:
  132.             self.jumpcount = 0
  133.         if self.jumpcount < 3:
  134.             self.jumpcount += 1
  135.             self.change_y = -5
  136.  
  137.     def image_perso(self, chemin_image):
  138.        
  139.         global player_image
  140.         global player2_image
  141.         self.image = pygame.image.load(chemin_image)
  142.  
  143. class Bullet(pygame.sprite.Sprite):
  144.    
  145.     def __init__(self,x,y):
  146.         super().__init__()
  147.        
  148.         self.rect
  149.  
  150. class Wall(pygame.sprite.Sprite):
  151.     """ Wall the player can run into. """
  152.     def __init__(self, x, y, width, height):
  153.         """ Constructor for the wall that the player can run into. """
  154.         # Call the parent's constructor
  155.         super().__init__()
  156.  
  157.         # Make a grey wall, of the size specified in the parameters
  158.         self.image = pygame.Surface([width, height])
  159.         self.image.fill(GREY)
  160.  
  161.         # Make our top-left corner the passed-in location.
  162.         self.rect = self.image.get_rect()
  163.         self.rect.y = y
  164.         self.rect.x = x
  165.    
  166.  
  167.  
  168. def main():
  169.    
  170.     # Call this function so the Pygame library can initialize itself
  171.     pygame.init()
  172.  
  173.     # Create an 800x600 sized screen
  174.     screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
  175.  
  176.     # Set the title of the window
  177.     pygame.display.set_caption('Test')
  178.  
  179.     # List to hold all the sprites
  180.     all_sprite_list = pygame.sprite.Group()
  181.  
  182.     # Make the walls. (x_pos, y_pos, width, height)
  183.     wall_list = pygame.sprite.Group()
  184.     all_sprite_list.add(wall_list)
  185.  
  186.     wall = Wall(150, 636, 379, 67)
  187.     wall_list.add(wall)
  188.     all_sprite_list.add(wall)
  189.  
  190.     wall = Wall(855, 636, 379, 67)
  191.     wall_list.add(wall)
  192.     all_sprite_list.add(wall)
  193.  
  194.     wall = Wall(216, 475, 90, 47)
  195.     wall_list.add(wall)
  196.     all_sprite_list.add(wall)
  197.  
  198.     wall = Wall(1048, 475, 90, 47)
  199.     wall_list.add(wall)
  200.     all_sprite_list.add(wall)
  201.  
  202.     wall = Wall(566, 443, 245, 11)
  203.     wall_list.add(wall)
  204.     all_sprite_list.add(wall)
  205.  
  206.     wall = Wall(215, 263, 450, 46)
  207.     wall_list.add(wall)
  208.     all_sprite_list.add(wall)
  209.  
  210.     wall = Wall(0, 126, 122, 46)
  211.     wall_list.add(wall)
  212.     all_sprite_list.add(wall)
  213.  
  214.     wall = Wall(358, 86, 200, 46)
  215.     wall_list.add(wall)
  216.     all_sprite_list.add(wall)    
  217.  
  218.     wall = Wall(826, 133, 481, 47)
  219.     wall_list.add(wall)
  220.     all_sprite_list.add(wall)
  221.  
  222.     done = False
  223.    
  224.     # Create the player paddle object
  225.     player = Player(20, -50, "player/Player-rouge-droite.png", 20, "Player_rouge")
  226.     player.walls = wall_list
  227.     all_sprite_list.add(player)
  228.  
  229.     player2 = Player(1000, 0, "player/Player-bleu-gauche.png", 20, "Player_bleu")
  230.     player2.walls = wall_list
  231.     all_sprite_list.add(player2)
  232.  
  233.    
  234.     clock = pygame.time.Clock()
  235.  
  236.        
  237.    
  238.     while not done:
  239.  
  240.         for event in pygame.event.get():
  241.             if event.type == pygame.QUIT:
  242.                 done = True
  243.                
  244.             elif player.vie <= 0:
  245.                 print("Player rouge est mort! Victoire joueur bleu!")
  246.                 done = True
  247.             elif player2.vie <= 0:
  248.                 print("Player bleu est mort! Victoire joueur rouge!")
  249.                 done = True
  250.                
  251.             elif event.type == pygame.KEYDOWN:
  252.                
  253.                 if event.key == pygame.K_ESCAPE:
  254.                     done = True
  255.                
  256.                 if event.key == pygame.K_r: #Un reset des positions des personnages
  257.                     player.rect.x = 20
  258.                     player.rect.y = -50
  259.                     player2.rect.x = 1000
  260.                     player2.rect.y = 0
  261.                    
  262.                    
  263.                 if event.key == pygame.K_LEFT:
  264.                     player.changespeed(-5, 0)
  265.                     player_image = 'player/Player-rouge-gauche.png'
  266.                     player.image_perso(player_image)
  267.                 elif event.key == pygame.K_RIGHT:
  268.                     player.changespeed(5, 0)
  269.                     player_image = 'player/Player-rouge-droite.png'
  270.                     player.image_perso(player_image)
  271.                 elif event.key == pygame.K_UP:
  272.                     player.jump()
  273.                 elif event.key == pygame.K_RCTRL:
  274.                     player.subit_attaque(player2)
  275.                    
  276.  
  277.                 elif event.key == pygame.K_a:
  278.                     player2.changespeed(-5, 0)
  279.                     player2_image = 'player/Player-bleu-gauche.png'
  280.                     player2.image_perso(player2_image)
  281.                 elif event.key == pygame.K_d:
  282.                     player2.changespeed(5, 0)
  283.                     player2_image = 'player/Player-bleu-droite.png'
  284.                     player2.image_perso(player2_image)
  285.                 elif event.key == pygame.K_w:
  286.                     player2.jump()
  287.                 elif event.key == pygame.K_SPACE:
  288.                     player.subit_attaque(player)
  289.                
  290.  
  291.  
  292.             elif event.type == pygame.KEYUP:
  293.                 if event.key == pygame.K_LEFT:
  294.                     player.changespeed(5, 0)
  295.                 elif event.key == pygame.K_RIGHT:
  296.                     player.changespeed(-5, 0)
  297.  
  298.                
  299.                 if event.key == pygame.K_a:
  300.                     player2.changespeed(5, 0)
  301.                 elif event.key == pygame.K_d:
  302.                     player2.changespeed(-5, 0)
  303.            
  304.        
  305.         all_sprite_list.update()
  306.  
  307.         screen.fill(BLACK)
  308.  
  309.         all_sprite_list.draw(screen)
  310.  
  311.         pygame.display.flip()
  312.  
  313.         clock.tick(60)
  314.  
  315.     pygame.quit()
  316.  
  317. if __name__ == "__main__":
  318.     main()
  319. else:
  320.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement