vssg/starmap.py
2007-01-22 07:10:17 +00:00

72 lines
No EOL
1.8 KiB
Python

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)