import pygame
pygame.init()
time = pygame.time.Clock()
ww = pygame.display.set_mode((500, 500))
ww.fill((0,175,230))
speed_x = 3
speed_y = 3
class Area():
def __init__(self, x,y,h,hx,):
self.rect = pygame.Rect(x,y,h,hx)
self.collor = (255, 255, 92)
self.test = 'hello'
def fill(self):
pygame.draw.rect(ww,self.color,self.rect)
def colliderect(self, rect):
return self.rect.colliderect(rect)
class Picture(Area):
def set_image(self,filename):
self.image = pygame.image.load(filename)
def draw(self, shift_x=0, shift_y=0):
ww.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))
ball = Picture(250,400 , 50,50)
ball.set_image('ball.png')
platform = Picture(250,500 , 50,50)
platform.set_image('platform.png')
mon = []
c = 50
for i in range(7):
mons = Picture( c,200, 55,55)
mons.set_image('enemy.png')
mons.draw()
mon.append(mons)
c += 55
b = 30
for i in range(8):
mons = Picture( b,150, 55,55)
mons.set_image('enemy.png')
mons.draw()
mon.append(mons)
b += 55
a = 10
for i in range(9):
mons = Picture( a,100, 55,55)
mons.set_image('enemy.png')
mons.draw()
mon.append(mons)
a += 55
move_left = 0
move_right = 0
while True :
## Проверка на столкновения
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
move_right = True
elif event.key == pygame.K_LEFT:
move_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a:
move_right = False
elif event.key == pygame.K_LEFT:
move_left = False
if move_right:
platform.rect.x += 3
if move_left:
platform.rect.x -= 3
ball.rect.x += speed_x
ball.rect.y += speed_y
if ball.colliderect(platform.rect):
speed_y *= -1
if ball.rect.y < 0:
speed_y *= -1
if ball.rect.x > 450 or ball.rect.x < 0:
speed_x *= -1
# Draw
ww.fill((0,175,230))
## Draw sprite
for i in mon:
i.draw()
platform.draw()
ball.draw()
pygame.display.update()
time.tick(40)