Games are very often highly interactive with even some text adventures operating in real time. I thought it would be fun to go in the opposite direction and create a game with minimal interaction. So I settled on an outer space theme and had a text experiment of a probe travelling through the emptiness of space.
The next step was to create a graphical display and, of course, stars were the first order of business! I looked around at a few star field examples and ended up coding one for myself. Straightforward 2D scrolling is all I want - needs to be calm for the display. The class is nice and simple whilst the demo shows it off fairly well.
Exercise for the reader - can you make a layered demo? I'll share the code on a follow up post. Happy stargazing!
The next step was to create a graphical display and, of course, stars were the first order of business! I looked around at a few star field examples and ended up coding one for myself. Straightforward 2D scrolling is all I want - needs to be calm for the display. The class is nice and simple whilst the demo shows it off fairly well.
Exercise for the reader - can you make a layered demo? I'll share the code on a follow up post. Happy stargazing!
importrandom
importpygame
importsys
frompygame.localsimport*
classstarfield(object):
def__init__(self,pos,size,max,speed):
self.stars=[]
self.pos=pos
self.size=size
self.max=max
self.speed=speed
self.box=True
self.color=(255,255,255)
self.bordercolor=Color(255,255,255)
self.backgroundcolor=Color(0,0,0)
forloopinrange(0,max):
star=[random.randrange(0,size[0]-1),
random.randrange(0,size[1]-1)]
self.stars.append(star);
defdraw(self,screen):
ifself.box:
pygame.draw.rect(screen,self.backgroundcolor,Rect(self.pos[0],self.pos[1],self.size[0],self.size[1]),0)
pygame.draw.rect(screen,self.bordercolor,Rect(self.pos[0],self.pos[1],self.size[0],self.size[1]),1)
forloopinrange(0,self.max):
p=(self.pos[0]+self.stars[loop][0],self.pos[1]+self.stars[loop][1])
screen.set_at(p,self.color)
defupdate(self):
forloopinrange(0,self.max):
self.stars[loop]=(self.stars[loop][0]+self.speed,self.stars[loop][1])
ifself.stars[loop][0]>self.size[0]:
self.stars[loop]=(0,self.stars[loop][1])
# DEMO
sf=[starfield((50,50),(550,410),80,1),
starfield((80,150),(200,200),33,2),
starfield((380,150),(200,200),33,3),
starfield((5,5),(80,80),12,4)
]
sf[1].color=(255,0,0)
sf[2].color=(0,255,0)
sf[3].color=(0,0,255)
TIMEREVENT=pygame.USEREVENT
UPDATEEVENT=pygame.USEREVENT+1
FPS=50
pygame.init()
window=pygame.display.set_mode((640,480))
background=None
background=pygame.Surface(window.get_size())
background.fill((0,0,0))
pygame.time.set_timer(TIMEREVENT,int(1000/FPS))
pygame.time.set_timer(UPDATEEVENT,50)
whileTrue:
foreventinpygame.event.get():
ifevent.type==TIMEREVENT:
background.fill((0,0,0))
forsinsf:
s.draw(background)
window.blit(background,(0,0))
pygame.display.flip()
elifevent.type==UPDATEEVENT:
forsinsf:
s.update()
# Move starfield
x=sf[3].pos[0]+2
y=sf[3].pos[1]+2
ifx>640:x=-50
ify>480:y=-40
sf[3].pos=(x,y)
elifevent.type==pygame.QUIT:
sys.exit()