-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadsPygame.py
More file actions
86 lines (72 loc) · 2.63 KB
/
Copy paththreadsPygame.py
File metadata and controls
86 lines (72 loc) · 2.63 KB
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
import pygame,sys, random
from threading import Thread
from pygame.locals import *
pygame.init()
surface = pygame.display.set_mode((800,600))
pygame.display.set_caption("Threads test")
#COLORES
WHITE = (255,255,255)
BLACK = (0,0,0)
threads = [] #Lista que contiene todos los hilos creados en el ciclo
balls = [] #Lista que contiene las bolas creadas en el ciclo
killnum = 0 #Numero que indica cual es la bola que quiere detener
running = True #Estado de la ventana
#Bola que se ve moverse en la ventana
class Ball():
def __init__(self, color, x, y, height, width, speedX, speedY):
self.color = color
self.x = x
self.y = y
self.height = height
self.width = width
self.speedX = speedX
self.speedY = speedY
self.dead = False
"""
Método que será ejecutado como hilo para hacer el moviemiento de la ventana, el atributo dead indica cuando
queremos matar al hilo, el delay se representa en milisegundos
"""
def move(self, surface):
self.dead = False
while (not self.dead):
if (self.x > 800) or (self.x < 0):
self.speedX = self.speedX * -1
if (self.y > 600) or (self.y < 0):
self.speedY = self.speedY * -1
self.x += self.speedX
self.y += self.speedY
pygame.time.delay(1)
#Método que dibuja la bola
def draw(self, surface):
pygame.draw.rect(surface, self.color, (self.x, self.y, self.height, self.width))
"""
Método que crea bolas la cantidad de bolas que desee, y las ubica aleatoriamente en un punto dentro de la ventana y
le asigna una velocidad en x y y entre -1 y 1. Crea un hilo con cada una de estas bolas y los hace mover.
"""
def createBalls(numBalls):
for i in range(0, numBalls):
ball = Ball(WHITE, random.randint(50,750), random.randint(50,550), 10, 10, random.choice([1,-1]), random.choice([1,-1]))
balls.append(ball)
#target: metodo que sera el hilo, name: nombre del hilo, args: argumentos del método
process = Thread(target = ball.move, name = "Thread #"+str(i), args = (surface,))
#daemon: indica si quiero que los hilos mueran cuando el programa se cierre
process.daemon = True
process.start()
print(process.name+" created")
threads.append(process)
createBalls(10)
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: #Con el espacio mato los hilos en orden de creacion
if killnum < len(balls):
balls[killnum].dead = True
killnum += 1
surface.fill(BLACK)
for ball in balls:
ball.draw(surface) #Dibujamos las bolas creadas
pygame.display.update()