@ -7,7 +7,7 @@ from ctypes import *
from PIL import Image
from PIL import Image
from sdl2 import SDL_AudioSpec
from sdl2 import SDL_AudioSpec
import random
class GameWindow :
class GameWindow :
def __init__ ( self , width , height , cell_size , title = " Default " , key_callback = None ) :
def __init__ ( self , width , height , cell_size , title = " Default " , key_callback = None ) :
@ -23,6 +23,7 @@ class GameWindow:
self . h_offset = self . h_start_offset
self . h_offset = self . h_start_offset
self . max_w_offset = self . target_size [ 0 ] - self . width
self . max_w_offset = self . target_size [ 0 ] - self . width
self . max_h_offset = self . target_size [ 1 ] - self . height
self . max_h_offset = self . target_size [ 1 ] - self . height
self . scale = self . target_size [ 1 ] / / self . cell_size
print ( f " Screen size: { self . width } x { self . height } " )
print ( f " Screen size: { self . width } x { self . height } " )
sdl2 . ext . init ( joystick = True )
sdl2 . ext . init ( joystick = True )
sdl2 . SDL_Init ( sdl2 . SDL_INIT_AUDIO )
sdl2 . SDL_Init ( sdl2 . SDL_INIT_AUDIO )
@ -37,11 +38,12 @@ class GameWindow:
self . running = True
self . running = True
self . key_down , self . key_up , self . axis_scroll = key_callback
self . key_down , self . key_up , self . axis_scroll = key_callback
self . performance = 0
self . performance = 0
self . audio_devs = [ ]
self . audio_devs = { }
#for i in range(5):
self . button_cursor = [ 0 , 0 ]
# self.audio_devs.append((True, sdl2.SDL_OpenAudioDevice(None, 0, SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048), None, 0)))
self . buttons = { }
self . audio_devs [ " base " ] = sdl2 . SDL_OpenAudioDevice ( None , 0 , SDL_AudioSpec ( freq = 22050 , aformat = sdl2 . AUDIO_U8 , channels = 1 , samples = 2048 ) , None , 0 )
self . audio_dev = sdl2 . SDL_OpenAudioDevice ( None , 0 , SDL_AudioSpec ( freq = 22050 , aformat = sdl2 . AUDIO_U8 , channels = 1 , samples = 2048 ) , None , 0 )
self . audio_devs [ " effects " ] = sdl2 . SDL_OpenAudioDevice ( None , 0 , SDL_AudioSpec ( freq = 22050 , aformat = sdl2 . AUDIO_U8 , channels = 1 , samples = 2048 ) , None , 0 )
self . audio_devs [ " music " ] = sdl2 . SDL_OpenAudioDevice ( None , 0 , SDL_AudioSpec ( freq = 22050 , aformat = sdl2 . AUDIO_U8 , channels = 1 , samples = 2048 ) , None , 0 )
def create_texture ( self , tiles : list ) :
def create_texture ( self , tiles : list ) :
bg_surface = sdl2 . SDL_CreateRGBSurface ( 0 , self . width , self . height , 32 , 0 , 0 , 0 , 0 )
bg_surface = sdl2 . SDL_CreateRGBSurface ( 0 , self . width , self . height , 32 , 0 , 0 , 0 , 0 )
for tile in tiles :
for tile in tiles :
@ -97,8 +99,8 @@ class GameWindow:
sprite . position = ( x + self . w_offset , y + self . h_offset )
sprite . position = ( x + self . w_offset , y + self . h_offset )
self . renderer . copy ( sprite , dstrect = sprite . position )
self . renderer . copy ( sprite , dstrect = sprite . position )
def draw_rectangle ( self , x , y , width , height , tag , outline = " red " , filling = None ) :
def draw_rectangle ( self , x , y , width , height , tag , outline = " red " , filling = None ) :
x , y = x + self . w_offset , y + self . h_offset
if filling :
if filling :
self . renderer . fill ( ( x , y , width , height ) , sdl2 . ext . Color ( * filling ) )
self . renderer . fill ( ( x , y , width , height ) , sdl2 . ext . Color ( * filling ) )
else :
else :
@ -113,8 +115,8 @@ class GameWindow:
def delete_tag ( self , tag ) :
def delete_tag ( self , tag ) :
pass
pass
def win_screen ( self , text , * * kwargs ) :
def dialog ( self , text , * * kwargs ) :
self . draw_rectangle ( 50 - self . w_offset , 50 - self . h_offset ,
self . draw_rectangle ( 50 , 50 ,
self . target_size [ 0 ] - 100 , self . target_size [ 1 ] - 100 , " win " , filling = ( 255 , 255 , 255 ) )
self . target_size [ 0 ] - 100 , self . target_size [ 1 ] - 100 , " win " , filling = ( 255 , 255 , 255 ) )
self . draw_text ( text , self . fonts [ self . target_size [ 1 ] / / 20 ] , " center " , sdl2 . ext . Color ( 0 , 0 , 0 ) )
self . draw_text ( text , self . fonts [ self . target_size [ 1 ] / / 20 ] , " center " , sdl2 . ext . Color ( 0 , 0 , 0 ) )
if image := kwargs . get ( " image " ) :
if image := kwargs . get ( " image " ) :
@ -133,11 +135,7 @@ class GameWindow:
sprite_score . position = ( self . target_size [ 0 ] / / 2 - 50 - sprite_score . size [ 0 ] / / 4 ,
sprite_score . position = ( self . target_size [ 0 ] / / 2 - 50 - sprite_score . size [ 0 ] / / 4 ,
self . target_size [ 1 ] / / 2 + 60 + 20 * ( i + 1 ) )
self . target_size [ 1 ] / / 2 + 60 + 20 * ( i + 1 ) )
self . renderer . copy ( sprite_score , dstrect = sprite_score . position )
self . renderer . copy ( sprite_score , dstrect = sprite_score . position )
def pause_screen ( self , text ) :
self . draw_rectangle ( 50 - self . w_offset , 50 - self . h_offset ,
self . target_size [ 0 ] - 100 , self . target_size [ 1 ] - 100 , " pause " , filling = ( 255 , 255 , 255 ) )
self . draw_text ( text , self . fonts [ self . target_size [ 1 ] / / 20 ] , " center " , sdl2 . ext . Color ( 0 , 0 , 0 ) )
def get_image_size ( self , image ) :
def get_image_size ( self , image ) :
return image . size
return image . size
@ -159,6 +157,9 @@ class GameWindow:
def is_in_visible_area ( self , x , y ) :
def is_in_visible_area ( self , x , y ) :
return - self . w_offset - self . cell_size < = x < = self . width - self . w_offset and - self . h_offset - self . cell_size < = y < = self . height - self . h_offset
return - self . w_offset - self . cell_size < = x < = self . width - self . w_offset and - self . h_offset - self . cell_size < = y < = self . height - self . h_offset
def get_perf_counter ( self ) :
return sdl2 . SDL_GetPerformanceCounter ( )
def mainloop ( self , * * kwargs ) :
def mainloop ( self , * * kwargs ) :
while self . running :
while self . running :
performance_start = sdl2 . SDL_GetPerformanceCounter ( )
performance_start = sdl2 . SDL_GetPerformanceCounter ( )
@ -228,7 +229,7 @@ class GameWindow:
self . w_offset = x
self . w_offset = x
self . h_offset = y
self . h_offset = y
def play_sound ( self , sound_file ) :
def play_sound ( self , sound_file , tag = " base " ) :
print ( sound_file )
print ( sound_file )
sound_file = os . path . join ( " sound " , sound_file )
sound_file = os . path . join ( " sound " , sound_file )
rw = sdl2 . SDL_RWFromFile ( byteify ( sound_file , " utf-8 " ) , b " rb " )
rw = sdl2 . SDL_RWFromFile ( byteify ( sound_file , " utf-8 " ) , b " rb " )
@ -237,12 +238,12 @@ class GameWindow:
_buf = POINTER ( sdl2 . Uint8 ) ( )
_buf = POINTER ( sdl2 . Uint8 ) ( )
_length = sdl2 . Uint32 ( )
_length = sdl2 . Uint32 ( )
spec = SDL_AudioSpec ( freq = 22050 , aformat = sdl2 . AUDIO_U8 , channels = 1 , samples = 2048 )
spec = SDL_AudioSpec ( freq = 22050 , aformat = sdl2 . AUDIO_U8 , channels = 1 , samples = 2048 )
if sdl2 . SDL_LoadWAV_RW ( rw , 1 , byref ( spec ) , byref ( _buf ) , byref ( _length ) ) == None :
if sdl2 . SDL_LoadWAV_RW ( rw , 1 , byref ( spec ) , byref ( _buf ) , byref ( _length ) ) == None :
raise RuntimeError ( " Failed to load WAV " )
raise RuntimeError ( " Failed to load WAV " )
devid = self . audio_dev
devid = self . audio_devs [ tag ]
sdl2 . SDL_ClearQueuedAudio ( devid )
# Clear any queued audio
sdl2 . SDL_ClearQueuedAudio ( devid )
sdl2 . SDL_ClearQueuedAudio ( devid )
# Start playing audio
# Start playing audio
@ -255,4 +256,16 @@ class GameWindow:
sdl2 . SDL_PauseAudioDevice ( dev [ 1 ] , 1 )
sdl2 . SDL_PauseAudioDevice ( dev [ 1 ] , 1 )
sdl2 . SDL_ClearQueuedAudio ( dev [ 1 ] )
sdl2 . SDL_ClearQueuedAudio ( dev [ 1 ] )
def start_dialog ( self , * * kwargs ) :
self . dialog ( " Welcome to the Mice! " , * * kwargs )
center = self . get_view_center ( )
self . draw_button ( center [ 0 ] , center [ 1 ] + 10 * self . scale , " Start " , 120 , 50 , ( 0 , 0 ) )
def draw_button ( self , x , y , text , width , height , coords ) :
if self . button_cursor [ 0 ] == coords [ 0 ] and self . button_cursor [ 1 ] == coords [ 1 ] :
color = ( 0 , 0 , 255 )
self . draw_rectangle ( x , y , width , height , " button " , outline8u = color )
self . draw_text ( text , self . fonts [ 20 ] , ( x + 10 , y + 10 ) , ( 0 , 0 , 0 ) )
def get_view_center ( self ) :
return self . w_offset + self . width / / 2 , self . h_offset + self . height / / 2