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
| import pygame
from pygame.locals import *
from math import*
pygame.init()
r=2*pi
class fleche():
def __init__(self,x,y):
self.img=pygame.image.load("cible.png").convert_alpha()
self.p_fleche=self.img.get_rect()
self.vo=0
self.vr=3
self.px=x
self.py=y
self.p_fleche=self.p_fleche.move(x,y)
def disp_fleche(self):
fenetre.blit(self.img,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-an)%r<pi:
self.vo+=0.1
else:
self.vo-=0.1
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)
class cible():
def __init__(self,x,y):
self.img=pygame.image.load("cible.png").convert_alpha()
self.p_cible=self.img.get_rect()
self.vx=0.0
self.vy=0.0
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=-2*sin(an)
self.vy=-2*cos(an)
self.px+=self.vx
self.py+=self.vy
self.p_cible[0]=int(self.px)
self.p_cible[1]=int(self.py)
fenetre = pygame.display.set_mode((640, 480))
perso =pygame.image.load("monstre.png").convert_alpha()
fond =pygame.image.load("fond.png").convert_alpha()
cible1=cible(40,40)
fleche1=fleche(300,40)
pos_fond=fond.get_rect()
position_perso = perso.get_rect()
fenetre.blit(perso, position_perso)
pygame.display.flip()
pygame.key.set_repeat(10,10)
continuer = True
position_perso = position_perso.move(0,460)
vy=0
key=[False]*323
while continuer:
pygame.time.Clock().tick(100)
for event in pygame.event.get():
if event.type == QUIT:
continuer = False
if event.type == KEYDOWN:
key[event.key]=True
if event.type == KEYUP:
key[event.key]=False
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
cible1.update_vec(position_perso)
fleche1.update_vec(position_perso)
fenetre.blit(fond, pos_fond)
cible1.disp_cible()
fleche1.disp_fleche()
fenetre.blit(perso, position_perso)
pygame.display.flip()
pygame.quit() |
Partager