#!/usr/bin/python import pygame from pygame.locals import * import os import sys import math import random import starmap import starmap_gen import gametimer #from ocempgui.widgets import * #from ocempgui.widgets.Constants import * import video pygame.init() def handle_keys(keys): global bounce, speed_player, trails, dir_player, color_player for key in keys: if key == K_b: bounce = not bounce elif key == K_q: sys.exit(0) elif key == K_c: color_player = (random.randint(0,255), random.randint(0,255), random.randint(0,255)) elif key == K_t: trails = not trails elif key == K_UP: speed_player += 0.01 elif key == K_DOWN: speed_player -= 0.01 elif key == K_LEFT: dir_player -= (math.pi / 180.0) elif key == K_RIGHT: dir_player += (math.pi / 180.0) def move(coords, dir, mag): return [coords[0] + (math.sin(dir) * mag), coords[1] - (math.cos(dir) * mag)] def limitdir(dir): while dir < 0: dir += math.pi*2.0 while dir > math.pi*2.0: dir -= math.pi*2.0 return dir def bouncedir(normal, dir): inv_normal = limitdir(normal + math.pi) return limitdir(normal + (inv_normal - dir)) loc_player = [100.0,150.0] dir_player = math.pi * 0.80 size = width, height = (800,600) scr = pygame.display.set_mode(size) #options color_player = (255,255,255) bounce = True speed_player = 5.0 trails = False #scr.fill((0,0,0)) keys = [] do_quit = False map = None pygame.event.set_allowed((KEYDOWN, KEYUP, QUIT)) sg = False def btn_start_game(): global guiscreen, map guiscreen = None map = starmap_gen.starmap_generate() def btn_quit_game(): pygame.event.post(pygame.event.Event(QUIT, {})) def btn_game_options(): global guiscreen guiscreen = screen_options() def screen_mainmenu(): re = Renderer() re.screen = scr re.title = "vssg 0.1 - Very Simple Space Game" re.color = (40,0,70) b_start = Button("#Start Game") b_options = Button("#Options") b_quit = Button("#Quit") blist = [b_start, b_options, b_quit] totheight = (b_start.height * len(blist)) + (5 * (len(blist) - 1)) maxwidth = 0 for button in blist: if maxwidth == None or button.width > maxwidth: maxwidth = button.width b_start.connect_signal(SIG_CLICKED, btn_start_game) b_quit.connect_signal(SIG_CLICKED, btn_quit_game) b_options.connect_signal(SIG_CLICKED, btn_game_options) for i, button in enumerate(blist): x = scr.get_rect().w / 2 y = scr.get_rect().h / 2 y = y - (totheight / 2) + (b_start.height / 2) y = y + (i * 5) + (i * b_start.height) button.set_minimum_size(maxwidth, b_start.height) button.center = (x, y) re.add_widget(button) return re def screen_options(): def slider_set_resolution(): global resolution_list, s_res, l_res new_res = resolution_list[int(s_res.value)] l_res.set_text("%s x %s" % (new_res[0], new_res[1])) def btn_ok(): global guiscreen, s_res, resolution_list, scr guiscreen = None new_res = resolution_list[int(s_res.value)] scr = pygame.display.set_mode(new_res) guiscreen = screen_mainmenu() def btn_apply(): global s_res, resolution_list, scr, guiscreen guiscreen = None new_res = resolution_list[int(s_res.value)] scr = pygame.display.set_mode(new_res) guiscreen = screen_options() def btn_cancel(): global guiscreen guiscreen = screen_mainmenu() def set_resolution(): global guiscreen global resolution_list, l_res, s_res re = Renderer() re.screen = scr re.color = (40,0,70) optframe = VFrame() optframe.spacing = 10 resolution_list = pygame.display.list_modes() resolution_list.reverse() cur_res = (scr.get_rect().w, scr.get_rect().h) try: cur_res_idx = resolution_list.index(cur_res) except ValueError: cur_res_idx = None s_res = HScale(0, len(resolution_list) - 1) s_res.set_minimum_size(300, s_res.size[1]) #s_res.center = (cur_res[0] / 2, (cur_res[1] / 2) - (s_res.size[1] / 2) - 2) if cur_res_idx != None: s_res.value = cur_res_idx s_res.connect_signal(SIG_VALCHANGED, slider_set_resolution) l_res = Label("%s x %s" % (cur_res[0], cur_res[1])) #l_res.center = (cur_res[0] / 2, (cur_res[1] / 2) + (l_res.size[1] / 2) + 2) optframe.add_child(s_res) optframe.add_child(l_res) btnframe = HFrame() btnframe.border = BORDER_NONE btnframe.spacing = 5 b_ok = Button("#OK") b_apply = Button("#Apply") b_cancel = Button("#Cancel") b_ok.connect_signal(SIG_CLICKED, btn_ok) b_apply.connect_signal(SIG_CLICKED, btn_apply) b_cancel.connect_signal(SIG_CLICKED, btn_cancel) btnframe.add_child(b_ok) btnframe.add_child(b_apply) btnframe.add_child(b_cancel) optframe.add_child(btnframe) optframe.center = (cur_res[0] / 2, cur_res[1] / 2) re.add_widget(optframe) return re guiscreen = None map = starmap_gen.starmap_generate() gametimer.start_loop() while True: events = pygame.event.get() gametimer.next_frame() if guiscreen != None: video.force_redraw() # irrelevant right now, but when we exit, we'll need a full redraw! for ev in events: if ev.type == QUIT: do_quit = True break if do_quit: break guiscreen.distribute_events(*events) continue for ev in events: if ev.type == QUIT: do_quit = True break elif ev.type == KEYUP: try: del keys[keys.index(ev.key)] except IndexError: pass elif ev.type == KEYDOWN: keys += [ev.key] elif ev.type == VIDEOEXPOSE: video.force_redraw() elif ev.type in (MOUSEBUTTONDOWN, MOUSEBUTTONUP): pass if do_quit: break handle_keys(keys) map.update() if video.get_forced_redraw(): scr.fill((0,0,0)) map.draw(scr) video.next_frame()