Update main.py

main
Marija Boiko 2024-03-03 19:54:57 +00:00
parent 6354532665
commit 73047b9406
1 changed files with 27 additions and 28 deletions

55
main.py
View File

@ -9,35 +9,35 @@ import os.path
pygame.init()
screen = pygame.display.set_mode((1000, 700))
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("ICON.png")
icon = pygame.image.load("images/ICON.png")
pygame.display.set_icon(icon)
space = pygame.image.load('SPACE.jpg')
hangar = pygame.image.load('HANGAR.jpg')
space = pygame.image.load('images/SPACE.jpg')
hangar = pygame.image.load('images/HANGAR.jpg')
mixer.music.load("music/background.mp3")
mixer.music.play(-1)
font = pygame.font.Font("KodeMono-Medium.ttf", 32)
smaller_font = pygame.font.Font("KodeMono-Medium.ttf", 24)
over_font = pygame.font.Font("KodeMono-Medium.ttf", 96)
font = pygame.font.Font("font/KodeMono-Medium.ttf", 32)
smaller_font = pygame.font.Font("font/KodeMono-Medium.ttf", 24)
over_font = pygame.font.Font("font/KodeMono-Medium.ttf", 96)
TEXT_COL = (255, 255, 255)
SCORE_FILE_NAME = "score.scr"
new_game_img = pygame.image.load("new_game.png")
difficulty_img = pygame.image.load("difficulty.png")
best_score_img = pygame.image.load("best_score.png")
back_img = pygame.image.load("back.png")
difficulty_easy_img = pygame.image.load("difficulty_easy.png")
difficulty_medium_img = pygame.image.load("difficulty_medium.png")
difficulty_hard_img = pygame.image.load("difficulty_hard.png")
difficulty_insane_img = pygame.image.load("difficulty_insane.png")
new_game_img = pygame.image.load("images/new_game.png")
difficulty_img = pygame.image.load("images/difficulty.png")
best_score_img = pygame.image.load("images/best_score.png")
back_img = pygame.image.load("images/back.png")
difficulty_easy_img = pygame.image.load("images/difficulty_easy.png")
difficulty_medium_img = pygame.image.load("images/difficulty_medium.png")
difficulty_hard_img = pygame.image.load("images/difficulty_hard.png")
difficulty_insane_img = pygame.image.load("images/difficulty_insane.png")
new_game_button = button.Button(400, 320, new_game_img, 1)
@ -148,9 +148,8 @@ def read_score_table():
def add_score(score):
current = read_score_table()
current.append(score)
current.sort()
updated = current[1:5]
print(score, updated)
current.sort(reverse=True) # Sort in descending order
updated = current[:4] # Keep only the top 4 scores
update_score_table(updated)
@ -162,18 +161,18 @@ textY = 10
#player data
playerX = 450
playerY = 580
player_image = pygame.image.load("SPACESHIP.png")
player_image = pygame.image.load("images/SPACESHIP.png")
playerX_change = 0
#bullet data
bullet_image = pygame.image.load("BULLET.png")
bullet_image = pygame.image.load("images/BULLET.png")
bulletX = 0
bulletY = 560
bulletY_change = 20
bullet_state = "ready"
#enemy data
enemy_image = pygame.image.load("ENEMY.png")
enemy_image = pygame.image.load("images/ENEMY.png")
enemies = reset_enemies(difficulty)
num_of_enemies = len(enemies)
@ -234,7 +233,7 @@ while running:
score_val = 0
#if game is lost, print some text like wanna start again? press R to continue
game_lost = True
draw_text('Press "R" to return to menu', font, TEXT_COL, 270, 600)
draw_text('Press "TAB" to return to menu', font, TEXT_COL, 270, 600)
break
# enemy movement
@ -250,7 +249,7 @@ while running:
#Collision check
collision = is_collision(enem["X"], enem["Y"], bulletX, bulletY)
if collision:
explosion_Sound = mixer.Sound("explosion.wav")
explosion_Sound = mixer.Sound("music/explosion.wav")
explosion_Sound.play()
bulletY = 560
bullet_state = "ready"
@ -265,10 +264,10 @@ while running:
bullet_state = "ready"
if bullet_state == "fire":
shoot_bullet(bulletX, bulletY)
bulletY -= 2.5
bulletY -= 4.5
# score, enemy and player update
draw_text("Press TAB to pause", smaller_font, TEXT_COL, 750, 10)
draw_text("Press R to pause", smaller_font, TEXT_COL, 750, 10)
show_score(textX, textY)
for i, enem in enumerate(enemies):
enemy(enem["X"], enem["Y"])
@ -285,7 +284,7 @@ while running:
case 3:
temp = "insane"
print_best_score(read_score_table())
draw_text("Press TAB to resume", smaller_font, TEXT_COL, 350, 550)
draw_text("Press R to resume", smaller_font, TEXT_COL, 350, 550)
case _:
screen.blit(space, (0, 0))
#event loop
@ -293,13 +292,13 @@ while running:
if scene == "game":
# player movement with left and right arrows. When key is unpressed, stop the movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_TAB:
if event.key == pygame.K_r:
if game_paused:
game_paused = False
else:
game_paused = True
# if game is over, then you can restart it by pressing the R key
if game_lost and event.key == pygame.K_r:
if game_lost and event.key == pygame.K_TAB:
scene = "menu"
game_lost = False
if event.key == pygame.K_LEFT:
@ -308,7 +307,7 @@ while running:
playerX_change = 1.2
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_sound = mixer.Sound("Laser.wav")
bullet_sound = mixer.Sound("music/Laser.wav")
bullet_sound.play()
bulletX = playerX
shoot_bullet(bulletX, bulletY)