Gestion de la Vélocité en Python avec Pygame
Résolu
Creuilcreuil
Messages postés
44
Date d'inscription
Statut
Membre
Dernière intervention
-
Creuilcreuil Messages postés 44 Date d'inscription Statut Membre Dernière intervention -
Creuilcreuil Messages postés 44 Date d'inscription Statut Membre Dernière intervention -
Bonsoir, je suis en train d'esseai de compred le calcul de la vélocité mais mon code ne fonctionne pas et je ne comprend pas pourquoi si qq'un pourrait m'aidé, merci de m'avoir lue.
import pygame
from pygame.locals import *
pygame.init();
window = pygame.display.set_mode((600,600));
pygame.display.set_caption("Velocité");
class Player:
def __init__(self, x = 300, y = 300, speed = 5, speed_max = 50):
self.x = x;
self.y = y;
self.vx = 0;
self.vy = 0;
self.speed = speed;
self.speed_max = speed_max;
def move(self, dir):
if dir == 'u':
self.vy -= self.speed;
elif dir == 'd':
self.vy += self.speed;
elif dir == 'l':
self.vx -= self.speed;
elif dir == 'r':
self.vx += self.speed;
def update(self):
self.x += self.vx;
self.y += self.vy;
if self.vx >= self.speed_max:
self.vx += (self.speed_max-1);
elif self.vx <= (-self.speed_max):
self.vx -= (self.speed_max - 1);
if self.vy >= self.speed_max:
self.vy += (self.speed_max - 1);
elif self.vy <= (-self.speed_max):
self.vy -= (self.speed_max - 1);
if self.vx > 0:
self.vx -= 1;
elif self.vx < 0:
self.vx += 1;
if self.vy > 0:
self.vy -= 1;
elif self.vy < 0:
self.vy += 1;
def get(self):
return {
'x':self.x, 'y':self.y, 'vx':self.vx, 'vy':self.vy, 'speed':self.speed, 'speed_max':self.speed_max,
};
done_prog = False;
while not done_prog:
player = Player();
player.update();
for event in pygame.event.get():
if event.type == QUIT:
done_prog = True;
elif event.type == KEYDOWN:
if event.key == K_UP:
player.move('u');
elif event.key == K_DOWN:
player.move('d');
elif event.key == K_LEFT:
player.move('l');
elif event.key == K_RIGHT:
player.move('r');
stat = player.get();
info = ('x:' + str(stat['x']) +
' y:' + str(stat['y']) +
' vx:' + str(stat['vx']) +
' vy:' + str(stat['vy']) +
' speed:' + str(stat['speed']) +
' speed_max:' + str(stat['speed_max']))
pygame.display.flip(); window.fill((0,0,0));
window.blit(pygame.font.Font(None, 18).render(info, True, (255, 255, 255)), (0, 0));
pygame.draw.rect(window, (0, 255, 0), [stat['x'], stat['y'], 10, 10]);
A voir également:
- Gestion de la Vélocité en Python avec Pygame
- Citizen code python avis - Accueil - Outils
- Logiciel gestion locative gratuit excel - Télécharger - Comptabilité & Facturation
- Logiciel gestion photo gratuit - Guide
- Gestion de fichiers - Télécharger - Gestion de fichiers
- Gestion autorisation application android - Guide
2 réponses
Salut,
Tu ne définis pas quel est le résultat voulu. Une vitesse constante ? une accélération ?
De plus, au niveau de ce qui sera effectivement obtenu à l'écran et visible par l'utilisateur, il faut que tu aies un mécanisme de régulation des FPS (frame per second = images par seconde). Autrement, suivant l'ordinateur utilisé, le jeu ira plus ou moins vite. Tu peux faire cela en déclarant un objet de type pygame.time.Clock() il me semble, puis en appelant la méthode tick(fps) de ton objet Clock à chaque tour de boucle. (je te laisse checker la documentation python pour un exemple plus précis, ou sinon tu peux te tourner vers une librairie qui donne ce genre de mécanismes par défaut)
Tu ne définis pas quel est le résultat voulu. Une vitesse constante ? une accélération ?
De plus, au niveau de ce qui sera effectivement obtenu à l'écran et visible par l'utilisateur, il faut que tu aies un mécanisme de régulation des FPS (frame per second = images par seconde). Autrement, suivant l'ordinateur utilisé, le jeu ira plus ou moins vite. Tu peux faire cela en déclarant un objet de type pygame.time.Clock() il me semble, puis en appelant la méthode tick(fps) de ton objet Clock à chaque tour de boucle. (je te laisse checker la documentation python pour un exemple plus précis, ou sinon tu peux te tourner vers une librairie qui donne ce genre de mécanismes par défaut)
Merci, mais désolé j'avais oublié de fermé ce topic, voila le code au quelle je suis arrivé, il et pas parfait mois pour comprend le base de la vélocité je pense bien m'etre débrouillé
#Python 3.4 from pygame.locals import * import pygame, time, math window_size = (1000, 600); pygame.init(); window = pygame.display.set_mode(window_size); pygame.display.set_caption("V-Force"); Fun = [0, 0, 0]; Bgc = [255, 255, 255]; class Player: def __init__(self, x = window_size[0] /2, y = window_size[1] /2, speed = 0.75, speed_max = 75, fx = 0.25, fy = 0.25): self.x = x; self.vx = 0; self.fx = fx; self.y = y; self.vy = 0; self.fy = fy; self.speed = speed; self.speed_max = speed_max; def move(self, dir): if dir == 'up': self.vy += -self.speed; elif dir == 'down': self.vy += self.speed; elif dir == 'left': self.vx += -self.speed; elif dir == 'right': self.vx += self.speed; def update(self): self.x += self.vx; self.y += self.vy; if self.vx > self.speed_max: self.vx = (self.speed_max); elif self.vx < (-self.speed_max): self.vx = -(self.speed_max); if self.vy > self.speed_max: self.vy = (self.speed_max); elif self.vy < (-self.speed_max): self.vy = -(self.speed_max); if self.vx > 0: self.vx += -self.fx; if self.vx < 0: self.vx = 0; elif self.vx < 0: self.vx += self.fx; if self.vx > 0: self.vx = 0; if self.vy > 0: self.vy += -self.fy; if self.vy < 0: self.vy = 0; elif self.vy < 0: self.vy += self.fy; if self.vy > 0: self.vy = 0; def set(self, x, y): self.x = x; self.y = y; def get(self): return { 'x':self.x, 'y':self.y, 'vx':self.vx, 'vy':self.vy, 'speed':self.speed, 'speed_max':self.speed_max, 'fx':self.fx, 'fy':self.fy, }; def Display(window, x, y, s, *text): Font = pygame.font.Font(None, s); count = y; for line in text: window.blit(Font.render(line, True, (255,255,255)), (x, count)); count += s; player = Player(); done_prog = False; while not done_prog: pressed = pygame.key.get_pressed(); cube_size = 30; pygame.time.Clock().tick(60); for event in pygame.event.get(): if event.type == QUIT: done_prog = True; if pressed[K_UP]: player.move('up'); if pressed[K_DOWN]: player.move('down'); if pressed[K_LEFT]: player.move('left'); if pressed[K_RIGHT]: player.move('right'); player.update(); stat = player.get(); if stat['x'] > window_size[0] + cube_size /2: player.set(math.ceil(window_size[0]/6.25) - cube_size, stat['y']); if stat['y'] > window_size[1] + cube_size /2: player.set(stat['x'], 0 - cube_size); if stat['x'] < math.ceil(window_size[0]/6.25) - cube_size: player.set(window_size[0], stat['y']); if stat['y'] < - cube_size: player.set(stat['x'], window_size[1]); Fun[2] += 1; if Fun[2] == 255: Fun[2] = 0; Fun[1] += 1; if Fun[1] == 255: Fun[1] = 0; Fun[0] += 1; if Fun[0] == 255: Fun = [0, 0, 0]; Bgc[2] -= 1; if Bgc[2] == 0: Bgc[2] = 255; Bgc[1] -= 1; if Bgc[1] == 0: Bgc[1] = 255; Bgc[0] -= 1; if Bgc[0] == 0: Bgc = [255, 255, 255]; pygame.display.flip(); window.fill(Bgc); pygame.draw.rect(window, Fun, [stat['x'], stat['y'], cube_size, cube_size]); pygame.draw.rect(window, (0, 0, 0), [0, 0, math.ceil(window_size[0] /6.25), window_size[1]]); Display(window, 10, 10, 20, 'X:' + str(stat['x']), 'Y:' + str(stat['y']), '___________________', 'VX:' + str(stat['vx']), 'VY:' + str(stat['vy']), '___________________', 'FX:' + str(stat['fx']), 'FY:' + str(stat['fy']), '___________________', 'SPEED:' + str(stat['speed']), 'SPEED_MAX:' + str(stat['speed_max']), '___________________', 'K_UP:' + str(pressed[K_UP]), 'K_DOWN:' + str(pressed[K_DOWN]), 'K_LEFT:' + str(pressed[K_LEFT]), 'K_RIGHT:' + str(pressed[K_RIGHT]), '___________________', 'CBC:' + str(Fun), 'BGC:' + str(Bgc), '___________________', 'Python 3.4.3', 'Pygame 1.9.2a0', '', '', '', '', '___________________', 'CREDIT:Creuilcreuil', );