Skip to content

Commit

Permalink
Merge pull request #1 from Cynthion21x/scenes-and-menus
Browse files Browse the repository at this point in the history
Scenes and menus
  • Loading branch information
Cynthion21x authored Jan 30, 2024
2 parents bec43c4 + 8037e9e commit f18ba68
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 67 deletions.
Binary file added assets/fonts/Sobiscuit.ttf
Binary file not shown.
Binary file added assets/fonts/default.ttf
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes.
50 changes: 50 additions & 0 deletions src/core/UI/elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pygame
import src.math.vectors as v
import src.shared.constants as c

class UiElement:

def __init__(self, pos, size):

self.pos = pos
self.size = size

class text(UiElement):

def __init__(self, pos, size, text, font=None, colour=c.Colours.WHITE):

super().__init__(pos, size)

self.textImg = font.render(text, True, colour)

ratio = self.textImg.get_size()[1] / self.textImg.get_size()[0]

newSize = v.Vector(size.x, size.x * ratio)

self.textImg = pygame.transform.smoothscale(self.textImg, newSize.value())

def render(self, display):

display.blit(self.textImg, self.pos.value())

class photo(UiElement):

def __init__(self, pos, size, image, preserveAspect = False):

super().__init__(pos, size)

if preserveAspect == False:

self.image = pygame.transform.smoothscale(image, size.value())

else:

ratio = image.get_size()[1] / image.get_size()[0]

newSize = v.Vector(size.x, size.x * ratio)

self.image = pygame.transform.smoothscale(image, newSize.value())

def render(self, display):

display.blit(self.image, self.pos.value())
71 changes: 59 additions & 12 deletions src/core/content/contentManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,8 @@ def __init__(self):

self.loadContent()

def loadContent(self):

self.textBase = dict()

self.spriteBase = dict()

self.soundBase = dict()
def loadText(self):

l.Logger.log("Loading Content...")

# text
self.textBase["title"] = c.ASSETS_PATH + "\\titleText.txt"

for i in os.listdir(c.ASSETS_PATH + "\\language"):
Expand All @@ -52,7 +43,7 @@ def loadContent(self):

f.close()

# Sprites
def loadSprites(self):

try:
self.spriteBase["icon"] = pygame.image.load(c.ASSETS_PATH + "\\icon.png")
Expand All @@ -79,6 +70,48 @@ def loadContent(self):

l.Logger.log("Failed to load", file, c.Logs.WARNING)

def loadFonts(self):

pygame.font.init()

for root, dirs, files in os.walk(c.ASSETS_PATH + "\\fonts"):

for file in files:

cDir = root + "\\" + file

identifier = root + "\\" + os.path.splitext(file)[0]

identifier = identifier.replace(c.ASSETS_PATH + "\\fonts\\", "")

l.Logger.log("Loading", identifier + os.path.splitext(file)[1])

try:

self.fontBase[identifier] = pygame.font.Font(cDir, 64)

except:

l.Logger.log("Failed to load", file, c.Logs.WARNING)

def loadContent(self):

self.textBase = dict()

self.spriteBase = dict()

self.fontBase = dict()

self.soundBase = dict()

l.Logger.log("Loading Content...")

self.loadText()

self.loadSprites()

self.loadFonts()

self.modLauncher.loadMods()

l.Logger.log("Content loaded!")
Expand All @@ -105,12 +138,26 @@ def Sprite(self, name):

except:

l.Logger.log("Failed to load", name, c.Logs.WARNING)
l.Logger.log("Failed to load sprite", name, c.Logs.WARNING)

data = self.spriteBase["error"]

return data

def Font(self, name):

try:

data = self.fontBase[name]

except:

l.Logger.log("Failed to load font", name, c.Logs.WARNING)

data = self.fontBase["default"]

return data

def getSound(self, name):

pass
Expand Down
45 changes: 29 additions & 16 deletions src/core/game.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import pygame
import src.core.inputManager as inputManager
import src.core.content.contentManager as content
import src.scenes.splashScreen as splash
import src.shared.constants as c
import src.shared.logger as l

class Game():

def __init__(self, message):

self.GameState = c.States.SPLASH

pygame.init()

self.message = message
Expand All @@ -25,34 +27,45 @@ def __init__(self, message):

# my stuff
self.circlePos = pygame.Vector2(c.SCREEN_WIDTH / 2, c.SCREEN_HEIGHT / 2)
#deltatime

#vdeltatime
self.deltaTime = 0

# scenes
self.splashScreen = splash.splashScreen(self)

def coreLoop(self):

while self.running:

keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.circlePos.y -= 300 * self.deltaTime
if keys[pygame.K_s]:
self.circlePos.y += 300 * self.deltaTime
if keys[pygame.K_a]:
self.circlePos.x -= 300 * self.deltaTime
if keys[pygame.K_d]:
self.circlePos.x += 300 * self.deltaTime
if self.GameState == c.States.SPLASH:

self.splashScreen.run()

if self.GameState == c.States.GAME:

self.display.fill("purple")
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.circlePos.y -= 300 * self.deltaTime
if keys[pygame.K_s]:
self.circlePos.y += 300 * self.deltaTime
if keys[pygame.K_a]:
self.circlePos.x -= 300 * self.deltaTime
if keys[pygame.K_d]:
self.circlePos.x += 300 * self.deltaTime

pygame.draw.circle(self.display, "red", self.circlePos, 40)
self.display.fill("purple")

pygame.draw.circle(self.display, "red", self.circlePos, 40)

self.deltaTime = self.clock.tick(c.FRAME_RATE) / 1000

self.window.blit(self.display, (0, 0))
pygame.display.flip()

for event in pygame.event.get():
if event.type == pygame.QUIT:
self.close()
pygame.display.flip()

self.deltaTime = self.clock.tick(c.FRAME_RATE) / 1000

pygame.quit()

Expand Down
12 changes: 0 additions & 12 deletions src/math/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,3 @@ def solve(self, _node):

elif _node.type == c.tokens.VAR:
return self.x

# --- Test ---

testInput = "x+10=29"

funct = Function(testInput)

l.Logger.log(testInput, logLevel = c.Logs.TEST)

for i in range(-5, 5):

l.Logger.log(testInput, funct.evaluate(i))
45 changes: 21 additions & 24 deletions src/math/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,6 @@ def angle(self):
y = self.y

return math.atan(y/x)

# Vector Addition
def add(self, vector):

self.x += vector.x
self.y += vector.y

return self

# Multiply by constant
def multC(self, constant):

self.x *= constant
self.y *= constant

return self

# Multiply by vector
def multV(self, vector):

newM = self.distance() * vector.distance()
newA = self.angle() * vector.angle()

return FromBearing(newA, newM)

# Vector set to values from 0-1
def normalize(self):
Expand All @@ -59,6 +35,27 @@ def value(self):

return (self.x, self.y)

# Multiply vector by number
def mult(vector1, const):

return Vector(vector1.x * const, vector1.y * const)

# Add two vectors together
def add(vector1, vector2):

x = vector1.x + vector2.x
y = vector1.y + vector2.y

return Vector(x, y)

# Sub two vectors
def sub(vector1, vector2):

x = vector1.x - vector2.x
y = vector1.y - vector2.y

return Vector(x, y)

# Generator Functions
def FromBearing(angle, magnitude):

Expand Down
6 changes: 6 additions & 0 deletions src/scenes/mainMenu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

class mainMenu:

def __init__(self, game):

pass
42 changes: 42 additions & 0 deletions src/scenes/splashScreen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import src.shared.constants as c
import src.core.UI.elements as elements
import src.core.content.contentManager as content
import src.math.vectors as v
import pygame

class splashScreen:

def __init__(self, game):

self.game = game

center = v.mult(v.Vector(c.SCREEN_WIDTH, c.SCREEN_HEIGHT), 0.5)

TitleSize = v.Vector(900, 500)

self.title = elements.photo(

v.add(v.sub(center, v.mult(TitleSize, 0.5)), v.Vector(0, -60)), TitleSize,
content.fetch().Sprite("Titles\\gameTitle-alt"),
True

)

self.prompts = elements.text(

v.sub(center, v.Vector(150, -150)), v.Vector(300, 100),
"Press Enter To Start",
content.fetch().Font("Sobiscuit")

)

def run(self):

self.game.display.fill(c.Colours.BACKGROUND)

self.title.render(self.game.display)
self.prompts.render(self.game.display)

keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
self.game.GameState = c.States.GAME
12 changes: 9 additions & 3 deletions src/shared/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@

class Colours:

RED = pygame.Color(255, 0, 0)
BLUE = pygame.Color(0, 0, 255)
GREEN = pygame.Color(0, 255, 0)
RED = pygame.Color(204, 39, 61)
BLUE = pygame.Color(38, 50, 181)
GREEN = pygame.Color(49, 161, 91)

WHITE = pygame.Color(255, 255, 255)
BLACK = pygame.Color(0, 0 ,0)

BACKGROUND = pygame.Color(5, 4, 59)

# Log Levels

Expand All @@ -39,6 +44,7 @@ class Logs:

class States:

SPLASH = 2
MENU = 0
GAME = 1

Expand Down

0 comments on commit f18ba68

Please sign in to comment.