Skip to content

Commit

Permalink
fixed character creator backround started to implement shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
GusInfiniteLinesOfCode committed Jun 28, 2024
1 parent 969d046 commit 9651dd4
Show file tree
Hide file tree
Showing 25 changed files with 320 additions and 208 deletions.
Binary file modified src/Recources/characters/current.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 6 additions & 7 deletions src/SquarePixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,27 @@
# Call the function to play the video
logo.play_intro_video(image_folder, not_skipped, screen, 0)
# start()
screen: pig.Surface = pig.display.set_mode(
(infoObject.current_w, infoObject.current_h - 32), pig.RESIZABLE
)
pig.display.toggle_fullscreen()

#pig.display.toggle_fullscreen()
image_folder: str = r"Recources\\NewHorizonsFrames"
logo.play_intro_video(image_folder, not_skipped, screen, 1)
play_music(r"Recources\sounds\music\Menu.mp3")
with open(r"Recources\data\first.txt", "r") as first:
bool_value = first.readlines()
if bool(bool_value[0]):
import SquarePixels.uimanagement.EllipsesWarning
from SquarePixels.uimanagement.EllipsesWarning import warning
warning(screen)

# with open(r"Recources\data\first.txt", "w") as first:3423434234234
# None564523454324324
# first.write("False") disabled for developmental purposes423423423423

MainMen.mainfunc()
MainMen.mainfunc(screen)
# Rest of game code goes here...
terrain_gen = tgen.TerrainGenerator(
width=(-100, infoObject.current_w // 15), height=infoObject.current_h // 15
)
player_sprite = main()
player_sprite = main(screen)
terrain_gen.generate_terrain(screen)
player = pl.Player(vx, vy, infoObject.current_w - 40, 0)
DayTime = 0
Expand Down
Binary file modified src/SquarePixels/game/__pycache__/game.cpython-311.pyc
Binary file not shown.
8 changes: 7 additions & 1 deletion src/SquarePixels/game/game.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import SquarePixels.render.render as render
import pygame as pig

infoObject: object = pig.display.Info()
screen: pig.Surface = pig.display.set_mode(
(infoObject.current_w, infoObject.current_h - 32), pig.RESIZABLE | pig.OPENGL | pig.DOUBLEBUF
)

import SquarePixels.render.render as render
import SquarePixels.enemymanagement.enemy_manager as enemy_manager
from SquarePixels.uimanagement import death, MainMen
import SquarePixels.soundmanagement.music as music
Expand Down
Binary file modified src/SquarePixels/player/__pycache__/player.cpython-311.pyc
Binary file not shown.
Binary file modified src/SquarePixels/render/__pycache__/render.cpython-311.pyc
Binary file not shown.
105 changes: 92 additions & 13 deletions src/SquarePixels/render/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,62 @@
import math
from SquarePixels.render.weather_manager import weather_manager, draw_weather
import config
from array import array
import moderngl

hidden_area = []
hidden_area = None
infoObject: object = pig.display.Info()
# Fill the screen with black rectangles
for x in range(infoObject.current_w // 20 + 50):
for y in range(infoObject.current_h // 20 + 50):
black_rect = pig.Rect((x * 20, y * 20, 20, 20))
hidden_area.append(black_rect)

playerx = 0

ctx = moderngl.create_context()


quad_buffer = ctx.buffer(data=array('f', [
# position (x, y), uv coords (x, y)
-1.0, 1.0, 0.0, 0.0, # topleft
1.0, 1.0, 1.0, 0.0, # topright
-1.0, -1.0, 0.0, 1.0, # bottomleft
1.0, -1.0, 1.0, 1.0, # bottomright
]))

vert_shader = '''
#version 330 core
in vec2 vert;
in vec2 texcoord;
out vec2 uvs;
void main() {
uvs = texcoord;
gl_Position = vec4(vert, 0.0, 1.0);
}
'''

frag_shader = '''
#version 330 core
uniform sampler2D tex;
uniform float time;
in vec2 uvs;
out vec4 f_color;
void main() {
vec2 sample_pos = vec2(uvs.x + sin(uvs.y * 10 + time * 0.01) * 0.1, uvs.y);
f_color = vec4(texture(tex, sample_pos).rg, texture(tex, sample_pos).b * 1.5, 1.0);
}
'''

program = ctx.program(vertex_shader=vert_shader, fragment_shader=frag_shader)
render_object = ctx.vertex_array(program, [(quad_buffer, '2f 2f', 'vert', 'texcoord')])

def surf_to_texture(surf):
tex = ctx.texture(surf.get_size(), 4)
tex.filter = (moderngl.NEAREST, moderngl.NEAREST)
tex.swizzle = 'BGRA'
tex.write(surf.get_view('1'))
return tex

def render_terrain(
screen: pig.Surface,
Expand Down Expand Up @@ -49,7 +95,7 @@ def render_terrain(
Returns:
list: A list containing sky and colliders.
"""
global playerx
global playerx, hidden_area
tile_size = 15
block_images = [
r"Recources\Textures\grass.jpg",
Expand All @@ -76,7 +122,13 @@ def render_terrain(
place_blocks = [
13,
]

if not hidden_area:
hidden_area=[]
# Fill the screen with black rectangles
for x in range(abs(width[0])+width[1] // 20 + 50):
for y in range(height // 20 + 50):
black_rect = pig.Rect((x * 20, y * 20, 20, 20))
hidden_area.append(black_rect)
frame_count = 0 # Count frames for lightning duration
lightning_duration = 10 # Adjust the duration of the lightning effect

Expand All @@ -92,6 +144,7 @@ def render_terrain(
darkness = DayTime + 0.5
else:
darkness = DayTime
# terrain_surf = pig.Surface((abs(width[0])+width[1], height))
for x in range(width[0], width[1]):
for y in range(height):
block_type = terrain[y][x]
Expand Down Expand Up @@ -127,17 +180,34 @@ def render_terrain(
Darken = Darken * DayTime
color = (211 - Darken, 211 - Darken, 211 - Darken)
try:
terrain_surf = pig.Surface(((abs(width[0])+width[1], height)))
pig.draw.rect(
screen,
terrain_surf,
color,
(currentblock),
)
frame_tex = surf_to_texture(terrain_surf)
frame_tex.use(0)
program['tex'] = 0
#program['time'] = t
render_object.render(mode=moderngl.TRIANGLE_STRIP)

frame_tex.release()

except:
terrain_surf = pig.Surface(((abs(width[0])+width[1], height)))
pig.draw.rect(
screen,
(0, 0, 0),
terrain_surf,
(0,0,0),
(currentblock),
)
frame_tex = surf_to_texture(terrain_surf)
frame_tex.use(0)
program['tex'] = 0
#program['time'] = t
render_object.render(mode=moderngl.TRIANGLE_STRIP)

frame_tex.release()

## Load and blit the corresponding block image
# if block_type < len(block_images):
Expand All @@ -155,11 +225,20 @@ def render_terrain(
):
colliders.append(currentblock)
else:
terrain_surf = pig.Surface(((abs(width[0])+width[1], height)))
if block_type == 10:
screen.blit(block_images[2], currentblock)
terrain_surf.blit(block_images[2], currentblock)
if block_type == 11:
screen.blit(block_images[1], currentblock)
terrain_surf.blit(block_images[1], currentblock)
colliders.append(currentblock)
frame_tex = surf_to_texture(terrain_surf)
frame_tex.use(0)
program['tex'] = 0
#program['time'] = t
render_object.render(mode=moderngl.TRIANGLE_STRIP)
frame_tex.release()



draw_weather(screen) # TODO: #62 add weather setting

Expand Down
51 changes: 33 additions & 18 deletions src/SquarePixels/uimanagement/Character_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# Initialize Pygame
pygame.init()

background = None
# Initialize the screen with transparency
infoObject = pygame.display.Info()
WIDTH, HEIGHT = 800, 600 # infoObject.current_w, infoObject.current_h
Expand All @@ -31,21 +32,14 @@
root = tk.Tk()
root.withdraw() # Hide the main tkinter window

# Pygame screen
screen = pygame.display.set_mode((infoObject.current_w, infoObject.current_h))
pygame.display.set_caption("Character Customization")
pygame_icon = pygame.image.load(
r"Recources\program recources\Screenshot 2023-09-21 181742.png"
)
pygame.display.set_icon(pygame_icon)
background = pygame.Surface(screen.get_size(), pygame.SRCALPHA)

font = pygame.font.Font(None, 36)

# Initial position of the background
bg_x, bg_y = 0, 0

backround = pygame.transform.scale(
pygame.image.load(r"Recources\ui\mainmen\backround\cover.png"),
background = pygame.transform.scale(
pygame.image.load(r"Recources\ui\mainmen\background\cover.png"),
(infoObject.current_w + 100, infoObject.current_h + 80),
)

Expand Down Expand Up @@ -83,7 +77,12 @@
for idx in range(len(character_thumbnails))
]

screen = None

def get_screen(display):
global screen
screen = display

def draw_sidebar(buttoncount):
"""
Draw the sidebar with character thumbnails as buttons.
Expand Down Expand Up @@ -181,21 +180,37 @@ def add_shape(x, y, width, height, color):
shapes.append(shape)


def main():
def main(screen):
"""
Main game loop for character customization.
Returns:
None
"""
global head_size, body_height, trails, bg_x, bg_y
get_screen(screen)
running = True
buttoncount = 0
hoveredbuttons = []
drawchar = (False, 0)
back = pygame.transform.scale(
pygame.image.load(r"Recources\ui\mainmen\background\cover.png"),
(infoObject.current_w + 100, infoObject.current_h + 80),)
while running:
if trails:
screen.fill((0, 0, 0))
# Calculate the offset for the panoramic effect
x, y = pygame.mouse.get_pos()
offset_x = (infoObject.current_w / 2 - x) / 20
offset_y = (infoObject.current_h / 2 - y) / 20

# Apply smoothing to gradually move toward the target position
smoothing_factor = 0.1
bg_x += (offset_x - bg_x) * smoothing_factor
bg_y += (offset_y - bg_y) * smoothing_factor

# Blit the background with the calculated offset
screen.blit(back, (round(-50 - bg_x), round(-50 - bg_y)))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
Expand Down Expand Up @@ -246,7 +261,7 @@ def main():
bg_y += (offset_y - bg_y) * smoothing_factor

# Blit the background with the calculated offset
screen.blit(backround, (round(-50 - bg_x), round(-50 - bg_y)))
screen.blit(background, (round(-50 - bg_x), round(-50 - bg_y)))

if drawchar[0]:
char = character_thumbnails[drawchar[1]]
Expand Down Expand Up @@ -311,7 +326,7 @@ def main():
{
"rect": pygame.Rect(600, 350, 150, 50),
"text": "Finish",
"callback": lambda: finish(),
"callback": lambda: finish(screen),
},
{
"rect": pygame.Rect(600, 410, 150, 50),
Expand All @@ -331,7 +346,7 @@ def main():
{
"rect": pygame.Rect(600, 590, 150, 50),
"text": "Load Character",
"callback": lambda: load_character_dialog(),
"callback": lambda: load_character_dialog(screen),
},
]

Expand Down Expand Up @@ -381,7 +396,7 @@ def add_square():
add_shape(x, y, width, height, color)


def finish():
def finish(screen):
"""
Finish character customization and export the character.
Expand Down Expand Up @@ -449,7 +464,7 @@ def save_character_dialog():
export_character(filename)


def load_character_dialog():
def load_character_dialog(screen):
"""
Open a file dialog for loading a character image.
Expand All @@ -461,7 +476,7 @@ def load_character_dialog():
initialdir=path + r"\\Recources\\characters\\",
)
if filename:
load_character(filename)
load_character(filename, screen)


def export_character(filename):
Expand All @@ -478,7 +493,7 @@ def export_character(filename):
pygame.image.save(background, filename)


def load_character(filename):
def load_character(filename, screen):
"""
Load a character image from the specified file and display it.
Expand Down
Loading

0 comments on commit 9651dd4

Please sign in to comment.