From b1f96c318d58b0a655b8f4a7b2a3ad2aca47f3e2 Mon Sep 17 00:00:00 2001 From: Irina Ternovaja Date: Mon, 19 Feb 2024 07:43:10 +0000 Subject: [PATCH] Upload files to "/" --- button.py | 24 ++++++++++++++++++++++ main.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 button.py create mode 100644 main.py diff --git a/button.py b/button.py new file mode 100644 index 0000000..77b7315 --- /dev/null +++ b/button.py @@ -0,0 +1,24 @@ +class Button(): + def __init__(self, x, y, image, scale): + width = image.get_width() + height = image.get_height() + self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale))) + self.rect = self.image.get_rect() + self.rect.topleft = (x, y) + self.clicked = False + + def draw(self, surface): + action = False + pos = pygame.mouse.get_pos() + + if self.rect.collidepoint(pos): + if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: + self.clicked = True + action = True + + if pygame.mouse.get_pressed()[0] == 0: + self.clicked = False + + surface.blit(self.image, (self.rect.x, self.rect.y)) + + return action \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..42884d9 --- /dev/null +++ b/main.py @@ -0,0 +1,59 @@ +import pygame +import sys +import button +import random + +def main(): + pygame.init() + + screen = pygame.display.set_mode((1000,700)) + pygame.display.set_caption("Space game") + icon = pygame.image.load("icon.png") + pygame.display.set_icon(icon) + background = pygame.image.load("space.jpg") + background = pygame.transform.scale(background, (1000,700)) + + difficulty_img = pygame.image.load("difficulty.png") + + #difficulty_button = button.Button(400, 410, difficulty_img, 1) + running = True + + difficulty = 0 + + def reset_enemies(difficulty): + enemies = [] + num_of_enemies = 4 + difficulty*2 + for i in range(num_of_enemies): + direction_x = 0.4 + + if random.randint(0, 1): + direction_x *= -1 + + if difficulty > 1: + direction_y = random.randint(0, 40*difficulty)/1000 + else: + direction_y = 0 + + enemies.append({ + "X": random.randint(0, 930), + "Y": random.randint(0, 30), + "X_change": direction_x, + "Y_change": direction_y + }) + return enemies + enemy_image = pygame.image.load("enemy.png") + enemies = reset_enemies(difficulty) + num_of_enemies = len(enemies) + + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + + screen.blit(background, (0, 0)) + + pygame.display.flip() + +if __name__=="__main__": + main()