commit aa0a0cd0d1833197a4feb675577b0c724bf923d2 Author: cecilkorik Date: Mon Jan 22 07:10:17 2007 +0000 initial import of "very simple space game" --HG-- branch : vssg diff --git a/framebg.png b/framebg.png new file mode 100644 index 0000000..16f5b0a Binary files /dev/null and b/framebg.png differ diff --git a/img/base_star.png b/img/base_star.png new file mode 100644 index 0000000..a574f65 Binary files /dev/null and b/img/base_star.png differ diff --git a/img/blue_dwarf1.png b/img/blue_dwarf1.png new file mode 100644 index 0000000..4fad37c Binary files /dev/null and b/img/blue_dwarf1.png differ diff --git a/img/green_dwarf1.png b/img/green_dwarf1.png new file mode 100644 index 0000000..9fda1c9 Binary files /dev/null and b/img/green_dwarf1.png differ diff --git a/img/red_dwarf1.png b/img/red_dwarf1.png new file mode 100644 index 0000000..bae460b Binary files /dev/null and b/img/red_dwarf1.png differ diff --git a/img/white_dwarf1.png b/img/white_dwarf1.png new file mode 100644 index 0000000..48efbee Binary files /dev/null and b/img/white_dwarf1.png differ diff --git a/img/yellow_dwarf1.png b/img/yellow_dwarf1.png new file mode 100644 index 0000000..8c3d334 Binary files /dev/null and b/img/yellow_dwarf1.png differ diff --git a/ships.py b/ships.py new file mode 100644 index 0000000..e69de29 diff --git a/starmap.py b/starmap.py new file mode 100644 index 0000000..a00bef7 --- /dev/null +++ b/starmap.py @@ -0,0 +1,72 @@ +import math +import pygame + +class Galaxy(object): + def __init__(self, width, height): + self.starmap = [[[] for x in range(width)] for x in range(height)] + self.starlist = [] + + def add_star(self, star): + self.starlist.append(star) + self.starmap[int(star.y / 100)][int(star.x / 100)].append(star) + + def get_distance(self, x, y, x2, y2): + return math.sqrt((x-x2)**2 + (y-y2)**2) + + def get_stars_in_sector(self, sx, sy): + return [star for star in self.starmap[sy][sx] if type(star) == StarSys] + + def get_all_stars(self): + return self.starlist + + def get_nearest_star(self, x, y): + mindist = None + minstar = None + for star in self.get_all_stars(): + stardist = self.get_distance(x, y, star.x, star.y) + if mindist == None or stardist < mindist: + mindist = stardist + minstar = star + + return minstar + + """ + star = None + radius = 0 + while True: + starlist = [] + self.get_stars_in_sector(x / 100, y / 100) + if + if star != None: + return star + else: + """ + +class StarSys(object): + def __init__(self, x, y, startype, habitat, minerals): + self.x = x + self.y = y + self.startype = startype + self.habitat = habitat + self.minerals = minerals + def draw(self, surf): + + self.startype.draw(surf, int(self.x / 10), int(self.y / 10)) + +class StarClass(object): + def __init__(self, name, color, size, habitat_mod, minerals_mod): + self.name = name + self.color = color + self.size = size + self.habitat_mod = habitat_mod + self.minerals_mod = minerals_mod + self.pngsurf = None + + def draw(self, surf, x, y): + if self.pngsurf == None: + self.pngsurf = pygame.image.load('img/%s_dwarf1.png' % (self.color.lower(),)) + self.pngsurf.convert_alpha(surf) + dest = self.pngsurf.get_rect() + dest.x = x - (dest.w / 2) + 1 + dest.y = y - (dest.h / 2) + 1 + surf.blit(self.pngsurf, dest) \ No newline at end of file diff --git a/starmap.pyc b/starmap.pyc new file mode 100644 index 0000000..041f984 Binary files /dev/null and b/starmap.pyc differ diff --git a/starmap_gen.py b/starmap_gen.py new file mode 100644 index 0000000..dce5928 --- /dev/null +++ b/starmap_gen.py @@ -0,0 +1,170 @@ +from starmap import * +import copy +import random + +class Starmap_Params(object): + def __init__(self): + self.density = 2 + self.minerals = Minerals_Params(0.1, 0.2, 0.4, 0.2, 0.1) + self.habitat = Habitat_Params(0.05, 0.03, 0.10, 0.07, 0.06, 0.09, 0.06, 0.09, 0.08, 0.08, 0.12, 0.08, 0.06, 0.08) + self.size = (80, 60) + +class PercentArray(object): + def __init__(self): + self.order = [] + self.array = {} + + def __setitem__(self, key, value): + if not self.array.has_key(key): + self.order += [key] + self.array[key] = value + + def __getitem__(self, key): + return self.array[key] + + def normalize(self): + tot = sum(self.array.values()) + + for k in self.array.keys(): + self[k] = self[k] / tot + + def __mul__(self, other): + if type(other) != type(self): + raise TypeError + retval = copy.deepcopy(self) + for k in retval.array.keys(): + retval[k] *= other[k] + retval.normalize() + return retval + + def random(self): + loopcount = 0 + while True: + loopcount += 1 + rv = random.random() + tot = 0.0 + for k in self.order: + tot += self[k] + if rv < tot: + return k + + if loopcount >= 5: + raise IndexError + + + +class Minerals_Params(PercentArray): + def __init__(self, vpoor, poor, normal, rich, vrich): + PercentArray.__init__(self) + self['vpoor'] = vpoor + self['poor'] = poor + self['normal'] = normal + self['rich'] = rich + self['vrich'] = vrich + + +class Habitat_Params(PercentArray): + def __init__(self, noplanet, paradise, terran, ocean, swamp, arid, desert, tundra, minimal, barren, dead, inferno, toxic, radiated): + PercentArray.__init__(self) + self['noplanet'] = noplanet + self['paradise'] = paradise + self['terran'] = terran + self['ocean'] = ocean + self['swamp'] = swamp + self['arid'] = arid + self['desert'] = desert + self['tundra'] = tundra + self['minimal'] = minimal + self['barren'] = barren + self['dead'] = dead + self['inferno'] = inferno + self['toxic'] = toxic + self['radiated'] = radiated + + + +startypes = [] + +whitedwarf_min = Minerals_Params(0.0, 0.2, 0.5, 2.0, 3.5) +whitedwarf_hab = Habitat_Params(3.0, 0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.8, 0.7, 1.5, 4.5, 1.0, 2.0, 2.0) +bluestar_min = Minerals_Params(0.5, 0.5, 1.0, 2.0, 2.5) +bluestar_hab = Habitat_Params(1.0, 0.4, 0.6, 0.6, 0.6, 0.9, 1.0, 0.6, 1.1, 1.2, 1.5, 3.0, 2.0, 3.0) +redstar_min = Minerals_Params(2.0, 2.0, 1.0, 0.4, 0.3) +redstar_hab = Habitat_Params(1.0, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.2, 1.0, 0.9, 0.8, 0.5, 0.5, 0.5) +yellowstar_min = Minerals_Params(1.0, 1.0, 1.0, 1.0, 1.0) +yellowstar_hab = Habitat_Params(1.0, 1.5, 1.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0) +greenstar_min = Minerals_Params(1.0, 1.0, 1.0, 1.5, 1.5) +greenstar_hab = Habitat_Params(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0) + + +startypes.append((0.15, StarClass(name="Blue Giant", color="Blue", size="Giant", minerals_mod=bluestar_min, habitat_mod=bluestar_hab))) +startypes.append((0.15, StarClass(name="Green Star", color="Green", size="Star", minerals_mod=greenstar_min, habitat_mod=greenstar_hab))) +startypes.append((0.25, StarClass(name="Yellow Star", color="Yellow", size="Star", minerals_mod=yellowstar_min, habitat_mod=yellowstar_hab))) +startypes.append((0.30, StarClass(name="Red Dwarf", color="Red", size="Dwarf", minerals_mod=redstar_min, habitat_mod=redstar_hab))) +startypes.append((0.10, StarClass(name="Red Giant", color="Red", size="Giant", minerals_mod=redstar_min, habitat_mod=redstar_hab))) +startypes.append((0.05, StarClass(name="White Dwarf", color="White", size="Dwarf", minerals_mod=whitedwarf_min, habitat_mod=whitedwarf_hab))) + + + +def starmap_generate_star(map): + pass + +def starmap_generate(): + params = Starmap_Params() + numstars = int(round((params.size[0] * params.size[1] / 100.0) * params.density)) + mindist = None + mapx = params.size[0] - 2 + mapy = params.size[1] - 2 + map = Galaxy(params.size[0], params.size[1]) + i = 0 + print numstars + while True: + if i >= numstars: + break + + if i != 0: + mindist = max(175.0, (mapx + mapy) / 2.0 * 100.0 / ((i + 1)**0.75*0.75)) + + x = (random.random() * mapx) + 1 + y = (random.random() * mapy) + 1 + x *= 100 + y *= 100 + + if mindist != None: + star = map.get_nearest_star(x, y) + dist = map.get_distance(x, y, star.x, star.y) + if dist < mindist: + print "Dist (%s, %s): %s, Mindist (%s, %s): %s" % (star.x, star.y, dist, int(x), int(y), mindist) + continue + + loopcount = 0 + startype = None + while True: + loopcount += 1 + rv = random.random() + tot = 0.0 + for pct, startype_test in startypes: + tot += pct + if rv < tot: + startype = startype_test + break + + if startype != None: + break + + if loopcount >= 5: + raise IndexError + + + hab = params.habitat * startype.habitat_mod + min = params.minerals * startype.minerals_mod + hab = hab.random() + min = min.random() + + print "Generated star %s %s %s at (%s, %s)" % (startype.name, hab, min, int(x), int(y)) + + star = StarSys(int(x), int(y), startype, hab, min) + map.add_star(star) + + i += 1 + return map \ No newline at end of file diff --git a/starmap_gen.pyc b/starmap_gen.pyc new file mode 100644 index 0000000..5340f37 Binary files /dev/null and b/starmap_gen.pyc differ diff --git a/testui.py b/testui.py new file mode 100644 index 0000000..fcb380e --- /dev/null +++ b/testui.py @@ -0,0 +1,72 @@ +#import testopt +import time +import pyui +import pyui.themes.win2k + +class MyFrame(object): + """A frame is a window that has a titlebar and borders. it is resizable and movable by dragging the titlebar. + """ + def __init__(self, x, y, w, h, title, topmost = 0): + self.theme = getTheme() + self.innerWidth = w + self.innerHeight = h + self.title = title + + self.panelOffsetLeft = 0 + self.panelOffsetTop = 0 + self.panelOffsetRight = 0 + self.panelOffsetBottom = 0 + + Window.__init__(self, x, y, w, h, topmost) + self.setTitle(self.title) + + self.panelOffsetLeft = self.theme.getFrameBorderLeft() + self.panelOffsetTop = self.theme.getFrameBorderTop() + self.panelOffsetRight = self.theme.getFrameBorderRight() + self.panelOffsetBottom = self.theme.getFrameBorderBottom() + + self._panel.moveto(self.panelOffsetLeft, self.panelOffsetTop) + w += self.panelOffsetLeft + self.panelOffsetRight + h += self.panelOffsetTop + self.panelOffsetBottom + self.backImage = None + + def setBackImage(self, filename): + self.backImage = filename + #getRenderer().loadImage(filename) + + + def draw(self, renderer): + """Draws to the actual frame if the renderer requires it. + """ + if not self.show: + return + self.hitList = getTheme().drawFrame( (0,0,self.width, self.height), self.title, self.backImage) + Window.draw(self, renderer) + +def onbutton(self): + print "got a button " + +def run(): + #opts = testopt.parseCommandLine(800, 600) + done = 1 + frame = 0 + t = time.time() + desktop = pyui.init(640, 480, '2d') + print pyui.core.gRenderer.screen + newtheme = pyui.themes.win2k.Win2kTheme(pyui.core.gRenderer, "lucida sans unicode", 14) + desktop.setTheme(newtheme) + + w = pyui.widgets.Frame(50, 50, 400, 400, "clipme") + b = pyui.widgets.Button( "A button is here", onbutton) + w.addChild(b) + w.pack() + + w.setBackImage("framebg.png") + pyui.run() + + print "done" + pyui.quit() + + +if __name__ == '__main__': + run() diff --git a/testui2.py b/testui2.py new file mode 100644 index 0000000..94a92e9 --- /dev/null +++ b/testui2.py @@ -0,0 +1,15 @@ +# Hello World example. +from ocempgui.widgets import * + +# Initialize the drawing window. +re = Renderer () +re.create_screen (640, 480) +re.title = "Hello World" +re.color = (250, 250, 250) + +button = Button ("Hello World") +button.topleft = (10, 10) +re.add_widget (button) + +# Start the main rendering loop. +re.start() \ No newline at end of file diff --git a/vssg.py b/vssg.py new file mode 100644 index 0000000..c3c5880 --- /dev/null +++ b/vssg.py @@ -0,0 +1,124 @@ +import pygame +from pygame.locals import * +import os +import sys +import math +import random +import starmap +import starmap_gen + +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 = starmap_gen.starmap_generate() + +while True: + events = pygame.event.get((KEYDOWN, KEYUP, QUIT)) + 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 + else: + keys += [ev.key] + + if do_quit: + break + + handle_keys(keys) + pygame.event.pump() + """ + loc_new = move(loc_player, dir_player, speed_player) + + if loc_new[0] < 5: + if not bounce: + loc_new[0] = width-6 + else: + dir_player = bouncedir(math.pi*0.5, dir_player) + elif loc_new[1] < 5: + if not bounce: + loc_new[1] = height-6 + else: + dir_player = bouncedir(math.pi, dir_player) + elif loc_new[0] > width - 5: + if not bounce: + loc_new[0] = 6 + else: + dir_player = bouncedir(math.pi*1.5, dir_player) + elif loc_new[1] > height - 5: + if not bounce: + loc_new[1] = 6 + else: + dir_player = bouncedir(0, dir_player) + + if bounce: + loc_new = move(loc_player, dir_player, speed_player) + + if not trails: + pygame.draw.circle(scr, (0,0,0), loc_player, 2, 1) + pygame.draw.circle(scr, color_player, loc_new, 2, 1) + + + loc_player = loc_new + """ + + for star in map.get_all_stars(): + star.draw(scr) + + + pygame.display.flip() \ No newline at end of file