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
| #-*- coding:utf-8 -*-
import pygame
import time
from random import randrange
from pygame.locals import *
class ElementGraphique() :
def __init__(self, image) :
self.image = image
self.rect = image.get_rect()
def afficher(self, fenetre):
fenetre.blit(self.image, self.rect)
def deplacer(self):
print "je ne me deplace pas"
class ElementAutogere (ElementGraphique) :
def __init__(self, image) :
ElementGraphique.__init__(self,image);
self.deltaX = 0
self.deltaY = 0
def deplacer(self):
self.rect.x += self.deltaX;
self.rect.y += self.deltaY;
print "je me deplace tout seul"
class Tir (ElementAutogere) :
def __init__(self, image) :
ElementAutogere.__init__(self,image);
self.deltaY = -10
class Perso (ElementGraphique) :
def __init__(self, image) :
ElementGraphique.__init__(self,image);
self.vitesse = 10;
def deplacer(self, touches, hauteur, largeur):
if touches[K_RIGHT] :
self.rect.x += self.vitesse
if touches[K_LEFT] :
self.rect.x -= self.vitesse
if touches[K_UP] :
self.rect.y -= self.vitesse
if touches[K_DOWN] :
self.rect.y += self.vitesse
if self.rect.x < 0 :
self.rect.x = 0
if self.rect.x > 750 :
self.rect.x = 750
if self.rect.y < 0 :
self.rect.y = 0
if self.rect.y > 550 :
self.rect.y = 550
print "je me deplace quand on joue avec le clavier"
class Monster (ElementGraphique) :
def __init__(self, image) :
ElementGraphique.__init__(self,image);
## Initialisation de la fenetre et création
pygame.init()
#creation de la fenetre
largeur = 850
hauteur = 650
fenetre=pygame.display.set_mode((largeur,hauteur))
## Lecture des differentes images.
# lecture de l'image du fond
imageFond = pygame.image.load("background.jpg").convert()
# lecture de l'image du perso
imagePerso = pygame.image.load("Spaceship.png").convert_alpha()
# lecture de l'image du projectile
imageProjectile = pygame.image.load("Laser.png").convert_alpha()
# lecture de l'image de l'ennemi 1
imageMonster = pygame.image.load("Monster.png").convert_alpha()
## Lecture des differents bruitages
# lecture du son du tir
son_Tir = pygame.mixer.Sound("tir.wav")
# Choix de la police pour le texte
font = pygame.font.Font(None, 34)
# Creation de la surface correspondant au texte
imageText = font.render('<Escape> pour quitter', True, (255, 255, 255))
tirs = []
perso = Perso(imagePerso);
monster = Monster(imageMonster);
fond = ElementGraphique(imageFond);
texte = ElementGraphique(imageText);
# rectangle du personnage
perso.rect.x = 60
perso.rect.y = 80
# rectangle du monster
monster.rect.x = 0
monster.rect.y = 50
# rectangle du texte
texte.rect.x = 10
texte.rect.y = 10
mesBalles = [];
for i in range (5):
uneBalle = ElementAutogere(imageMonster);
uneBalle.rect.x = 200 * i
uneBalle.rect.y = 10
uneBalle.deltaY = randrange(0, 4)
uneBalle.deltaX = randrange(0, 3)
mesBalles.append(uneBalle)
# servira a regler l'horloge du jeu
framerate = pygame.time.Clock()
continuer=1
while continuer:
pygame.event.clear()
fenetre.fill(0)
# fixons le nombre max de frames / secondes
framerate.tick(50)
# on recupere l'etat du clavier
touches = pygame.key.get_pressed();
fond.deplacer()
perso.deplacer(touches, largeur, hauteur)
for i in range(len(tirs)):
tirs[i].deplacer()
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_SPACE:
tir = Tir(imageProjectile);
tir.rect.x = perso.rect.x+15
tir.rect.y = perso.rect.y-45
tirs.append(tir)
son_Tir.play() #Lancer le bruitage du vaisseaux quand la touche ESPACE est appuiyer
if touches[K_ESCAPE] :
continuer=0
# Affichage du Fond
fond.afficher(fenetre)
# Affichage Perso
perso.afficher(fenetre)
for i in range(len(tirs)) :
tirs[i].afficher(fenetre)
# Affichage du Texte
texte.afficher(fenetre)
# Affichage des Balles
for i in range (5):
mesBalles[i].deplacer();
# Affichage des Balles
for i in range (5):
mesBalles[i].afficher(fenetre)
# On vide la pile d'evenements et on verifie certains evenements
for event in pygame.event.get(): # parcours de la liste des evenements recus
if event.type == QUIT: #Si un de ces evenements est de type QUIT
continuer = 0 # On arrete la boucle
# raffraichissement
pygame.display.flip()
# fin du programme principal...
pygame.quit() |
Partager