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
| import pygame
from pygame.locals import *
from math import*
from random import*
from time import*
pygame.init()
r=2*pi
class pouf():
def __init__(self,x,y,vi,vt,vo):
self.img=pygame.image.load("pouf.png").convert_alpha()
self.p_pouf=self.img.get_rect()
self.vo=vo
self.vi=vi
self.vr=0
self.px=x
self.py=y
self.vt=vt
self.p_pouf=self.p_pouf.move(x,y)
def disp_pouf(self):
fenetre.blit(pygame.transform.rotate(self.img,degrees(self.vo)-90),self.p_pouf)
def update_vec(self,point):
if self.vr<=0:
an=atan2(point[0]-self.p_pouf[0],point[1]-self.p_pouf[1])
if (self.vo+0.5*pi-an)%r<pi:
self.vo-=self.vt
else:
self.vo+=self.vt
if (self.vo+pi+0.5*pi-an)%r>-0.1 and (self.vo+0.5*pi-an)%r<0.1:
self.vr+=self.vi
else:
self.vr-=0.2
self.px+=self.vr*cos(self.vo)
self.py-=self.vr*sin(self.vo)
self.p_pouf[0]=int(self.px)
self.p_pouf[1]=int(self.py)
def collision(self,point):
return (self.p_pouf[0]-point[0])**2+(self.p_pouf[1]-point[1])**2<400
class balle():
def __init__(self,x,y,vx,vy):
self.img=pygame.image.load("balle.png").convert_alpha()
self.p_balle=self.img.get_rect()
self.vx=vx
self.vy=vy
self.px=x
self.py=y
self.p_balle=self.p_balle.move(x,y)
def disp_balle(self):
fenetre.blit(self.img,self.p_balle)
def update_vec(self):
self.vy+=0.1
if self.px<0 or self.px>640:
self.vx=-self.vx
if self.py>480:
self.vy=-self.vy
self.px+=self.vx
self.py+=self.vy
self.p_balle[0]=int(self.px)
self.p_balle[1]=int(self.py)
def collision(self,point):
return (self.p_balle[0]-point[0])**2+(self.p_balle[1]-point[1])**2<400
class fleche():
def __init__(self,x,y,vr,vt,vo):
self.img=pygame.image.load("fusee.png").convert_alpha()
self.p_fleche=self.img.get_rect()
self.vo=vo
self.vr=vr
self.px=x
self.py=y
self.vt=vt
self.p_fleche=self.p_fleche.move(x,y)
def disp_fleche(self):
fenetre.blit(pygame.transform.rotate(self.img,degrees(self.vo)-90),self.p_fleche)
def update_vec(self,point):
an=atan2(point[0]-self.p_fleche[0],point[1]-self.p_fleche[1])
if ((self.vo+0.5*pi-an)%r)<pi:
self.vo-=self.vt
else:
self.vo+=self.vt
self.px+=self.vr*cos(self.vo)
self.py-=self.vr*sin(self.vo)
self.p_fleche[0]=int(self.px)
self.p_fleche[1]=int(self.py)
def collision(self,point):
return (self.p_fleche[0]-point[0])**2+(self.p_fleche[1]-point[1])**2<400
class cible():
def __init__(self,x,y,v):
self.img=pygame.image.load("cible.gif").convert_alpha()
self.p_cible=self.img.get_rect()
self.vx=0.0
self.vy=0.0
self.v=v
self.px=x
self.py=y
self.p_cible=self.p_cible.move(x,y)
def disp_cible(self):
fenetre.blit(self.img,self.p_cible)
def update_vec(self,point):
an=atan2(self.p_cible[0]-point[0],self.p_cible[1]-point[1])
self.vx=-self.v*sin(an)
self.vy=-self.v*cos(an)
self.px+=self.vx
self.py+=self.vy
self.p_cible[0]=int(self.px)
self.p_cible[1]=int(self.py)
def collision(self,point):
return (self.p_cible[0]-point[0])**2+(self.p_cible[1]-point[1])**2<self.p_cible[2]**2
fenetre = pygame.display.set_mode((640, 480))
perso =pygame.image.load("perso.png").convert_alpha()
fond =pygame.image.load("fond.png").convert_alpha()
fond=pygame.transform.scale(fond,(640,480))
font = pygame.font.Font(None, 36)
pos_fond=fond.get_rect()
fenetre.blit(fond, pos_fond)
lba=[]
lci=[]
lfl=[]
lpo=[]
for i in range(2):
lci.append(cible(randint(0,640),randint(0,360),randint(1,3)))
lci[i].disp_cible()
for i in range(3):
lba.append(balle(randint(0,640),200,uniform(-5,5),uniform(-5,5)))
lba[i].disp_balle()
for i in range(4):
lfl.append(fleche(randint(0,640),randint(0,360),7,0.03,uniform(0,r)))
lfl[i].disp_fleche()
for i in range(5):
lpo.append(pouf(randint(0,640),randint(0,360),12,0.03,uniform(0,r)))
lpo[i].disp_pouf()
position_perso = perso.get_rect()
position_perso = position_perso.move(240,460)
fenetre.blit(perso, position_perso)
pygame.display.flip()
pygame.key.set_repeat(10,10)
continuer = True
vy=0
for i in range(10):
pygame.time.delay(200)
pygame.display.flip()
deb=time()
while continuer:
pygame.time.Clock().tick(100)
for event in pygame.event.get():
if event.type == QUIT:
continuer = False
key=pygame.key.get_pressed()
if key[K_UP]:
vy=-5
if key[K_RIGHT]:
position_perso = position_perso.move(3,0)
if key[K_LEFT]:
position_perso = position_perso.move(-3,0)
position_perso = position_perso.move(0,vy)
if position_perso[0]<=0:
position_perso[0]=0
if position_perso[0]>=620:
position_perso[0]=620
if position_perso[1]<=0:
position_perso[1]=1
vy=0
if position_perso[1]>460:
position_perso[1]=460
vy=0
vy+=0.3
fenetre.blit(fond, pos_fond)
for ci in lci:
if ci.collision(position_perso):
continuer=0
fenetre.fill((0,0,0))
texte=font.render("tue par une cible",1,(255,255,255))
fenetre.blit(texte,(240,100))
pygame.display.flip()
pygame.time.delay(2000)
ci.update_vec(position_perso)
ci.disp_cible()
for fl in lfl:
if fl.collision(position_perso):
continuer=0
fenetre.fill((0,0,0))
texte=font.render("tue par une fleche",1,(255,255,255))
fenetre.blit(texte,(240,100))
pygame.display.flip()
pygame.time.delay(2000)
fl.update_vec(position_perso)
fl.disp_fleche()
for po in lpo:
if po.collision(position_perso):
continuer=0
fenetre.fill((0,0,0))
texte=font.render("tue par un insecte",1,(255,255,255))
fenetre.blit(texte,(240,100))
pygame.display.flip()
pygame.time.delay(2000)
po.update_vec(position_perso)
po.disp_pouf()
for ba in lba:
if ba.collision(position_perso):
continuer=0
fenetre.fill((0,0,0))
texte=font.render("tue par une balle",1,(255,255,255))
fenetre.blit(texte,(240,100))
pygame.display.flip()
pygame.time.delay(2000)
ba.update_vec()
ba.disp_balle()
fenetre.blit(perso, position_perso)
pygame.display.flip()
print int((time()-deb)*1000)/1000.0,'s'
pygame.quit() |
Partager