Untitled

 avatar
unknown
plain_text
9 months ago
3.4 kB
19
Indexable
import pygame
pygame.init()


back = (200, 255, 255) #background color
mw = pygame.display.set_mode((500, 500)) #main window
mw.fill(back)
clock = pygame.time.Clock()

#flag for the end of the game
game_over = False
#a class from a previous project
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 colliderect(self, rect):
       return self.rect.colliderect(rect)


class Picture(Area):
    def __init__(self, filename, x=0, y=0, width=10, height=10, color=None):
        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))



class Label(Area):
    def set_text(self, text, fsize=64, text_color=(0, 0, 0)):
        self.image = pygame.font.SysFont('verdana', fsize).render(text, True, text_color)
    def draw(self, shift_x=0, shift_y=0):
        self.fill()
        mw.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))




ball = Picture("ball.png", 250, 200, 50, 50)
platfom = Picture("platform.png", 250, 400, 100, 20)


start_x = 5
start_y = 5
monsters = []
n = 9

for j in range(3):
    y = start_y + (55 * j)
    x = start_x + (55/2 * j)

    for i in range(n):
        m = Picture("enemy.png", x, y, 50, 50)
        monsters.append(m)
        x = x + 55
    
    n = n  - 1

move_right = False
move_left = False

SPEED_X = 3
SPEED_Y = 3

while not game_over:
    mw.fill(back)
    
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                move_right = True
            elif event.key == pygame.K_LEFT:
                move_left = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
               move_right = False
            elif event.key == pygame.K_LEFT:
                move_left = False

    if ball.colliderect(platfom.rect):
        SPEED_Y *= -1

    ball.rect.y += SPEED_Y
    ball.rect.x += SPEED_X

    if ball.rect.x >= 500 - 50:
        SPEED_X *= -1 
    
    if ball.rect.x <= 0:
        SPEED_X *= -1
    
    # програш 
    if ball.rect.y >= 500:
        lose_text = Label(150, 250)
        lose_text.set_text("ВИ ПРОГРАЛИ!")
        lose_text.draw()
        game_over = True
    
    if ball.rect.y <= 0:
        SPEED_Y *= -1

    if move_right and platfom.rect.x + 100 < 500:
        platfom.rect.x += 10

    if move_left and platfom.rect.x > 0:
        platfom.rect.x -= 10

    for m in monsters:
        m.draw()
    for m in monsters:
        if ball.colliderect(m.rect):
            SPEED_Y *= -1
            monsters.remove(m)
    
    if len(monsters) == 0:
        lose_text = Label(150, 250)
        lose_text.set_text("ВИ ВИГРАЛИ!")
        lose_text.draw()
        game_over = True
        
    

    platfom.draw()
    ball.draw()

    pygame.display.update()
    clock.tick(40)


Editor is loading...
Leave a Comment