Salut les gars, je suis nouveau sur pygame.J'ai développé un jeu simple dans lequel la balle rebondit les uns les autres.Il fonctionne bien.Début de l'événement quand on clique sur le bouton avec pygame
J'ai ajouté un ui avec des boutons avec des options comme new game
, , options
. Ce dont j'ai besoin, c'est que lorsqu'un utilisateur clique sur new game button
, il doit voir la balle rebondir. Mon code est
import pygame
import math
from itertools import cycle
def magnitude(v):
return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))
def add(u, v):
return [ u[i]+v[i] for i in range(len(u)) ]
def sub(u, v):
return [ u[i]-v[i] for i in range(len(u)) ]
def dot(u, v):
return sum(u[i]*v[i] for i in range(len(u)))
def normalize(v):
vmag = magnitude(v)
return [ v[i]/vmag for i in range(len(v)) ]
screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
class Ball(object):
def __init__(self, path):
self.x, self.y = (0, 0)
self.img = pygame.image.load('ball.jpg')
self.speed = 2.5
self.color = (200, 200, 200)
self.path = cycle(path)
self.set_target(next(self.path))
self.sound = pygame.mixer.music.load('yeah.mp3')
@property
def pos(self):
return self.x, self.y
# for drawing, we need the position as tuple of ints
# so lets create a helper property
@property
def int_pos(self):
return map(int, self.pos)
@property
def target(self):
return self.t_x, self.t_y
@property
def int_target(self):
return map(int, self.target)
def next_target(self):
self.set_target(self.pos)
self.set_target(next(self.path))
def set_target(self, pos):
self.t_x, self.t_y = pos
def update(self):
# if we won't move, don't calculate new vectors
if self.int_pos == self.int_target:
return self.next_target()
target_vector = sub(self.target, self.pos)
# a threshold to stop moving if the distance is to small.
# it prevents a 'flickering' between two points
if magnitude(target_vector) < 2:
return self.next_target()
# apply the balls's speed to the vector
move_vector = [c * self.speed for c in normalize(target_vector)]
# update position
self.x, self.y = add(self.pos, move_vector)
def draw(self):
screen.blit(self.img, self.int_pos)
pygame.mixer.music.play()
class Option:
hovered = False
def __init__(self, text, pos):
self.text = text
self.pos = pos
self.set_rect()
self.draw()
def draw(self):
self.set_rend()
screen.blit(self.rend, self.rect)
def set_rend(self):
self.rend = menu_font.render(self.text, True, self.get_color())
def get_color(self):
if self.hovered:
return (255, 255, 255)
else:
return (100, 100, 100)
def set_rect(self):
self.set_rend()
self.rect = self.rend.get_rect()
self.rect.topleft = self.pos
pygame.init()
quit = False
path = [(26, 43),(105, 110),(45, 225),(145, 295),(266, 211),(178, 134),(250,5)(147,12)]
path2 = [(26, 43),(105, 10),(45, 125),(150, 134),(150, 26),(107, 12)]
ball = Ball(path)
ball.speed = 1.9
ball2 = Ball(path2)
ball2.color = (200, 200, 0)
balls = [ball, ball2]
screen = pygame.display.set_mode((480, 320))
menu_font = pygame.font.Font(None, 40)
options = [Option("NEW GAME", (140, 105)), Option("LOAD GAME", (135, 155)),
Option("OPTIONS", (145, 205))]
while not quit:
pygame.event.pump()
screen.fill((0, 0, 0))
for option in options:
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered = True
else:
option.hovered = False
option.draw()
pygame.display.update()
quit = pygame.event.get(pygame.QUIT)
pygame.event.poll()
map(Ball.update, balls)
screen.fill((0, 0, 0))
map(Ball.draw, balls)
pygame.display.flip()
clock.tick(60)
Quand j'ai essayé ce code, le rebond de la balle et l'interface de départ fonctionne très bien, mais quand je clique sur le nouveau bouton icône, il ne montre rien.
Ce dont j'ai besoin, c'est quand un utilisateur clique sur le bouton new game
qu'il doit rediriger vers l'écran de rebond de la balle.
Je l'ai essayé avec pygame.mouse.get_pressed
mais cela ne m'a pas aidé.
J'espère que vous pouvez m'aider.
Merci à l'avance
@odera ... si vous connaissez le Anser s'il vous plaît dire. – user3792941