-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolisoes.py
141 lines (107 loc) · 3.7 KB
/
colisoes.py
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
import sys
import pygame
import math
from random import randrange, random, choice
num = int(input("Número de discos: "))
resolution = [600, 600]
#Cores
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
GREEN = [0, 255, 0]
RED = [255, 0, 0]
BROWN = [128, 0, 0]
#Tela
screen = pygame.display.set_mode(resolution)
pygame.display.set_caption('CAIXA')
class Disco():
def __init__(self, cor, xPos, yPos, xVel, yVel, rad):
self.cor = cor
self.x = xPos
self.y = yPos
self.dx = xVel
self.dy = yVel
self.radius = rad
#self.ax = xAcc
#self.ay = yAcc
#self.mass = m
self.type = "disco"
def debug(self, disco):
dx = (self.x - disco.x)
dy = (self.y - disco.y)
distancia = math.sqrt(dx * dx + dy * dy)
diametro = (self.radius + disco.radius)
print(f"Self: Posição X = {self.x} Posição Y = {self.y} VelX = {self.dx} VelY = {self.y} raio = {self.radius}")
print(f"Disco: Posição X = {disco.x} Posição Y = {disco.y} VelX = {disco.dx} VelY = {disco.y} raio = {disco.radius}")
print(f"Distancia = {distancia} Diâmetro = {diametro}")
def colision(self, disco):
dx = (self.x - disco.x)
dy = (self.y - disco.y)
distancia = math.sqrt(dx*dx + dy*dy)
diametro = (self.radius + disco.radius)
if distancia <= diametro:
self.debug(disco)
self.dx *= -1
self.dy *= -1
disco.dx *= -1
disco.dy *= -1
#def force(self):
#fx = self.ax/self.mass
#fy = self.ay/self.mass
def draw(self, screen):
pygame.draw.circle(screen, self.cor, (int(self.x), int(self.y)), self.radius)
def update(self, gameObjects):
self.x += self.dx
self.y += self.dy
for disc in gameObjects:
if disc != self:
self.colision(disc)
if (self.x - self.radius <= 0 or self.x + self.radius >= resolution[0]):
self.dx *= -1
if (self.y - self.radius <= 0 or self.y + self.radius >= resolution[1]):
self.dy *= -1
class Game():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode(resolution)
self.clock = pygame.time.Clock()
self.gameObjects = []
def load(self):
matriz = []
overlapping = False
i = 0
protecao = 0
while len(matriz) < num:
rad = randrange(20, 50)
disco = Disco(choice([BLACK, WHITE, RED, BROWN]), randrange(rad, (resolution[0] - rad)), randrange(rad, (resolution[1] - rad)), 1, 1, rad)
overlapping = False
for j in range(len(matriz)):
outro = matriz[j]
dx = (disco.x - outro.x)
dy = (disco.y - outro.y)
distancia = math.sqrt(dx*dx + dy*dy)
if distancia < (disco.radius + outro.radius):
overlapping = True
break
if overlapping == False:
matriz.append(disco)
self.gameObjects.append(disco)
i = i + 1
protecao = protecao + 1
if protecao > 10000:
break
def handleEvents(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
def run(self):
while True:
self.handleEvents()
self.screen.fill(GREEN)
for gameObj in self.gameObjects:
gameObj.draw(self.screen)
gameObj.update(self.gameObjects)
self.clock.tick(30)
pygame.display.flip()
game = Game()
game.load()
game.run()