Untitled
Anonymous
plain_text
04/24/2026 6:25 PM
3.9 KB
73
Indexable
import pygame
pygame.init()
MONSTER_WIDTH = 50
PLATFORM_WIDTH = 80
BALL_SIZE = 50
SPACING = 5
back = (200, 255, 255) #колір фону (background)
mw = pygame.display.set_mode((500, 500)) #вікно програми (main window)
mw.fill(back)
class Area():
def __init__(self, x=0, y=0, width=10, height=10, color=None):
self.rect = pygame.Rect(x, y, width, height)
self.fill_color = back
if color:
self.fill_color = color
def color(self, new_color):
self.fill_color = new_color
def fill(self):
pygame.draw.rect(mw, self.fill_color, self.rect)
def collidepoint(self, x, y):
return self.rect.collidepoint(x, y)
def is_collide(self, game_object):
return self.rect.colliderect(game_object.rect)
class Picture(Area):
def __init__(self, x=0, y=0, width=10, height=10, color=None, filename = ""):
super().__init__(x, y, width, height, color)
self.image = pygame.image.load(filename)
def draw(self):
mw.blit(self.image, (self.rect.x, self.rect.y))
ball = Picture(200,300,50,50, filename="ball.png")
platform = Picture(200,400, PLATFORM_WIDTH,20, filename= "platform.png")
monsters = []
start_x = SPACING
start_y = SPACING
count = 9
for j in range(4): #заповнення рядів
y = start_y + ((MONSTER_WIDTH) * j )
x = start_x + ((MONSTER_WIDTH / 2) * j)
for i in range(count):
m = Picture(x, y, MONSTER_WIDTH, MONSTER_WIDTH, filename = "enemy.png")
monsters.append(m)
x = x + MONSTER_WIDTH + SPACING
count = count - 1
clock = pygame.time.Clock()
move_right = False
move_left = False
speed_x = 3
speed_y = 3
game_over = False
while not game_over:
mw.fill(back)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
move_left = True
if event.key == pygame.K_d:
move_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a:
move_left = False
if event.key == pygame.K_d:
move_right = False
if move_left == True and platform.rect.x > 0:
platform.rect.x -= 5
if move_right == True and platform.rect.x + PLATFORM_WIDTH < 500:
platform.rect.x += 5
if ball.rect.y < 0:
speed_y = speed_y * -1
if ball.rect.x > 500 - BALL_SIZE or ball.rect.x < 0:
speed_x = speed_x * -1
if ball.is_collide(platform):
speed_y *= -1
ball.rect.x += speed_x
ball.rect.y -= speed_y
ball.draw()
platform.draw()
for m in monsters:
m.draw()
pygame.display.update()
clock.tick(40)
pygame.quit()Editor is loading...
Leave a Comment