Aggiungi file di configurazione per i binding dei tasti e implementa la gestione degli input nel gioco
This commit is contained in:
+15
-18
@@ -5,37 +5,34 @@ import random
|
||||
|
||||
class KeyBindings:
|
||||
def key_pressed(self, key, coords=None):
|
||||
|
||||
#print(key)
|
||||
if key == "Q" or key == 12:
|
||||
if key in self.keybindings["quit"]:
|
||||
self.engine.close()
|
||||
elif key == "Return" or key == 13:
|
||||
elif key in self.keybindings["new_rat"]:
|
||||
self.new_rat()
|
||||
elif key == "D":
|
||||
elif key in self.keybindings["kill_rat"]:
|
||||
if self.units:
|
||||
self.units[random.choice(list(self.units.keys()))].die(score=5)
|
||||
elif key == "M":
|
||||
elif key in self.keybindings["toggle_audio"]:
|
||||
self.audio = not self.audio
|
||||
elif key == "F":
|
||||
elif key in self.keybindings["toggle_full_screen"]:
|
||||
self.full_screen = not self.full_screen
|
||||
self.engine.full_screen(self.full_screen)
|
||||
elif key == "Up" or key == 8:
|
||||
elif key in self.keybindings["scroll_up"]:
|
||||
self.start_scrolling("Up")
|
||||
elif key == "Down" or key == 9:
|
||||
elif key in self.keybindings["scroll_down"]:
|
||||
self.start_scrolling("Down")
|
||||
elif key == "Left" or key == 10:
|
||||
elif key in self.keybindings["scroll_left"]:
|
||||
self.start_scrolling("Left")
|
||||
elif key == "Right" or key == 11:
|
||||
elif key in self.keybindings["scroll_right"]:
|
||||
self.start_scrolling("Right")
|
||||
elif key == "Space" or key == 1:
|
||||
self.play_sound("PUTDOWN.WAV")
|
||||
elif key in self.keybindings["spawn_bomb"]:
|
||||
self.play_sound("PUTDOWN.WAV", tag="effects")
|
||||
self.spawn_bomb(self.pointer)
|
||||
elif key == "P" or key == 16:
|
||||
elif key in self.keybindings["pause"]:
|
||||
self.pause = not self.pause
|
||||
# elif key == "mouse":
|
||||
# adjusted_coords = (coords[0]//self.cell_size*2, coords[1]//self.cell_size*2)
|
||||
# self.pointer = adjusted_coords
|
||||
# self.scroll_cursor()
|
||||
|
||||
def quit_game(self):
|
||||
self.engine.close()
|
||||
def key_released(self, key):
|
||||
if key in ["Up", "Down", "Left", "Right", 8, 9, 10, 11]:
|
||||
self.stop_scrolling()
|
||||
|
||||
+32
-19
@@ -7,7 +7,7 @@ from ctypes import *
|
||||
from PIL import Image
|
||||
from sdl2 import SDL_AudioSpec
|
||||
|
||||
import random
|
||||
|
||||
class GameWindow:
|
||||
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.max_w_offset = self.target_size[0] - self.width
|
||||
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}")
|
||||
sdl2.ext.init(joystick=True)
|
||||
sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO)
|
||||
@@ -37,11 +38,12 @@ class GameWindow:
|
||||
self.running = True
|
||||
self.key_down, self.key_up, self.axis_scroll = key_callback
|
||||
self.performance = 0
|
||||
self.audio_devs = []
|
||||
#for i in range(5):
|
||||
# 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.audio_dev = sdl2.SDL_OpenAudioDevice(None, 0, SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048), None, 0)
|
||||
self.audio_devs = {}
|
||||
self.button_cursor = [0, 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_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):
|
||||
bg_surface = sdl2.SDL_CreateRGBSurface(0, self.width, self.height, 32, 0, 0, 0, 0)
|
||||
for tile in tiles:
|
||||
@@ -97,8 +99,8 @@ class GameWindow:
|
||||
sprite.position = (x+self.w_offset, y+self.h_offset)
|
||||
self.renderer.copy(sprite, dstrect=sprite.position)
|
||||
|
||||
|
||||
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:
|
||||
self.renderer.fill((x, y, width, height), sdl2.ext.Color(*filling))
|
||||
else:
|
||||
@@ -113,8 +115,8 @@ class GameWindow:
|
||||
def delete_tag(self, tag):
|
||||
pass
|
||||
|
||||
def win_screen(self, text, **kwargs):
|
||||
self.draw_rectangle(50 - self.w_offset, 50 - self.h_offset,
|
||||
def dialog(self, text, **kwargs):
|
||||
self.draw_rectangle(50, 50,
|
||||
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))
|
||||
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,
|
||||
self.target_size[1] // 2 + 60 + 20 * (i + 1))
|
||||
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):
|
||||
return image.size
|
||||
|
||||
@@ -159,6 +157,9 @@ class GameWindow:
|
||||
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
|
||||
|
||||
def get_perf_counter(self):
|
||||
return sdl2.SDL_GetPerformanceCounter()
|
||||
|
||||
def mainloop(self, **kwargs):
|
||||
while self.running:
|
||||
performance_start = sdl2.SDL_GetPerformanceCounter()
|
||||
@@ -228,7 +229,7 @@ class GameWindow:
|
||||
self.w_offset = x
|
||||
self.h_offset = y
|
||||
|
||||
def play_sound(self, sound_file):
|
||||
def play_sound(self, sound_file, tag="base"):
|
||||
print(sound_file)
|
||||
sound_file = os.path.join("sound", sound_file)
|
||||
rw = sdl2.SDL_RWFromFile(byteify(sound_file, "utf-8"), b"rb")
|
||||
@@ -237,12 +238,12 @@ class GameWindow:
|
||||
|
||||
_buf = POINTER(sdl2.Uint8)()
|
||||
_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:
|
||||
raise RuntimeError("Failed to load WAV")
|
||||
devid = self.audio_dev
|
||||
sdl2.SDL_ClearQueuedAudio(devid)
|
||||
devid = self.audio_devs[tag]
|
||||
# Clear any queued audio
|
||||
sdl2.SDL_ClearQueuedAudio(devid)
|
||||
|
||||
# Start playing audio
|
||||
@@ -255,4 +256,16 @@ class GameWindow:
|
||||
sdl2.SDL_PauseAudioDevice(dev[1], 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
|
||||
Reference in New Issue
Block a user