Skip to content

Commit

Permalink
Added multiple property decorators and added new methods to the butto…
Browse files Browse the repository at this point in the history
…n class, and removed redundant variables, more info is provided in the release of 2.2.0
  • Loading branch information
Spacexplorer11 committed Oct 23, 2024
1 parent f0b0294 commit 5d159c6
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 122 deletions.
26 changes: 25 additions & 1 deletion space_dodge/classes/button.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import pygame as p

from space_dodge.file_handling.constants_and_file_loading import WINDOW


class Button:
def __init__(self, image, x, y):
self.image = image
self._image = image
self.x = x
self.y = y
self.pos = (x, y)
self.width = image.get_width()
self.height = image.get_height()
self.image_hover = p.transform.scale(self._image, (self.width + 3, self.height + 3))
self.rect = p.Rect(x, y, self.width, self.height)

@property
def image(self):
mouse_pos = p.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
return self.image_hover
else:
return self._image


def update_rect(self, x):
self.rect = p.Rect(x, self.y, self.width, self.height)
self.x = x
return self.rect

def clicked(self):
mouse_pos = p.mouse.get_pos()
if self.rect.collidepoint(mouse_pos) and p.mouse.get_pressed()[0]:
return True
else:
return False

def draw(self):
WINDOW.blit(self.image, self.pos)
31 changes: 23 additions & 8 deletions space_dodge/classes/player.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
from space_dodge.file_handling.constants_and_file_loading import HEIGHT, PLAYER_HEIGHT, PLAYER_WIDTH, PLAYER_VELOCITY, \
playerL, playerR
playerL, playerR, WIDTH


class Player:
def __init__(self):
self.x = 0
self._x = 0
self.y = HEIGHT - PLAYER_HEIGHT
self.width = PLAYER_WIDTH
self.height = PLAYER_HEIGHT
self.velocity = PLAYER_VELOCITY
self.left_image = playerL
self.right_image = playerR
self.image = playerL
self._image = playerL
self.direction = 0 # The direction the player is facing( written in binary ) 0 = left, 1 = right

def direction(self, direction):
if direction == 0:
self.image = self.left_image
@property
def image(self):
if self.direction == 0:
self._image = self.left_image
else:
self.image = self.right_image
return self.image
self._image = self.right_image
return self._image

@property
def x(self):
return self._x

@x.setter
def x(self, value):
if value - self.velocity >= 0 and value + self.velocity + self.width <= WIDTH:
self._x = value

@property
def pos(self):
return self._x, self.y
41 changes: 0 additions & 41 deletions space_dodge/classes/slider.py

This file was deleted.

21 changes: 10 additions & 11 deletions space_dodge/drawing/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
from space_dodge.classes.animation import Animation
from space_dodge.file_handling.constants_and_file_loading import (
WINDOW, WIDTH, HEIGHT, FONT, BULLET_HEIGHT,
threeLives, twoLives, oneLife, background, bullet_texture, bullet_explosion_frames,
pauseButtonImage, muteImage, unmuteImage
)
threeLives, twoLives, oneLife, game_background, bullet_texture, bullet_explosion_frames)

# Create a mask for the bullet
bullet_mask = pygame.mask.from_surface(bullet_texture)


def draw(player, bullets, direction, highscore, highscoreBreak, mute, lives, timeText, scoreText, explosions, dt,
settingsButton):
WINDOW.blit(background, (0, 0))
def draw(player, bullets, highscore, highscoreBreak, mute, lives, timeText, scoreText, explosions, dt,
muteSymbol, unmuteSymbol, settingsButton, pauseButton):
WINDOW.blit(game_background, (0, 0))

# Draw settings icon
WINDOW.blit(settingsButton.image, (settingsButton.x, settingsButton.y))
Expand All @@ -36,19 +34,20 @@ def draw(player, bullets, direction, highscore, highscoreBreak, mute, lives, tim
WINDOW.blit(timeText, (10, 10))
WINDOW.blit(scoreText, (WIDTH - 270, 10))
WINDOW.blit(FONT.render("Your high score", 1, "white"), (250, 10))
WINDOW.blit(pauseButtonImage, (scoreText.get_width() + 745, 19))
WINDOW.blit(muteImage if mute else unmuteImage, (timeText.get_width() + 10, 10))
WINDOW.blit(pauseButton.image, (scoreText.get_width() + 745, 19))
WINDOW.blit(muteSymbol.image if mute else unmuteSymbol.image, (timeText.get_width() + 10, 10))
highScoreText = FONT.render(f" is {highscore}" if highscoreBreak else f" was {highscore}", 1, "white")
WINDOW.blit(highScoreText, (500, 10))

# Draw player
WINDOW.blit(player.direction(direction), (player.x, player.y))
WINDOW.blit(player.image, player.pos)

# Draw lives
lives_images = {3: threeLives, 2: twoLives, 1: oneLife}
lives_images = {3: threeLives,
2: twoLives, 1: oneLife}
if lives in lives_images:
WINDOW.blit(lives_images[lives], (780, 50))
else:
WINDOW.blit(background, (0, 0))
WINDOW.blit(game_background, (0, 0))

pygame.display.update()
33 changes: 21 additions & 12 deletions space_dodge/drawing/title_screen/draw_title_screen.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import pygame

from space_dodge.classes.button import Button
from space_dodge.drawing.tutorial_and_information.keybindings import keybindings_screen
from space_dodge.drawing.tutorial_and_information.settings import settings_menu
from space_dodge.drawing.tutorial_and_information.welcome import welcome_screen
from space_dodge.file_handling.constants_and_file_loading import (
WINDOW, WIDTH, HEIGHT, start_button_image, muteImage, unmuteImage, title_screen_image
WINDOW, start_button_image, muteImage, unmuteImage, title_screen_background, settingsIcon, WIDTH, HEIGHT
)
from space_dodge.file_handling.utility import ref

# Initialise pygame & pygame.mixer
pygame.init()
pygame.mixer.init()

# Define Rect objects
start_button_rect = start_button_image.get_rect(center=(WIDTH / 2, HEIGHT / 2))
muteSymbol_rect = muteImage.get_rect(x=50, y=200, width=80, height=60)
unmuteSymbol_rect = unmuteImage.get_rect(x=50, y=200, width=80, height=60)


# Draw the title screen
def draw_title():
Expand All @@ -26,22 +23,34 @@ def draw_title():
mute = False
start = False

# Define Button objects
muteSymbol = Button(muteImage, 50, 200)
unmuteSymbol = Button(unmuteImage, 50, 200)
startButton = Button(start_button_image, 350, 300)
settingsButton = Button(settingsIcon, WIDTH - settingsIcon.get_width() - 10,
HEIGHT - settingsIcon.get_height())

while not start:
WINDOW.blit(title_screen_image, (0, 0))
WINDOW.blit(start_button_image, start_button_rect.topleft)
WINDOW.blit(muteImage if mute else unmuteImage, (50, 200))
WINDOW.blit(title_screen_background, (0, 0))
WINDOW.blit(startButton.image, startButton.pos)
if mute:
WINDOW.blit(muteSymbol.image, muteSymbol.pos)
else:
WINDOW.blit(unmuteSymbol.image, unmuteSymbol.pos)
WINDOW.blit(settingsButton.image, settingsButton.pos)

for event in pygame.event.get():
if event.type == pygame.QUIT:
return False, 0.0
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
if start_button_rect.collidepoint(mouse_x, mouse_y):
if startButton.clicked():
start = True
pygame.mixer.music.stop()
elif muteSymbol_rect.collidepoint(mouse_x, mouse_y) or unmuteSymbol_rect.collidepoint(mouse_x, mouse_y):
elif muteSymbol.clicked() or unmuteSymbol.clicked():
mute = not mute
pygame.mixer.music.unpause() if not mute else pygame.mixer.music.pause()
elif settingsButton.clicked():
settings_menu(mute, music_path="sounds/background_music/title_screen/title_screen_music.mp3")

pygame.display.update()

Expand Down
5 changes: 2 additions & 3 deletions space_dodge/drawing/tutorial_and_information/keybindings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import time

import pygame
from pygame.mixer_music import pause

# Importing the crucial variables from the constants file
from space_dodge.file_handling.constants_and_file_loading import WINDOW, WIDTH, HEIGHT, welcome_screen_image, FONT, \
from space_dodge.file_handling.constants_and_file_loading import WINDOW, WIDTH, HEIGHT, welcome_screen_background, FONT, \
FONT_SMALL

keybindText1 = FONT.render("Press M to mute/unmute or just click the symbol.", 1, "orange")
Expand All @@ -15,7 +14,7 @@


def keybindings_screen(pausedTimes):
WINDOW.blit(welcome_screen_image, (0, 0))
WINDOW.blit(welcome_screen_background, (0, 0))
keybindText1Place = WIDTH / 2 - keybindText1.get_width() / 2, 300
WINDOW.blit(keybindText1, keybindText1Place)
WINDOW.blit(keybindText2, (WIDTH / 2 - keybindText2.get_width() / 2,
Expand Down
4 changes: 2 additions & 2 deletions space_dodge/drawing/tutorial_and_information/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


@pause_time
def settings_menu(mute):
def settings_menu(mute, music_path=None):
pygame.mixer.music.load(ref("sounds/background_music/pause_screen/pause_music.mp3"))
pygame.mixer.music.play(-1)
slider = Slider(WINDOW, 350, 400, 100, 30, min=0, max=100, initial=pygame.mixer.music.get_volume() * 100)
Expand Down Expand Up @@ -45,7 +45,7 @@ def settings_menu(mute):
elif keys[pygame.K_ESCAPE]:
pygame.mixer.music.stop()
pygame.mixer.music.unload()
pygame.mixer.music.load(ref("sounds/background_music/background_music.mp3"))
pygame.mixer.music.load(ref("sounds/background_music/background_music.mp3" if music_path is None else music_path))
pygame.mixer.music.play(-1)
return True

Expand Down
4 changes: 2 additions & 2 deletions space_dodge/drawing/tutorial_and_information/welcome.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pygame

# Importing the crucial variables from the constants file
from space_dodge.file_handling.constants_and_file_loading import WINDOW, WIDTH, HEIGHT, welcome_screen_image, FONT, \
from space_dodge.file_handling.constants_and_file_loading import WINDOW, WIDTH, HEIGHT, welcome_screen_background, FONT, \
FONT_SMALL


def welcome_screen():
WINDOW.blit(welcome_screen_image, (0, 0))
WINDOW.blit(welcome_screen_background, (0, 0))
welcomeText1 = FONT.render("Welcome to Space Dodge!", 1, "orange")
welcomeText2 = FONT.render("Use A & D keys to move left & right!", 1, "orange")
welcomeText3 = FONT.render("Try not to hit the bullets!", 1, "orange")
Expand Down
6 changes: 3 additions & 3 deletions space_dodge/file_handling/constants_and_file_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ def update_loading_bar(assets_num=1):
draw_except("Lives")

try:
background = t.scale(i.load(ref("assets/space_background.jpg")), (WIDTH, HEIGHT))
title_screen_image = t.scale(i.load(ref("assets/title_screen.jpg")), (WIDTH, HEIGHT))
welcome_screen_image = t.scale(i.load(ref("assets/welcome_screen.png")), (WIDTH, HEIGHT))
game_background = t.scale(i.load(ref("assets/space_background.jpg")), (WIDTH, HEIGHT))
title_screen_background = t.scale(i.load(ref("assets/title_screen.jpg")), (WIDTH, HEIGHT))
welcome_screen_background = t.scale(i.load(ref("assets/welcome_screen.png")), (WIDTH, HEIGHT))
pause_background = t.scale(i.load(ref("assets/pause_background.png")), (WIDTH, HEIGHT))
update_loading_bar(4)
except FileNotFoundError:
Expand Down
Loading

0 comments on commit 5d159c6

Please sign in to comment.