1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
| import pygame
import math
import random
from pygame.locals import *
from pygame.math import Vector2
pygame.init()
*
*
#Ouverture de la fenêtre Pygame
fenetre = pygame.display.set_mode((1280,720))
*
*
pygame.mixer.music.load("Maple leaf rag.mp3")
son=pygame.mixer.Sound("pas.wav")
son.set_volume(0.1)
*
#Chargement et collage du fond
fond = pygame.image.load("Map.png").convert()
fond = pygame.transform.scale(fond , (1280,720))
fenetre.blit(fond, (0,0))
*
pygame.time.set_timer(USEREVENT+1, 1000)
pygame.time.set_timer(USEREVENT+2, 1000)
FPS = 60
clock = pygame.time.Clock()
pygame.mixer.music.play()
RATE_DASH = 300
*
class ZOMBIE(pygame.sprite.Sprite):
*
****def __init__(self, position):
********super(ZOMBIE, self).__init__()
*
********self.image = pygame.image.load("perso.png").convert_alpha()
********self.rect = self.image.get_rect(topleft=position)
********self.position = pygame.math.Vector2(position)
********self.speed = 1
*
****def hunt_JOUEUR(self, JOUEUR):
********JOUEUR_position = JOUEUR.rect.topleft
********direction = JOUEUR_position - self.position
********velocity = direction.normalize() * self.speed
*
********self.position += velocity
********self.rect.topleft = self.position
*
****def update(self, JOUEUR):
********self.hunt_JOUEUR(JOUEUR)
*
*
class JOUEUR(pygame.sprite.Sprite):
*
****def __init__(self, position):
********super(JOUEUR, self).__init__()
*
********self.image = pygame.Surface((32, 32))
********self.image.fill(pygame.Color('blue'))
********self.rect = self.image.get_rect(topleft=position)
*
********self.position = pygame.math.Vector2(position)
********self.velocity = pygame.math.Vector2(0, 0)
********self.angle = 0
********self.speed = 3
********self.vie = 100
********self.stamina = 100
********self.vulnerabilité = 0
*
****def update(self):
********keys = pygame.key.get_pressed()
********if keys[pygame.K_LEFT]:
************if self.position.x <= 70:
****************self.velocity.x = 0
************else:
****************self.velocity.x = -self.speed
****************self.angle = 180
****************son.play()
********elif keys[pygame.K_RIGHT]:
************if self.position.x >= 1212:
****************self.velocity.x = 0
************else:
****************self.velocity.x = self.speed
****************self.angle = 0
****************son.play()
********else:
************self.velocity.x = 0
*
********if keys[pygame.K_UP]:
************if self.position.y <= 70:
****************self.velocity.y = 0
************else:
****************self.velocity.y = -self.speed
****************self.angle = -90
****************son.play()
********elif keys[pygame.K_DOWN]:
************if self.position.y >= 649:
****************self.velocity.y = 0
************else:
****************self.velocity.y = self.speed
****************self.angle = 90
****************son.play()
********else:
************self.velocity.y = 0
*
********if keys[pygame.K_SPACE]:
************if not self.fire:
****************if self.stamina > 9:
********************if self.angle == 180:
************************self.velocity.x = -80
************************self.stamina -= 10
************************print(self.stamina)
*
********************if self.angle == 0:
************************self.velocity.x = 80
************************self.stamina -= 10
************************print(self.stamina)
*
********************if self.angle == -90:
************************self.velocity.y = -80
************************self.stamina -= 10
************************print(self.stamina)
*
********************if self.angle == 90:
************************self.velocity.y = 80
************************self.stamina -= 10
************************print(self.stamina)
*
****************else:
********************print("plus de stamina")
*
*
*
****************self.fire = True # Active Delay
************else:
****************self.count += 1
****************if self.count >= RATE_DASH:
********************self.fire = False
********************self.count = 0
********else:
************self.count = 0
************self.fire = False
*
********self.position += self.velocity
********self.rect.topleft = self.position
*
*
class Bullet(pygame.sprite.Sprite):
****def __init__(self, position, angle):
********super().__init__()
********self.image = pygame.Surface([7, 7])
********self.image.fill(pygame.Color('black'))
********self.rect = self.image.get_rect(center=position)
********offset = Vector2(0, 0).rotate(angle)
********self.position = Vector2(position) + offset
********self.velocity = Vector2(9, 0).rotate(angle)
********self.degat = 10
*
****def update(self):
********self.position += self.velocity
********self.rect.center = self.position
*
********if self.rect.x < 0 or self.rect.x > 1280 or self.rect.y < 0 or self.rect.y > 720:
************self.kill()
*
player = JOUEUR(position=(350, 220))
monster = ZOMBIE
all_sprites_list = pygame.sprite.Group()
vague_de_monstre = pygame.sprite.Group()
monstre_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
*
for i in range(3):
****monstre = ZOMBIE(player.position)
****tmpx = random.randrange(0,680)
****tmpy = random.randrange(0,480)
****monstre = ZOMBIE(position=(tmpx, tmpy))
****all_sprites_list.add(monstre)
****ZOMBIE.add(monstre)
*
*
running = True
while running:
*
****clock.tick(FPS)
*
****for event in pygame.event.get():
********if event.type == pygame.QUIT:
************running = False
*
********elif event.type == pygame.MOUSEBUTTONDOWN:
************if event.button == 1:
****************bullet = Bullet(player.position, player.angle)
****************all_sprites_list.add(bullet)
****************bullet_list.add(bullet)
*
********elif player.stamina < 100:
************if event.type == USEREVENT+1:
****************player.stamina += 1
****************print (player.stamina)
*
********elif player.vulnerabilité == 1:
************if event.type == USEREVENT+1:
****************player.vulnerabilité -= 1
*
*
****player.update()
****monstre_list.update()
****bullet_list.update()
****all_sprites_list.update(player)
*
*
****fenetre.blit(fond, (0,0))
****fenetre.blit(player.image, player.rect)
****monstre_list.draw(fenetre)
****bullet_list.draw(fenetre)
****all_sprites_list.draw(fenetre)
*
****pygame.display.update()
*
pygame.quit() |