vssg/starmap.py
cecilkorik 04e12b7eb9 redid star images
started to add rudimentary animation support

--HG--
branch : vssg
2007-01-24 08:50:29 +00:00

91 lines
No EOL
2.3 KiB
Python

import math
import pygame
import random
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):
frame = random.random()
if frame > 0.5:
frame = 1
else:
frame = 2
self.startype.draw(surf, int(self.x / 10), int(self.y / 10), frame)
def set_frame_timers(self, show1, show2):
self.show1 = show1
self.show2 = show2
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.png1 = None
self.png2 = None
def draw(self, surf, x, y, frame):
if self.png1 == None:
self.png1 = pygame.image.load('img/%s_%s1.png' % (self.color.lower(), self.size.lower()))
self.png1.convert_alpha(surf)
self.png2 = pygame.image.load('img/%s_%s2.png' % (self.color.lower(), self.size.lower()))
self.png2.convert_alpha(surf)
if frame == 1:
dest = self.png1.get_rect()
dest.x = x - (dest.w / 2) + 1
dest.y = y - (dest.h / 2) + 1
surf.blit(self.png1, dest)
else:
dest = self.png2.get_rect()
dest.x = x - (dest.w / 2) + 1
dest.y = y - (dest.h / 2) + 1
surf.blit(self.png2, dest)