Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- '''Still missing the best bit. Yes thats the bullets. but don't worry they're on their way'''
- '''Get the full project ZIP here https://www.dropbox.com/s/xvg5tv3lc2txuox/Stickman%20Skirmish%20V2.0.zip?dl=0'''
- '''Have fun!'''
- import pygame
- from random import *
- # -- Global constants
- wall_list = []
- platform_hit_list = []
- # Colors
- BLACK = ( 0, 0, 0)
- WHITE = ( 255, 255, 255)
- BLUE = ( 0, 0, 255)
- GREY = ( 96, 96, 96)
- # Screen dimensions
- SCREEN_WIDTH = 1200
- SCREEN_HEIGHT = 800
- # This class represents the bar at the bottom that the player controls
- class Player(pygame.sprite.Sprite):
- """ This class represents the bar at the bottom that the player controls. """
- # Constructor function
- def __init__(self, x, y, image_init, nbreDeVie, nomDuPerso):
- # Call the parent's constructor
- super().__init__()
- global done
- global player_image
- global player2_image
- self.vie = nbreDeVie
- self.nom = nomDuPerso
- # Set player
- self.image = pygame.image.load(image_init)
- # Make our top-left corner the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.y = y
- self.rect.x = x
- # Set speed vector
- self.change_x = 0
- self.change_y = 0
- self.blockcount = 0
- self.jumpcount = 0
- self.walls = None
- def subit_attaque(self, attaquant):
- taux_reussite = random()
- if taux_reussite > 0.2:
- h=random()
- if h<0.2:
- self.vie -= 2
- else:
- self.vie -= 1
- def changespeed(self, x, y):
- """ Change the speed of the player. """
- self.change_x += x
- self.change_y += y
- def update(self):
- """ Update the player position. """
- self.blockcount=0
- # Move left/right
- self.rect.x += self.change_x
- # Did this update cause us to hit a wall?
- block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
- for block in block_hit_list:
- # If we are moving right, set our right side to the left side of the item we hit*
- if self.change_x > 0:
- self.rect.right = block.rect.left
- else:
- # Otherwise if we are moving left, do the opposite.
- self.rect.left = block.rect.right
- # Move up/down
- self.rect.y += self.change_y
- # Check and see if we hit anything
- block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
- for block in block_hit_list:
- # Reset our position based on the top/bottom of the object.
- if self.change_y >= 0:
- self.rect.bottom = block.rect.top
- self.blockcount=1
- else:
- self.rect.top = block.rect.bottom
- """ Calculate effect of gravity. """
- if self.change_y == 0:
- self.change_y = 1
- else:
- self.change_y += .20
- # See if we are on the ground.
- if self.rect.y >= SCREEN_HEIGHT: #+ (self.rect.height/4): "and self.change_y >= 0"
- #self.change_y = 0
- self.rect.y = SCREEN_HEIGHT #+ (self.rect.height/4)
- def jump(self):
- """ Appeler quand le joueur saute """
- if self.blockcount == 1:
- self.jumpcount = 0
- if self.jumpcount < 3:
- self.jumpcount += 1
- self.change_y = -5
- def image_perso(self, chemin_image):
- global player_image
- global player2_image
- self.image = pygame.image.load(chemin_image)
- class Bullet(pygame.sprite.Sprite):
- def __init__(self,x,y):
- super().__init__()
- self.rect
- class Wall(pygame.sprite.Sprite):
- """ Wall the player can run into. """
- def __init__(self, x, y, width, height):
- """ Constructor for the wall that the player can run into. """
- # Call the parent's constructor
- super().__init__()
- # Make a grey wall, of the size specified in the parameters
- self.image = pygame.Surface([width, height])
- self.image.fill(GREY)
- # Make our top-left corner the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.y = y
- self.rect.x = x
- def main():
- # Call this function so the Pygame library can initialize itself
- pygame.init()
- # Create an 800x600 sized screen
- screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
- # Set the title of the window
- pygame.display.set_caption('Test')
- # List to hold all the sprites
- all_sprite_list = pygame.sprite.Group()
- # Make the walls. (x_pos, y_pos, width, height)
- wall_list = pygame.sprite.Group()
- all_sprite_list.add(wall_list)
- wall = Wall(150, 636, 379, 67)
- wall_list.add(wall)
- all_sprite_list.add(wall)
- wall = Wall(855, 636, 379, 67)
- wall_list.add(wall)
- all_sprite_list.add(wall)
- wall = Wall(216, 475, 90, 47)
- wall_list.add(wall)
- all_sprite_list.add(wall)
- wall = Wall(1048, 475, 90, 47)
- wall_list.add(wall)
- all_sprite_list.add(wall)
- wall = Wall(566, 443, 245, 11)
- wall_list.add(wall)
- all_sprite_list.add(wall)
- wall = Wall(215, 263, 450, 46)
- wall_list.add(wall)
- all_sprite_list.add(wall)
- wall = Wall(0, 126, 122, 46)
- wall_list.add(wall)
- all_sprite_list.add(wall)
- wall = Wall(358, 86, 200, 46)
- wall_list.add(wall)
- all_sprite_list.add(wall)
- wall = Wall(826, 133, 481, 47)
- wall_list.add(wall)
- all_sprite_list.add(wall)
- done = False
- # Create the player paddle object
- player = Player(20, -50, "player/Player-rouge-droite.png", 20, "Player_rouge")
- player.walls = wall_list
- all_sprite_list.add(player)
- player2 = Player(1000, 0, "player/Player-bleu-gauche.png", 20, "Player_bleu")
- player2.walls = wall_list
- all_sprite_list.add(player2)
- clock = pygame.time.Clock()
- while not done:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- elif player.vie <= 0:
- print("Player rouge est mort! Victoire joueur bleu!")
- done = True
- elif player2.vie <= 0:
- print("Player bleu est mort! Victoire joueur rouge!")
- done = True
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- done = True
- if event.key == pygame.K_r: #Un reset des positions des personnages
- player.rect.x = 20
- player.rect.y = -50
- player2.rect.x = 1000
- player2.rect.y = 0
- if event.key == pygame.K_LEFT:
- player.changespeed(-5, 0)
- player_image = 'player/Player-rouge-gauche.png'
- player.image_perso(player_image)
- elif event.key == pygame.K_RIGHT:
- player.changespeed(5, 0)
- player_image = 'player/Player-rouge-droite.png'
- player.image_perso(player_image)
- elif event.key == pygame.K_UP:
- player.jump()
- elif event.key == pygame.K_RCTRL:
- player.subit_attaque(player2)
- elif event.key == pygame.K_a:
- player2.changespeed(-5, 0)
- player2_image = 'player/Player-bleu-gauche.png'
- player2.image_perso(player2_image)
- elif event.key == pygame.K_d:
- player2.changespeed(5, 0)
- player2_image = 'player/Player-bleu-droite.png'
- player2.image_perso(player2_image)
- elif event.key == pygame.K_w:
- player2.jump()
- elif event.key == pygame.K_SPACE:
- player.subit_attaque(player)
- elif event.type == pygame.KEYUP:
- if event.key == pygame.K_LEFT:
- player.changespeed(5, 0)
- elif event.key == pygame.K_RIGHT:
- player.changespeed(-5, 0)
- if event.key == pygame.K_a:
- player2.changespeed(5, 0)
- elif event.key == pygame.K_d:
- player2.changespeed(-5, 0)
- all_sprite_list.update()
- screen.fill(BLACK)
- all_sprite_list.draw(screen)
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
- if __name__ == "__main__":
- main()
- else:
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement