Upload files to "/"

main
Irina Ternovaja 2024-02-19 07:43:10 +00:00
parent 59e682c963
commit b1f96c318d
2 changed files with 83 additions and 0 deletions

24
button.py 100644
View File

@ -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

59
main.py 100644
View File

@ -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()