Bonjour tous le monde,
pour m'entrainer a utiliser pygame et pyhton en génerale j'ai essailler de recrée le jeu the powder toy.
voici le code principale:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import pygame
import pyautogui
 
from particule import Part
 
class Game:
    def __init__(self, screen):
        self.screen = screen
        self.running = True
        self.clock = pygame.time.Clock()
        self.parts = {}
        self.color = "black"
        self.z = 0
 
    def handling_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False
 
        keys = pygame.key.get_pressed()
        if keys[pygame.K_c]:
            x = 5 * round(pyautogui.position()[0] / 5)
            y = 5 * round(pyautogui.position()[1] / 5)
            if {"x":x,"y":y} not in self.parts.values():
                self.parts[self.z] = {"x": x,"y": y,}
                self.z += 1
                print(self.z)
 
 
    def update(self):
 
        for part in self.parts.copy():
            if self.parts[part]["y"] <= 550 and {"x":self.parts[part]["x"],"y":self.parts[part]["y"] + 5,} not in self.parts.values():
                self.parts[part]["y"] += 5
 
 
    def display(self):
        for part in self.parts.copy():
            rect = Part(self.parts[part]["x"], self.parts[part]["y"]-5)
            rect.draw(self.screen, self.color)
            rect1 = Part(self.parts[part]["x"], self.parts[part]["y"])
            rect1.draw(self.screen,"red")
 
 
        pygame.display.flip()
 
    def run(self):
        while self.running:
            self.handling_events()
            self.update()
            self.display()
            self.clock.tick(60)
 
 
pygame.init()
screen = pygame.display.set_mode((900, 600))
game = Game(screen)
game.run()
 
pygame.quit()
et la classe part:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
import pygame
 
 
class Part:
 
    def __init__(self, x, y):
        self.rect = pygame.Rect(x, y, 5, 5)
 
    def draw(self, screen, color):
        pygame.draw.rect(screen,color,self.rect)
voila mes question:
-comment optimiser mon code pour le rendre plus performent
-la class part est elle utile dans ce genre de jeu
-comment enregistrer chaque rect de l'ecran efficacement

Merci pour vos réponce