forked from virtualeiro/pygame-imfvj2-2023-24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAula6_projectile3.py
76 lines (63 loc) · 1.92 KB
/
Aula6_projectile3.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
import pygame
import sys
import math
GRAVITY =1
class Projectile:
def __init__(self, x, y, v0, angle):
self.x = x
self.y = y
self.v0 = v0
self.angle = angle
self.delta_time = 0.01
self.vx = 0
self.vy = 0
self.time = 0
self.radius=5
self.color=(0,0,0)
def shoot(self):
#Vx = V0×cos(α)
self.vx=self.v0*math.cos(math.radians(self.angle))
#Vy = V0×sin(α)
self.vy=-self.v0*math.sin(math.radians(self.angle))
def update(self):
self.time += self.delta_time
#x=x0+Vxt
#x=Vx×t
self.x += self.vx*self.delta_time
#y=h+Vy*t−(g×t2)/2 no inicio h=y0 altura inicial
#y=y0+v0yt-1/2gt2
self.y += self.vy*self.delta_time - (GRAVITY*self.delta_time**2)/2
self.vy += GRAVITY*self.delta_time
def draw(self, screen):
pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)
def main():
# Initialize Pygame
pygame.init()
# Set up the display window
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Particle Class Example")
# Define colors (RGB format)
BLACK = (0, 0, 0)
# Create a Particle object
particle = Projectile(20, height // 2, 25, 45)
# Main loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
particle.shoot()
# Fill the background with white
screen.fill((255, 255, 255))
# Draw the particle
particle.update()
particle.draw(screen)
# Update the display
pygame.display.flip()
if __name__ == "__main__":
main()