import pygame
import random
# אתחול של pygame
pygame.init()
# הגדרות חלון
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("אנימציה של דמויות")
# צבעים
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# מחלקת דמות
class Character:
def __init__(self, name, color):
self.name = name
self.color = color
self.x = random.randint(50, width - 50)
self.y = random.randint(50, height - 50)
self.speed = 5
def draw(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), 20)
def move(self):
self.x += random.choice([-self.speed, self.speed])
self.y += random.choice([-self.speed, self.speed])
# מגביל את הדמות לא לצאת מגבולות המסך
self.x = max(20, min(width - 20, self.x))
self.y = max(20, min(height - 20, self.y))
# יצירת דמויות
characters = [
Character("הלוחם", GREEN),
Character("דרויד", RED),
]
# לולאת המשחק
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(WHITE) # ניקוי המסך
# עדכון והצגת הדמויות
for character in characters:
character.move()
character.draw()
pygame.display.flip() # עדכון התצוגה
pygame.time.delay(100) # עיכוב לסנכרון
pygame.quit()