import pygame, math, random,time,os, sys, pickle from pygame.locals import * BGCOLOR = [10,10,30] angle = math.pi * 0 colors = colors=[[ [4,109,4], [190,0,30], [35,75,225], [225,185,0],\ [108,13,196], [210,200,110], [105,176,205], [170,170,170],\ [170,220,170], [220,170,220], [200,200,250], [250,250,200],\ [250,200,250], [250,50,200], [200,250,250], [255,255,255],\ ],\ [[34,139,34], [220,20,60], [65,105,255], [255,215,0],\ [138,43,226], [240,230,140], [135,206,235], [200,200,200],\ [200,250,200], [250,200,250], [200,200,250], [250,250,200],\ [250,200,250], [250,50,200], [200,250,250], [255,255,255],\ ],\ [[64,169,64], [250,50,90], [95,135,255], [255,225,10],\ [168,73,256], [255,255,170], [165,236,265], [230,230,230],\ [230,253,230], [255,230,255], [200,200,250], [250,250,200],\ [250,200,250], [250,50,200], [200,250,250], [255,255,255],\ ]] ##Functions #Sub functions def distance(vector): """Distance formula""" return (vector[0]**2 + vector[1]**2)**0.5 def theta(vector): return math.atan2(vector[1],vector[0]) def normal(vector): """Normalize a vector into a unit vector""" magna=distance(vector) if magna != 0: return [vector[0]/magna,vector[1]/magna] else: return [0,0] #functions to conver from one grid to another. def oddgrid(center): """Take a coordinate and rotate it""" global angle newcenter = [center[0] - 320,center[1] - 480] ##set earth to origin magnitude=distance(newcenter) thet=theta(newcenter) newnewcenter=[magnitude*math.cos(angle+thet),magnitude*math.sin(angle+thet)] #rotate newnewnewcenter=[newnewcenter[0] +320,newnewcenter[1] + 480] #Set origin back to corner return newnewnewcenter def normgrid(center): """Take an off coordinate and rotate it back""" global angle newcenter = [center[0] - 320,center[1] - 480] ##set earth to origin magnitude=distance(newcenter) thet=theta(newcenter) newnewcenter=[magnitude*math.cos(thet-angle),magnitude*math.sin(thet-angle)] #rotate newnewnewcenter=[newnewcenter[0] +320,newnewcenter[1] + 480] #Set origin back to corner return newnewnewcenter def angmag(ang,mag): return [math.cos(math.radians(ang))*mag+320,math.sin(math.radians(ang))*mag+480] #button function def buttoninfo(number=1,amount=1,typeno=0): """Send a few arguments to get the button arguments""" global colors n=number m=amount x=(30-(10-m/2))*math.cos(n*2*math.pi/m)+320 y=(30-(10-m/2))*math.sin(n*2*math.pi/m)+80 color = colors[1][typeno] return typeno,color,[x,y],10-m/3 ## Classes class circle: """MAKE CIRCLES""" def __init__(self,color,center=[320,0],radius=10, width=0, grid=1,typeno=-1,pain=1,vel=[0,0]): """Self,color,center,radius. Makes a circle. Perfection""" global colors self.color = color self.center = center self.radius = radius self.width = width self.grid = grid # Is it part of the rotating grid? self.vel=vel self.typeno=typeno self.pain=pain if typeno >=0: self.color=colors[pain][typeno] return None def draw(self,screen): """Uses the self attributes to draw itself and update the rect""" if self.grid==0: self.rect = pygame.draw.circle(screen, self.color, self.center, self.radius, self.width) else: self.rect = pygame.draw.circle(screen, self.color, oddgrid(self.center), self.radius, self.width) return None def update(self): """Move based on velocity""" if self.radius >= 2: self.center[0] += self.vel[0] self.center[1] += self.vel[1] self.vel[0] *= 0.95 self.vel[1] *= 0.95 return None def pull(self,force,fma=20): self.vel[0] += force[0]/fma self.vel[1] += force[1]/fma if distance(force) == 0: self.vel[0] *= 0.5 self.vel[0] *= 0.5 #---------------------------------------------------------- class button(circle): """Buttons used to control gravity""" def __init__(self,typeno,color, center, radius, width=0): self.typeno=typeno self.color=color self.center=center self.radius=radius self.width=width self.state=1 def draw(self,screen): """Uses the self attributes to draw itself and update the rect""" global colors self.rect = pygame.draw.circle(screen,colors[self.state][self.typeno], self.center, self.radius, self.width) return None ############################################################################3 class dot: """These make the stars""" def __init__(self,color,center=[320,0],grid=1): """Self,color,center. Makes a tiny aaline""" self.color = color self.center = center self.grid = grid # Is it part of the rotating grid? return None def draw(self,screen): """Uses the self attributes to draw itself and update the rect""" x=self.center[0] y=self.center[1] if self.grid==0: self.rect = pygame.draw.aalines(screen, self.color,1\ ,[[x,y],[x+1,y+1]]) else: self.rect = pygame.draw.aalines(screen, self.color,1\ , [oddgrid([x,y]),oddgrid([x+1,y+1])]) return None #############level gen############################################################ def levelgen(level): return [\ [circle(colors[2][0],[50,550], 28,0,1,0,2), ], [circle(colors[2][0],[150,550], 9,0,1,0,2), circle(colors[0][1],[50,50], 8,0,1,1,2)], [circle([0,0,0],[50,550], 8,0,1,0,2), circle([0,0,0],[50,50], 7,0,1,0,0)], [circle([0,0,0],[50,550], 8,0,1,0,2), circle([0,0,0],[50,50], 7,0,1,0,0),circle([0,0,0],[50,50], 7,0,1,1,1)], [circle([0,0,0],angmag(0,160),8,0,1,0,2),circle([0,0,0],angmag(60,160),8,0,1,0,0), circle([0,0,0],angmag(120,160),8,0,1,1,2),circle([0,0,0],angmag(180,160),8,0,1,1,0), circle([0,0,0],angmag(240,160),8,0,1,2,2),circle([0,0,0],angmag(300,160),8,0,1,2,0),], [circle([0,0,0],angmag(0,160),8,0,1,0,2),circle([0,0,0],angmag(60,160),10,0,1,0,0), circle([0,0,0],angmag(120,160),8,0,1,1,2),circle([0,0,0],angmag(180,160),10,0,1,1,0), circle([0,0,0],angmag(240,160),8,0,1,2,2),circle([0,0,0],angmag(300,160),10,0,1,2,0), circle([0,0,0],angmag(30,200),7,0,1,0,2),circle([0,0,0],angmag(90,200),8,0,1,0,0), circle([0,0,0],angmag(150,200),7,0,1,1,2),circle([0,0,0],angmag(210,200),8,0,1,1,0), circle([0,0,0],angmag(270,200),7,0,1,2,2),circle([0,0,0],angmag(330,200),8,0,1,2,0),] ][level] ##Main Function############################################################### def main(): #start up pygame pygame.init() screen = pygame.display.set_mode((640, 480),0,32) pygame.display.set_caption("MoonBlock! Press 1,2 & 3. Light good. Dark bad. Neutral strategic.") screen.fill(BGCOLOR) pygame.display.update() clock = pygame.time.Clock() # Globals global angle global colors #initialize local variables lose = 1 speed = 1 level = 0 levelmax = 4 ##set earth, mooon, north pole and stars earth = circle([50,50,200],[320,480],120,0,0) pole2 = circle([220,220,200],[310,485],10,0,1) pole = circle([220,220,200],[320,480],20,0,0) moon = circle([150,150,150],[320,80], 32,0,0) starlist = [] for i in range(240): rota=random.random()*2*math.pi - math.pi magna=random.randint(120,520) starlist.append(dot([255,255,220],\ [magna*math.cos(rota)+320,\ magna*math.sin(rota)+480])) #while you're in the game while lose: if lose: quit = False goals = 0 cols = 0 rocklist = levelgen(level) for rock in rocklist: if rock.pain == 2: goals += 1 cols = max(cols,rock.typeno) buttlist = [] for i in range(cols+1): buttlist.append(button(*buttoninfo(i,cols+1,i))) ##loop############################################################ while not quit: # basics clock.tick(30) screen.fill(BGCOLOR) angle += 0.002 * math.pi * speed #update stuff and check for winning for rock in rocklist: grav = 4*[-0.3,0,1][buttlist[rock.typeno].state] norm = normal([normgrid(moon.center)[0]-rock.center[0]\ ,normgrid(moon.center)[1]-rock.center[1]]) rock.pull([norm[0]*grav,norm[1]*grav] ,rock.radius) rock.update() if distance(rock.center) > 2000: #It went out too far. rock.center = [0,0] rock.vel = [0,0] rockcenter=oddgrid(rock.center) if distance([rockcenter[0]-320,rockcenter[1]-80]) <= rock.radius + 3: #bullseye if rock.pain == 2 and rock.radius>2: #it's good and big pole.radius += 5 #make the pole bigger to show more score goals -= 1 rock.radius = 1 rock.grid = 0 rock.center = oddgrid(rock.center) if goals == 0: #no more good orbs if level == levelmax: #last level level=0 speed +=0.002 quit = True else: #more levels to go level += 1 quit = True elif rock.pain == 0: quit = True lose = False print 'And the score is...' print pole.radius - 20 #draw stuff for star in starlist: star.draw(screen) earth.draw(screen) pole.draw(screen) pole2.draw(screen) moon.draw(screen) for butt in buttlist: butt.draw(screen) for rock in rocklist: rock.draw(screen) for e in pygame.event.get(): #get the keys! if e.type == QUIT: quit = True lose = 0 if e.type == KEYDOWN: if e.key == K_ESCAPE: quit = True lose = 0 elif e.key == K_1 and len(buttlist) > 0: buttlist[0].state = [1,-1,0][buttlist[0].state] #if the buttons state is 1 change it to -1 and change it to 0 and to 1 elif e.key == K_2 and len(buttlist) > 1: buttlist[1].state = [1,-1,0][buttlist[1].state] elif e.key == K_3 and len(buttlist) > 2: buttlist[2].state = [1,-1,0][buttlist[2].state] elif e.key == K_4 and len(buttlist) > 3: buttlist[3].state = [1,-1,0][buttlist[3].state] elif e.key == K_5 and len(buttlist) > 4: buttlist[4].state = [1,-1,0][buttlist[4].state] elif e.key == K_6 and len(buttlist) > 5: buttlist[5].state = [1,-1,0][buttlist[5].state] elif e.key == K_7 and len(buttlist) > 6: buttlist[6].state = [1,-1,0][buttlist[6].state] elif e.key == K_8 and len(buttlist) > 7: buttlist[7].state = [1,-1,0][buttlist[7].state] elif e.key == K_9 and len(buttlist) > 8: buttlist[8].state = [1,-1,0][buttlist[8].state] elif e.key == K_0 and len(buttlist) > 9: buttlist[9].state = [1,-1,0][buttlist[9].state] elif e.key == K_k: level +=1 quit = True pygame.display.flip() pygame.quit() if __name__ == "__main__": main()