From 81948a2e50be9129b2cb8056219d52cd0e2cdef6 Mon Sep 17 00:00:00 2001 From: LingDong Date: Fri, 11 Dec 2015 02:08:53 -0500 Subject: [PATCH] Import everything --- creature.py | 1005 +++++++++++++++++++++++++++++++++++++++++++++++++ filter.py | 54 +++ font.py | 80 ++++ foo.py | 56 +++ main.py | 592 +++++++++++++++++++++++++++++ noise.py | 105 ++++++ parse.py | 42 +++ particle.py | 41 ++ pattern.py | 78 ++++ projectile.py | 51 +++ settings.py | 5 + tree.py | 169 +++++++++ utilities.py | 36 ++ 13 files changed, 2314 insertions(+) create mode 100644 creature.py create mode 100644 filter.py create mode 100644 font.py create mode 100644 foo.py create mode 100644 main.py create mode 100644 noise.py create mode 100644 parse.py create mode 100644 particle.py create mode 100644 pattern.py create mode 100644 projectile.py create mode 100644 settings.py create mode 100644 tree.py create mode 100644 utilities.py diff --git a/creature.py b/creature.py new file mode 100644 index 0000000..8bdfe78 --- /dev/null +++ b/creature.py @@ -0,0 +1,1005 @@ +import pygame +import sys +import math +import utilities as u +import copy +import random +import noise +import projectile +import tree +import settings + +pygame.init() + +class Animal(object): + def __init__(self,x,y): + self.x = x + self.y = y + self.yo = 0 + self.s = 2 + self.t = 0 + self.aspd = 0.05 + self.skel = [] + self.color = (150,150,150) + self.animations = [[]] + self.dir = 1 + self.spd = 0.5 + self.timers = [] + self.health = 100 + + def __str__(self): + return self.skel + + def super(self): + return super(type(self),self) + + def calcCoord(self,n): + if n == self.skel[n][2]: + return [0,0,0] + else: + trl = self.skel[n] + pc = self.calcCoord(trl[2]) + tc = [0,0,0] + + tc[0] = pc[0] + trl[1]*math.cos(math.radians(trl[0]+pc[2])) + tc[1] = pc[1] - trl[1]*math.sin(math.radians(trl[0]+pc[2])) + tc[2] = pc[2] + trl[0] + return tc + def to(self,r,l,n,spd=3): + self.skel[r][l] += (n-self.skel[r][l])/float(spd) + + def animate(self): + for a in self.animations[0]: + if a[0][0] == "trans": + if a[0][1] == "x": + self.x+=(a[1][0]-self.x)/float(a[1][1]) + #self.dir = (a[1][0]-self.x>0)*2-1 + self.walk() + if a[0][1] == "xt": + self.x+=(a[1][0]-self.x)/float(a[1][1]) + + elif a[0][1] == "y": + self.y+=(a[1][0]-self.y)/float(a[1][1]) + else: + self.skel[a[0][0]][a[0][1]]+=(a[1][0]-self.skel[a[0][0]][a[0][1]])/float(a[1][1]) + a[1][1]-=1 + if a[1][1]<=0: + a.remove(a[1]) + if len(a) <= 1: + self.animations[0].remove(a) + if len(self.animations[0]) == 0: + self.animations.pop(0) + if len(self.animations)==0: + self.animations.append([]) + + for ti in self.timers: + ti[0] -= 1 + if ti[0] == 0: + ti[1](*ti[2]) + self.timers.remove(ti) + + + + def addanim(self,skn,rol,dest,t): + na = [[skn,rol],[dest,t]] + for a in self.animations[-1]: + if a[0][0]==na[0][0] and a[0][1]==na[0][1]: + a.append(na[1]) + return + self.animations[-1].append(na) + + def animback(self,t,exceptions=[]): + for i in range(0,len(self.skel)): + if i not in exceptions: + self.addanim(i,0,self.ssk[i][0],t) + self.addanim(i,1,self.ssk[i][1],t) + + + + + def poly(self,surf,*args): + u.polygon(surf,self.color,u.lmap(lambda l: [self.x+l[0]*self.s*self.dir,self.y+self.yo+l[1]*self.s], args)) + def circle(self,surf,pos,radius): + u.circle(surf,self.color,[self.x+pos[0]*self.s*self.dir,self.y+self.yo+pos[1]*self.s],radius*self.s) + def line(self,surf,start_pos,end_pos,width=1): + u.line (surf,self.color,[self.x+start_pos[0]*self.s*self.dir,self.y+self.yo+start_pos[1]*self.s], + [self.x+ end_pos[0]*self.s*self.dir,self.y+self.yo+ end_pos[1]*self.s],width*self.s) + + + def drawSkel(self,surf): + for i in range(0,len(self.skel)): + c = self.calcCoord(i) + pc = self.calcCoord(self.skel[i][2]) + self.line(surf,[c[0],c[1]],[pc[0],pc[1]],1) + + + +class Horse(Animal): + def __init__(self,x,y): + + super(Horse,self).__init__(x,y) + self.phase = "playing" + self.skel=[ [-90,10, 1], + [ 30,20, 2],#1 + [ 0, 0, 2], + [190,10, 2], + [-20,10, 3], + [ 50,10, 4],#5 + [ 30,15, 5], + [-70,10, 2], + [-10,12, 7], + [ 0,12, 8], + [-30,12, 7],#10 + [ 20,12,10], + [ 70,10, 4], + [ 70,10,12], + [-90,10,13], + [ 45,12,14],#15 + [ 85,10,12], + [-90,10,16], + [ 45,12,17],#18 + ] + self.ssk = copy.deepcopy(self.skel) + + def draw(self,surf): + cd = [] + for i in range(len(self.skel)): + cd.append(self.calcCoord(i)[:2]) + + self.poly(surf, cd[2], + [cd[7][0]+2,cd[7][1]+2], [cd[3][0]+5,cd[3][1]+13], + cd[12], cd[4], [cd[4][0]+4,cd[4][1]], cd[3] + ) + self.poly(surf, cd[2], [cd[1][0]-3,cd[1][1]], + [cd[1][0]+1,cd[1][1]-1], [cd[1][0]+3,cd[1][1]], + [cd[1][0]+2,cd[1][1]+1], [cd[0][0]+1,cd[0][1]-1], + [cd[0][0]-1,cd[0][1]+1], [cd[1][0]-1,cd[1][1]+5], + [(cd[2][0]+cd[1][0])/2,(cd[2][1]+cd[1][1])/2+8], + [cd[7][0]+2,cd[7][1]+2], + ) + self.poly(surf, cd[2], [cd[7][0]+2,cd[7][1]], [cd[8][0]+2,cd[8][1]], + [cd[9][0]+2,cd[9][1]], [cd[9][0],cd[9][1]], + cd[8], [cd[7][0]-6,cd[7][1]] + ) + self.poly(surf, cd[2], [cd[7][0]+2,cd[7][1]], [cd[10][0]+2,cd[10][1]], + [cd[11][0]+2,cd[11][1]], [cd[11][0],cd[11][1]], + cd[10], [cd[7][0]-6,cd[7][1]] + ) + self.poly(surf, cd[3],cd[2],cd[13],cd[12],cd[4]) + self.poly(surf, cd[3],cd[2],cd[16],cd[12],cd[4]) + + self.poly(surf, cd[12],cd[13], + [cd[14][0]+2,cd[14][1]], [cd[15][0]+1,cd[15][1]], + [cd[15][0]-1,cd[15][1]], [cd[14][0],cd[14][1]], + [(cd[12][0]+cd[14][0])/2+2,(cd[12][1]+cd[14][1])/2] + ) + self.poly(surf, cd[12],cd[16], + [cd[17][0]+2,cd[17][1]], [cd[18][0]+1,cd[18][1]], + [cd[18][0]-1,cd[18][1]], [cd[17][0],cd[17][1]], + [(cd[12][0]+cd[17][0])/2+2,(cd[12][1]+cd[17][1])/2] + ) + + self.poly(surf, [cd[4][0],cd[4][1]],cd[5],cd[6]) + + + + def walk(self): + s = self + s.t += 1 + + s.to(1,0,30-math.cos(s.t*s.aspd*2)*5) + s.to(0,0,-85+math.cos(s.t*s.aspd*2)*10) + + s.to(3,0,190+math.cos(s.t*s.aspd*2)*1) + s.to(4,0,-20+math.cos(s.t*s.aspd*2)*2) + + s.to(6,0,30+math.cos(s.t*s.aspd*1.5)*10) + + s.to(7,1,9-math.cos(s.t*s.aspd*2)*1) + + s.to(8,0,-18+math.sin(s.t*s.aspd)*25) + s.to(9,0,-20-math.cos(s.t*s.aspd)*20) + + s.to(10,0,-18+math.sin(s.t*s.aspd+math.pi)*25) + s.to(11,0,-20-math.cos(s.t*s.aspd+math.pi)*20) + + s.to(12,1,7+math.cos(s.t*s.aspd*2)*1) + + s.to(13,0,75+math.cos(s.t*s.aspd)*15) + s.to(14,0,-90+math.cos(s.t*s.aspd)*15) + s.to(15,0,55+math.sin(s.t*s.aspd)*2) + + s.to(16,0,75+math.cos(s.t*s.aspd+math.pi)*15) + s.to(17,0,-90+math.cos(s.t*s.aspd+math.pi)*20) + s.to(18,0,55+math.sin(s.t*s.aspd+math.pi)*2) + + def rest(self): + s = self + s.t +=1 + for i in range(0,len(s.skel)): + if i != 6 and i != 1: + s.to(i,0,s.ssk[i][0]+noise.noise(i*0.05,s.t*0.05),5) + s.to(6,0,30+math.cos(s.t*s.aspd*0.5)*10) + + noi = max(min((noise.noise(s.t*s.aspd*0.05)-0.4)*40,1),-1) + s.to(1,0,-5+noi*50,5) + s.to(0,0,-40-noi*40,5) + + #s.to(1,1,25+math.cos(s.t*s.aspd*0.5+math.pi)*2) +class Unicorn(Horse): + def __init__(self,x,y): + super(Unicorn,self).__init__(x,y) + def draw(self,surf): + super(Unicorn,self).draw(surf) + + cd = [] + for i in range(len(self.skel)): + cd.append(self.calcCoord(i)[:2]) + self.poly(surf,cd[1],[cd[1][0]+(cd[0][0]-cd[1][0])/3.0,cd[1][1]+(cd[0][1]-cd[1][1])/3.0], + [cd[1][0]+20*math.cos(math.radians(-self.calcCoord(1)[2]+5*self.dir)),cd[1][1]+50*math.sin(math.radians(-self.calcCoord(1)[2]+5*self.dir))]) + self.color = [random.randrange(0,255),random.randrange(0,255),random.randrange(0,255)] + +class Deer(Horse): + def __init__(self,x,y,color=(140,140,140),s=1.1): + super(Deer,self).__init__(x,y) + self.skel[1][1] = 15 + self.skel[5][1] = 5 + self.skel[6][1] = 5 + self.ssk = copy.deepcopy(self.skel) + self.s = s + self.dir = -1 + self.horn = pygame.Surface([100*self.s,50*self.s]) + self.horn.fill([255,0,255]) + self.horn.set_colorkey([255,0,255]) + self.color = color + + self.spd = 0.6 + self.tx = 0 + + tree.drawTree(surf = self.horn, + x = 50*self.s,#cd[1][0]*self.s+self.x, + y = 50*self.s,#cd[1][1]*self.s+self.y+self.yo, + angle = math.pi*2/3, + dangle = lambda dep: 0,#-(random.random()-0.5)*math.pi/3, + + trunk = 0, + dtrunk = lambda dep: 0,#0.8*random.random(), + + width = 3*self.s*0.6, + dwidth = lambda dep: random.random()*0.1+0.9, + + height = 4*self.s*0.6, + dheight = lambda dep: 1.2*((dep*2)%2)+0.4,#(((dep+1)*2)%2), + + opening = math.pi/6, + dopening = lambda dep: 0.5+random.random()*0.5, + + color = self.color, + depth = 0, + maxdepth = 6 + ) + if self.dir == -1: + self.horn = pygame.transform.flip(self.horn,1,0) + #pygame.draw.rect(self.horn,(255,0,0),[0,0,self.horn.get_width()/2,self.horn.get_height()],5) + self.shorn = self.horn + #self.horn.get_rect().center=(50*self.s,50*self.s) + #self.horn = pygame.transform.rotate(self.horn, 90) + #self.horn.get_rect().center=(50*self.s,50*self.s) + #print self.horn.get_rect().center + #self.horn = pygame.transform.scale(self.horn, (50*self.s,35*self.s)) + def draw(self,surf): + super(Deer,self).draw(surf) + cd = [] + for i in range(len(self.skel)): + cd.append(self.calcCoord(i)[:2]) + #print self.horn.get_width() + self.horn = self.shorn + #self.tx = (self.tx+0.5)%90.0 + #a = self.tx + a = self.dir*(self.calcCoord(0)[2]+30) + + self.horn = pygame.transform.rotate(self.horn, a) + #cd[1] = [-80,-80] + if self.dir == 1: + hc = [cd[1][0]*self.s+self.x - 50*self.s*math.cos(math.radians(90+a))-70*self.s*math.cos(math.radians(45-a)), + cd[1][1]*self.s+self.y+self.yo - 68*self.s*math.sin(math.radians(45-a))] + else: + #hc = [-cd[1][0]*self.s+self.x - 50*self.s*math.sin(math.radians(a)), + # cd[1][1]*self.s+self.y+self.yo - 50*self.s*math.sin(math.radians(a)) - 50*self.s*math.cos(math.radians(a))] + hc = [-cd[1][0]*self.s+self.x - 50*self.s*math.cos(math.radians(a))-49*self.s*math.sin(math.radians(a)), + cd[1][1]*self.s+self.y+self.yo - 50*self.s*math.sin(math.radians(a)) - 49*self.s*math.cos(math.radians(a))] + + + #pygame.draw.rect(surf,(0,255,0),[hc[0],hc[1],self.horn.get_width()/2,self.horn.get_height()],1) + + surf.blit(self.horn,[hc[0],hc[1]]) + + + + + +class Man(Animal): + def __init__(self,x,y): + super(Man,self).__init__(x,y) + self.skel=[ [ -20, 5, 1], + [ 105,20, 2],#1 + [ 0, 0, 2], + [-100,30, 2], + [-170, 8, 1], + [ 50, 5, 4],#5 + [-160, 8, 1], + [ 20, 5, 6], + [ 180, 0, 7], + [-180, 0, 7], + [ 0, 0, 5] #10 + ] + self.ssk = copy.deepcopy(self.skel) + self.f1 = 6 + self.f2 = 6 + self.s1 = 0 + self.s2 = 0 + self.status = ["",""] + self.assets = [] + self.eventdelay = 0 + self.arrows = [] + + + + def draw(self,surf): + cd = [] + for i in range(len(self.skel)): + cd.append(self.calcCoord(i)[:2]) + s = self + s.poly(surf, [(cd[0][0]+cd[1][0])/2,(cd[0][1]+cd[1][1])/2], + [cd[1][0]+3,cd[1][1]+3], [cd[2][0]+3,cd[2][1]], + [cd[2][0]-4,cd[2][1]], [cd[1][0]-2,cd[1][1]+5] + ) + + s.poly(surf, [cd[2][0]-4,cd[2][1]], + [(cd[2][0]+cd[3][0])/2-s.f1/2,(cd[2][1]+cd[3][1])/2], + [cd[3][0]-s.f1,cd[3][1]], [cd[3][0]+s.f2,cd[3][1]], + [cd[2][0]+3,cd[2][1]] + ) + + + s.poly(surf, [cd[1][0],cd[1][1]], [cd[4][0],cd[4][1]], + [cd[4][0]+s.s1,cd[4][1]+15], [cd[1][0]-2,cd[1][1]+5] + ) + s.poly(surf, [cd[4][0],cd[4][1]], + [cd[5][0],cd[5][1]], [cd[5][0]+s.s1,cd[5][1]+12], + [cd[4][0]+s.s1,cd[4][1]+15] + ) + + s.poly(surf, [cd[1][0],cd[1][1]], [cd[6][0],cd[6][1]], + [cd[7][0],cd[7][1]], [cd[7][0]+s.s2,cd[7][1]+12], + [cd[6][0]+s.s2,cd[6][1]+15], [cd[1][0]-2,cd[1][1]+5] + ) + #s.circle(surf,[cd[0][0],cd[0][1]],1.5) + s.line(surf,cd[0],cd[1],3) + if "bow" in self.assets: + s.line(surf,cd[7],cd[8],2) + s.line(surf,cd[7],cd[9],2) + + if "arrow" in self.assets: + s.line(surf,cd[5],cd[10],1) + + if "cup" in self.assets: + s.line(surf,cd[5],cd[10],2) + + def walk(self): + s = self + s.t += 1 + s.f1 = 6 + math.cos(s.t*s.aspd*2)*2 +noise.noise(s.t*s.aspd)*2 + s.f2 = 8 + math.cos(s.t*s.aspd*2)*2 + #print self.animations + if self.status[1] == "": + for i in range(0,len(s.skel)): + for j in range(0,max(0,len(self.animations))): + for a in self.animations[j]: + if i == a[0][0]: + #print "y" + return + s.to(i,0,s.ssk[i][0]+10*(noise.noise(i*0.05,s.t*0.05)-0.5),2) + def rest(self): + s = self + s.t+=1 + noi = noise.noise(s.t*s.aspd*0.5) + s.f1 += (6+(noi*5)-s.f1)/2 + s.s1 += (1-(noi*4)-s.s1)/2 + s.s2 += (1-(noi*4)-s.s2)/2 + + + def mount(self,horse): + + self.addanim("trans","y",self.y,50) + horse.addanim("trans","x",self.x+horse.dir*horse.s*7,50) + self.animations.append([]) + self.addanim(1,0,60,30) + self.addanim("trans","y",horse.y+horse.s*0.5+(horse.yo-self.yo),20) + self.animations.append([]) + self.addanim("trans","y",horse.y-horse.s*2+(horse.yo-self.yo),20) + self.addanim(1,0,100,20) + self.addanim(3,0,-120,20) + self.addanim(3,0,-80,10) + self.addanim(3,1,30,30) + self.addanim(3,1,10,20) + self.status[0] = "mounting" + def f(x): self.status[0]='mounted' + self.timers.append([120,f,[0]]) + + def dismount(self,horse): + self.addanim(3,0,-120,20) + self.addanim(3,0,-80,20) + self.addanim(3,0,-100,10) + self.addanim(3,1,30,30) + self.addanim(1,0,60,20) + self.addanim("trans","y",0,60) + self.addanim(1,0,105,40) + #self.animations.append([]) + #self.addanim("trans","x",horse.x+horse.s*50,100) + horse.addanim("trans","y",horse.y,60) + horse.animations.append([]) + horse.addanim("trans","x",self.x-horse.s*50,50) + self.status[0] = "dismounting" + def f(x): self.status[0]='' + self.timers.append([80,f,[0]]) + + def drawbow(self): + + if "prepares" in self.status[1]: + self.status[1]="bow starts drawing" + self.assets.append("bow") + self.assets.append("arrow") + self.animations = [[]] + self.addanim(1,0,80,20) + + self.addanim(8,1,20,25) + self.addanim(9,1,20,25) + + self.addanim(8,0,90,1) + self.addanim(9,0,-90,1) + self.addanim(10,1,25,10) + self.addanim(10,0,-15,10) + def f(x): + self.status[1]="bow is drawing" + self.timers.append([25,f,[0],"drawbow"]) + elif self.status[1] == "bow is drawing": + self.to(1,0,110,15) + self.to(4,0,70,15) + self.to(5,0,-160,15) + self.to(6,0,-100,15) + self.to(7,0,0,15) + + self.to(8,0,100,15) + self.to(9,0,-100,15) + + + if abs(self.skel[1][0]-110)<1: + self.status[1]="bow is tightening" + elif self.status[1] == "bow is tightening": + self.to(4,1,10,50) + self.to(8,0,110,50) + self.to(9,0,-110,50) + self.to(1,0,120,50) + + + + def releasebow(self): + self.status[1] = "bow is releasing" + self.addanim(8,0,90,3) + self.addanim(9,0,-90,3) + self.addanim(8,0,92,5) + self.addanim(9,0,-92,5) + self.addanim(5,0,-130,5) + if "arrow" in self.assets: + self.assets.remove("arrow") + arr = projectile.Arrow(self.x+self.calcCoord(5)[0]*self.s*self.dir,self.yo+self.y+self.calcCoord(5)[1]*self.s) + arr.a = self.calcCoord(10)[2] + #print(arr.a) + arr.l = 25*self.s + arr.spd = 1+0.05*max(1,self.skel[1][0]-100)*max(1,self.skel[8][0]-90) + arr.v = arr.calcV() + arr.color = self.color + self.arrows.append(arr) + + def f(x): + self.addanim(8,1,0,20) + self.addanim(9,1,0,20) + self.animback(20,exceptions=[3,8,9]) + self.status[1] = "" + def g(x): + self.assets.remove("bow") + self.timers.append([25,g,[0],"removebow"]) + self.timers.append([25,f,[0],"releasebow"]) + + + def drink(self): + self.addanim(4,0,-140,20) + self.addanim(5,0,60,20) + + self.addanim(0,0,-20,20) + self.addanim(10,1,3,20) + self.addanim(10,0,80,20) + + self.animations.append([]) + + self.addanim(4,0,-90,20) + self.addanim(5,0,0,20) + self.addanim(0,0,0,20) + + self.animations.append([]) + self.addanim(4,0,-110,20) + self.addanim(5,0,30,20) + + self.animations.append([]) + + self.addanim(4,0,-60,30) + self.addanim(5,0,120,30) + self.addanim(1,0,120,30) + self.addanim(0,0,40,30) + self.addanim(10,0,45,30) + + self.animations.append([]) + self.addanim(10,0,50,30) + self.addanim(0,0,50,30) + + self.animations.append([]) + self.animback(30,[3]) + self.assets.append("cup") + self.status[1] = "drinking" + def f(x): + self.assets.remove("cup") + self.status[1] = "" + self.timers.append([200,f,[0]]) + + + + def keyupdowncontrol(self,event,horse): + + if event.type == pygame.KEYDOWN: + if event.key==pygame.K_DOWN: + if self.status[1] == "": + self.drink() + + if event.key==pygame.K_UP: + if self.status[0] == "" : + + if abs((self.yo-10) - horse.yo)<10*self.s: + self.mount(horse) + else: + settings.msg= ["CANNOT MOUNT ON SLOPE.",settings.msgt] + elif self.status[0] == "mounted": + self.dismount(horse) + if event.type == pygame.KEYDOWN: + if self.eventdelay <= 0: + if event.key==pygame.K_LEFT: + if not "bow" in self.status[1] and not "drinking" in self.status[1]: + self.status[1] = "bow prepares" + self.eventdelay=80 + if event.type == pygame.KEYUP: + + if event.key==pygame.K_LEFT: + if "bow" in self.status[1]: + self.releasebow() + + + def keyholdcontrol(self): + self.eventdelay -= self.eventdelay>0 + if pygame.key.get_pressed()[pygame.K_RIGHT]: + self.walk() + else: + self.rest() + if pygame.key.get_pressed()[pygame.K_LEFT] and (not "ing" in self.status[0]) and "bow" in self.status[1] and not "releasing" in self.status[1]: + self.drawbow() + + + + +class Bird(Animal): + def __init__(self,x,y): + super(Bird,self).__init__(x,y) + self.skel=[ [ -60, 5, 1], + [ 30, 5, 2],#1 + [ 0, 0, 2], + [ 190,10, 2], + [ -10, 8, 3], + [ 150, 8, 2],#5 + [ -60,10, 5], + [ 50,10, 6], + [ 150, 8, 2], + [ -60,10, 8], + [ 50,10, 9],#10 + [-140, 8, 2], + [ -30, 3,11], + [ 50, 5,12], + [ -30, 3,11], + [ 50, 5,14] #15 + ] + self.ssk = copy.deepcopy(self.skel) + self.aspd = 0.1 + + self.t = random.random()*math.pi*2 + + self.w1 = [-5,-5] + self.w2 = [-3,-20] + self.w3 = [-8,-30] + self.t2 = 0 + self.v = [0,0] + self.on = 0 + + self.arrow = None + + def wingCoordToRL(self,n,w,lw=[0,0],lr=0,slr=0): + self.skel[n][0] = -(180-(-math.degrees(math.atan2(w[1]-lw[1],w[0]-lw[0]))-slr+180-lr)) + self.skel[n][1] = math.sqrt((w[0]-lw[0])**2+(w[1]-lw[1])**2) + + def fly(self): + s = self + s.t += 1 + + s.w1[1] = -1+u.trapwave(s.t*s.aspd)*3 + s.w2[1] = -2+u.trapwave(s.t*s.aspd)*8 + s.w3[1] = -1+u.trapwave(s.t*s.aspd+math.pi*0.2)*12 + + s.w2[0] = -3+math.sin(s.t*s.aspd-math.pi*0.5)*2 + s.w3[0] = -12+math.sin(s.t*s.aspd-math.pi*0.5)*3 + + + s.wingCoordToRL(5,s.w1) + s.wingCoordToRL(6,s.w2,s.w1,s.skel[5][0]) + s.wingCoordToRL(7,s.w3,s.w2,s.skel[6][0],s.skel[5][0]) + + s.wingCoordToRL(8,s.w1) + s.wingCoordToRL(9,s.w2,s.w1,s.skel[5][0]) + s.wingCoordToRL(10,s.w3,s.w2,s.skel[6][0],s.skel[5][0]) + + + s.to(4,0,-0+math.sin(s.t*s.aspd+math.pi)*10) + s.to(1,0,10+math.sin(s.t*s.aspd)*10) + + s.to(12,0,-30) + s.to(14,0,-30) + s.to(1,1,3) + + + s.to(13,0,50+math.sin(s.t*s.aspd)*10 + 10*noise.noise(s.t*s.aspd*0.01,1)-5) + s.to(15,0,50+math.sin(s.t*s.aspd)*10 + 10*noise.noise(s.t*s.aspd*0.01,2)-5) + + s.x += s.v[0]*s.dir + s.y += 0.5*s.v[1]+0.5*s.v[1]*(0.5*(math.sin(s.t*s.aspd)+1)) + + def simpFly(self): + s = self + s.t += 1 + + s.w1[1] = -1+u.trapwave(s.t*s.aspd)*3 + s.w2[1] = -2+u.trapwave(s.t*s.aspd)*8 + s.w3[1] = -1+u.trapwave(s.t*s.aspd+math.pi*0.2)*12 + + s.w2[0] = -3+math.sin(s.t*s.aspd-math.pi*0.5)*2 + s.w3[0] = -12+math.sin(s.t*s.aspd-math.pi*0.5)*3 + + s.wingCoordToRL(5,s.w1) + s.wingCoordToRL(6,s.w2,s.w1,s.skel[5][0]) + s.wingCoordToRL(7,s.w3,s.w2,s.skel[6][0],s.skel[5][0]) + + s.to(4,0,-0+math.sin(s.t*s.aspd+math.pi)*10) + s.to(1,0,10+math.sin(s.t*s.aspd)*10) + + s.to(1,1,3) + + s.x += s.v[0]*s.dir + s.y += 0.5*s.v[1]+0.5*s.v[1]*(0.5*(math.sin(s.t*s.aspd)+1)) + + def fall(self): + s = self + s.v[0] = s.arrow.v[0] + s.v[1] = s.arrow.v[1] + s.x += s.v[0] + s.y += s.v[1] + + def rest(self): + + s = self + #s.t = -1 #math.pi + s.t2 += 1 + s.to(5,0,20+180*2*((s.skel[5][0]>0)-0.5),10) + s.to(6,0,-20+180*2*((s.skel[6][0]>0)-0.5),10) + s.to(7,0,20-180*2*((s.skel[10][0]<0)-0.5),10) + + s.to(8,0,20+180*2*((s.skel[8][0]>0)-0.5),10) + s.to(9,0,-20+180*2*((s.skel[9][0]>0)-0.5),10) + s.to(10,0,20-180*2*((s.skel[10][0]<0)-0.5),10) + + s.to(12,0,30,10) + s.to(14,0,30,10) + s.to(13,0,100,10) + s.to(15,0,100,10) + + noi = max(min((noise.noise(s.t2*s.aspd*0.5)-0.3)*50,1),-1) + s.to(1,0,-10+noi*20,5) + s.to(1,1,5-noi*2,5) + s.to(4,0,-10+noi*10,5) + + s.x += s.v[0]*s.dir + s.y += s.v[1] + if s.y>=0: + s.v[1] = 0 + s.v[0] = 0 + s.y = 0 + else: + s.v[1] += 0.2*s.s + if random.random() < 0.02 and s.v[1] == 0: + s.v[1]=-1*s.s + r = random.choice([1,1]) + s.v[0]+=0.5*r*s.s + #s.dir = r + + + + def draw(self,surf): + cd = [] + for i in range(len(self.skel)): + cd.append(self.calcCoord(i)[:2]) + s = self + s.poly(surf,cd[3],cd[2],cd[5], + [cd[5][0]-5,cd[5][1]]) + s.poly(surf,cd[5],cd[6], + [cd[6][0]-8,cd[6][1]], + [cd[5][0]-5,cd[5][1]]) + s.poly(surf,cd[6],[cd[6][0]-8,cd[6][1]],cd[7]) + s.poly(surf,cd[2],cd[5],cd[6]) + + + s.poly(surf,cd[3],cd[2],cd[8], + [cd[8][0]-5,cd[8][1]]) + s.poly(surf,cd[8],cd[9], + [cd[9][0]-8,cd[9][1]], + [cd[8][0]-5,cd[8][1]]) + s.poly(surf,cd[9],[cd[9][0]-8,cd[9][1]],cd[10]) + s.poly(surf,cd[2],cd[8],cd[9]) + + + + + + s.poly(surf,cd[2],[(cd[2][0]+cd[3][0])/2,(cd[2][1]+cd[3][1])/2-2],cd[3],cd[11],[cd[11][0]+5,cd[11][1]]) + s.poly(surf,[(cd[3][0]+cd[4][0])/2,(cd[3][1]+cd[4][1])/2],cd[3],cd[11]) + s.poly(surf,cd[0],cd[1],cd[2]) + s.poly(surf,cd[11],[(cd[0][0]+cd[1][0])/2,(cd[0][1]+cd[1][1])/2],cd[1],cd[2]) + + s.line(surf,cd[11],cd[12],3) + s.line(surf,cd[11],cd[14],3) + + s.line(surf,cd[3],cd[4],2) + + s.line(surf,cd[12],cd[13]) + s.line(surf,cd[14],cd[15]) + def simpDraw(self,surf): + cd = [] + for i in range(len(self.skel)-3): + cd.append(self.calcCoord(i)[:2]) + s = self + + s.poly(surf,cd[3],cd[2],cd[5], + [cd[5][0]-5,cd[5][1]]) + s.poly(surf,cd[5],cd[6], + [cd[6][0]-8,cd[6][1]], + [cd[5][0]-5,cd[5][1]]) + s.poly(surf,cd[6],[cd[6][0]-8,cd[6][1]],cd[7]) + #s.poly(surf,cd[2],cd[5],cd[6]) + + s.poly(surf,cd[4],cd[11],cd[0],cd[1],cd[2],cd[3]) + + + +class Crane(Bird): + def __init__(self,x,y): + super(Crane,self).__init__(x,y) + self.skel=[ [ -5, 8, 1], + [ 30,10, 2],#1 + [ 0, 0, 2], + [ 190,10, 2], + [ -10, 5, 3], + [ 150, 8, 2],#5 + [ -60,10, 5], + [ 50,10, 6], + [ 150, 8, 2], + [ -60,10, 8], + [ 50,10, 9],#10 + [-140, 8, 2], + [ -30, 3,11], + [ 50,10,12], + [ -30, 3,11], + [ 50,10,14] #15 + ] + self.t = random.random()*math.pi*2 + def fly(self): + + s = self + s.t += 1 + + s.w1[1] = -1+u.trapwave(s.t*s.aspd)*3 + s.w2[1] = -2+u.trapwave(s.t*s.aspd)*8 + s.w3[1] = -1+u.trapwave(s.t*s.aspd+math.pi*0.2)*12 + + s.w2[0] = -3+math.sin(s.t*s.aspd-math.pi*0.5)*2 + s.w3[0] = -5+math.sin(s.t*s.aspd-math.pi*0.5)*3 + + + s.wingCoordToRL(5,s.w1) + s.wingCoordToRL(6,s.w2,s.w1,s.skel[5][0]) + s.wingCoordToRL(7,s.w3,s.w2,s.skel[6][0],s.skel[5][0]) + + s.wingCoordToRL(8,s.w1) + s.wingCoordToRL(9,s.w2,s.w1,s.skel[5][0]) + s.wingCoordToRL(10,s.w3,s.w2,s.skel[6][0],s.skel[5][0]) + + + s.to(4,0,-0+math.sin(s.t*s.aspd+math.pi)*10) + s.to(1,0,-5+math.sin(s.t*s.aspd)*1) + + s.to(12,0,-40) + s.to(14,0,-40) + + s.to(13,0,10+math.sin(s.t*s.aspd)*5 + 10*noise.noise(s.t*s.aspd*0.01,1)-5) + s.to(15,0,10+math.sin(s.t*s.aspd)*5 + 10*noise.noise(s.t*s.aspd*0.01,2)-5) + + #s.x += s.v[0]*s.dir + s.y += 0.2*self.s*math.sin(s.t*s.aspd+math.pi)#30.5*s.v[1]+0.5*s.v[1]*(0.5*(math.sin(s.t*s.aspd)+1)) + + def draw(self,surf): + cd = [] + for i in range(len(self.skel)): + cd.append(self.calcCoord(i)[:2]) + s = self + s.poly(surf,cd[3],cd[2],cd[5], + [cd[5][0]-5,cd[5][1]]) + s.poly(surf,cd[5],cd[6], + [cd[6][0]-8,cd[6][1]], + [cd[5][0]-5,cd[5][1]]) + s.poly(surf,cd[6],[cd[6][0]-8,cd[6][1]],cd[7]) + s.poly(surf,cd[2],cd[5],cd[6]) + + + s.poly(surf,cd[3],cd[2],cd[8], + [cd[8][0]-5,cd[8][1]]) + s.poly(surf,cd[8],cd[9], + [cd[9][0]-8,cd[9][1]], + [cd[8][0]-5,cd[8][1]]) + s.poly(surf,cd[9],[cd[9][0]-8,cd[9][1]],cd[10]) + s.poly(surf,cd[2],cd[8],cd[9]) + + + s.poly(surf,cd[2],[(cd[2][0]+cd[3][0])/2,(cd[2][1]+cd[3][1])/2-2],cd[3],cd[11],[(cd[2][0]+cd[1][0])/2,(cd[2][1]+cd[1][1])/2]) + s.poly(surf,[(cd[3][0]+cd[4][0])/2,(cd[3][1]+cd[4][1])/2],cd[3],cd[11]) + s.poly(surf,cd[1],cd[2],[cd[2][0],cd[2][1]+2]) + s.poly(surf,cd[0],cd[1],[(cd[0][0]+cd[1][0])/2,(cd[0][1]+cd[1][1])/2-2]) + #s.line(surf,cd[2],cd[1],1) + #s.poly(surf,cd[0],cd[1],cd[2]) + #s.poly(surf,cd[11],[(cd[0][0]+cd[1][0])/2,(cd[0][1]+cd[1][1])/2],cd[1],cd[2]) + + s.line(surf,cd[11],cd[12],1) + s.line(surf,cd[11],cd[14],1) + + s.poly(surf,cd[3],cd[4],cd[11]) + #s.line(surf,cd[3],cd[4],2) + + s.line(surf,cd[12],cd[13],0.5) + #s.line(surf,cd[14],cd[15]) + + + + + + + + +if __name__ == "__main__": + settings.init() + screen = pygame.display.set_mode([480,320]) + + horse = Horse(100,0) + horse2 = Horse(100,0) + horse2.s = 2.5 + horse2.aspd = 0.1 + + + deer = Deer(220,0,s=4) + deer.yo = 150 + + arrows = [] + man = Man(200,0) + man.arrows = arrows + horse2.yo=140 + man.yo=160 + man.s = 2#0.7 + man.walk() + + bird = Crane(200,0) + bird.s = 5 + bird.aspd = 0.05 + bird.yo = 250 + + bird2 = Crane(200,0) + bird2.s = 5 + bird2.aspd = 0.05 + bird2.yo = 120 + + + birds = [] + + + for i in range(0,10): + b = Bird(random.randrange(150,300),0) + b.s = 0.5 + b.aspd = 0.3 + b.yo = 176 + b.dir = random.choice([1,-1]) + birds.append(b) + + while 1: + for event in pygame.event.get(): + if event.type == pygame.QUIT: sys.exit() + man.keyupdowncontrol(event,horse2) + + + + man.keyholdcontrol() + screen.fill([240,240,240]) + + + #horse.drawSkel(screen) + #horse.walk() + + #horse2.draw(screen) + man.animate() + horse2.animate() + #bird.draw(screen) + bird.fly() + + #bird2.drawSkel(screen) + bird2.fly() + + #print(man.status) + #man.draw(screen) + #man.drawSkel(screen) + for b in birds: + if abs(man.x - b.x) < 100 and random.random()<0.05 and b.on == 0: + b.on = 1 + ra = math.pi/6.0+random.random()*math.pi/6.0*1.5 + rl = random.choice([2,3,4]) + b.v=[rl*math.cos(ra),-rl*math.sin(ra)] + if b.on == 1: + b.simpFly() + else: + b.rest() + + + #bird.drawSkel(screen) + #bird.s = math.sin(bird.t*0.01)*5+5 + #for b in birds: + #b.simpDraw(screen) + deer.walk() + if pygame.key.get_pressed()[pygame.K_RIGHT]: + horse2.walk() + + for b in birds: + b.x -= 0.5 + #print deer.t + + else: + horse2.rest() + deer.rest() + deer.draw(screen) + + + for a in arrows: + a.fly() + a.draw(screen) + + #man.mount(horse2) + #pygame.display.set_icon(screen) + pygame.display.flip() + + diff --git a/filter.py b/filter.py new file mode 100644 index 0000000..6f657ee --- /dev/null +++ b/filter.py @@ -0,0 +1,54 @@ + +import pygame +import sys +import math +import utilities as u +import copy +import random +import noise +import numpy as np + +pygame.init() + +def filter(array,t = 0): + + twave = math.sin(t*0.0005+1) + for x in np.nditer(array[0],op_flags=['readwrite'],flags = ['external_loop']): + x[...]= (x-35+twave*45)/(1.3-twave*0.3) + + for x in np.nditer(array[1],op_flags=['readwrite'],flags = ['external_loop']): + x[...] = (x-35+twave*44)/(1.3-twave*0.3) + + for x in np.nditer(array[2],op_flags=['readwrite'],flags = ['external_loop']): + + x[...] = (x-25+twave*25)/(1.3-twave*0.3) + + + + +if __name__ == "__main__": + screen = pygame.display.set_mode([640,320]) + clock = pygame.time.Clock() + print screen.get_bitsize() + t = 0 + while 1: + t += 100 + for event in pygame.event.get(): + if event.type == pygame.QUIT: sys.exit() + + array = [] + screen.fill([200,200,200]) + + u.circle(screen,(100,100,100),[50,50],50) + + clock.tick() + u.text(screen,10,10,"FPS: %.1f" % clock.get_fps(),(150,150,150)) + + + + array = [pygame.surfarray.pixels_red(screen),pygame.surfarray.pixels_green(screen),pygame.surfarray.pixels_blue(screen)] + + filter(array,t) + #pygame.surfarray.blit_array(screen,array) + pygame.display.flip() + diff --git a/font.py b/font.py new file mode 100644 index 0000000..09e868e --- /dev/null +++ b/font.py @@ -0,0 +1,80 @@ + + +import pygame +import sys +import math +import utilities as u +import copy +import random +import noise +import numpy as np + +pygame.init() + +class GFont(): + font = { + ' ' : [], + 'H' : [['l',0,0,0,4],['l',1.8,0,1.8,4],['l',0,2,1.8,2]], + + + 'a' : [['a',0,2,2,0,360], ['l',2,3,2,4]], + 'b' : [['a',0,2,2,0,360], ['l',0,0,0,3]], + 'c' : [['a',0,2,2,30,330]], + 'd' : [['a',0,2,2,0,360], ['l',2,0,2,4]], + 'e' : [['a',0,2,2,0,340], ['l',0,3,2,3]], + 'f' : [['a',0,0,2,30,180],['l',0,1,0,5], ['l',0,2,2,2]], + 'g' : [['a',0,2,2,0,360], ['l',2,3,2,5], ['a',0,4,2,180,360]], + 'h' : [['a',0,2,2,0,180], ['l',0,0,0,4], ['l',2,3,2,4]], + 'i' : [['l',1,0,1,0.5], ['l',1,2,1,4]], + 'j' : [['l',1,0,1,0.5], ['l',1,2,1,5], ['a',-1,4,2,180,360]], + 'k' : [['l',0,0,0,4], ['l',2,2,0,3], ['l',0,3,2,4]], + 'l' : [['l',0,0,0,3], ['a',0,2,2,180,360]], + 'm' : [['a',0,2,2,0,90], ['l',0,2,0,4], ['l',2,3,2,4], ['l',1,2,1,4],['l',0,2,1,2]], + 'n' : [['a',0,2,2,0,180], ['l',0,2,0,4], ['l',2,3,2,4]], + 'o' : [['a',0,2,2,0,360]], + 'p' : [['a',0,2,2,0,360], ['l',0,3,0,6]], + 'q' : [['a',0,2,2,0,360], ['l',2,2,2,6]], + 'r' : [['a',.5,2,2,90,180],['l',.5,2,.5,4]], + 's' : [['a',0,2,2,0,180], ['a',0,3,2,180,360],['l',0,3,2,4]], + 't' : [['l',0,0,0,3], ['a',0,2,2,180,360],['l',0,2,2,2]], + 'u' : [['l',2,2,2,4], ['a',0,2,2,180,360],['l',0,2,0,3]], + 'v' : [['l',0,2,1,4], ['l',2,2,1,4]], + 'w' : [['l',0,2,0,4], ['a',0,2,2,270,360],['l',2,2,2,3], ['l',1,2,1,4],['l',0,4,1,4]], + 'x' : [['l',0,2,2,4], ['l',0,4,2,2]], + 'y' : [['l',2,2,2,5], ['a',0,2,2,180,360],['l',0,2,0,3],['a',0,4,2,180,360]], + 'z' : [['l',0,2,2,2], ['l',2,2,0,4],['l',0,4,2,4]] + } + def __init__(self,size,width,color=(0,0,0)): + self.s = size + self.w = width + self.color = color + def drawStr(self,surf,st,x,y,size=1): + s = self + for i in range(0,len(st)): + sp = i*2.3 + for f in self.font[st[i]]: + if f[0] == 'a': + pygame.draw.arc(surf,s.color, [f[1]*s.s*size+x+sp*s.s*size, f[2]*s.s*size+y, f[3]*s.s*size, f[3]*s.s*size],math.radians(f[4]),math.radians(f[5]),s.w) + if f[0] == 'l': + pygame.draw.line(surf,s.color, [f[1]*s.s*size+x+sp*s.s*size, f[2]*s.s*size+y],[f[3]*s.s*size+x+sp*s.s*size,f[4]*s.s*size+y],s.w) + + + + + + + +if __name__ == "__main__": + screen = pygame.display.set_mode([640,320]) + gfont = GFont(20,2) + while 1: + + for event in pygame.event.get(): + if event.type == pygame.QUIT: sys.exit() + + screen.fill([255,255,255]) + gfont.drawStr(screen,"by lingdong",10,100) + + + pygame.display.flip() + diff --git a/foo.py b/foo.py new file mode 100644 index 0000000..48f60c9 --- /dev/null +++ b/foo.py @@ -0,0 +1,56 @@ +class IRCReplyModule(object): + + activated=True + moduleHandlerResultList=None + moduleHandlerCommandlist=None + modulename="" + + def __init__(self,modulename): + self.modulename = modulename + + +class SimpleHelloWorld(IRCReplyModule): + + def __init__(self): + super(SimpleHelloWorld,self).__init__('hello world') + + +class A(SimpleHelloWorld): + def __init__(self): + super(A,self).__init__() + +a = SimpleHelloWorld() +b = A() + +""" +def Icon1(): + global icon + color = (70,69,63) + icondeer = creature.Deer(240,0,color,7.6) + icondeer.yo = 240 + icondeer.t = 50 + icondeer.walk() + icon.fill(COLOR_KEY) + icondeer.draw(icon) + iconman = creature.Man(320,0) + iconman.yo=240 + iconman.s=7 + iconman.color=color + iconman.to(4,0,90,1) + iconman.to(5,0,-160,1) + iconman.to(6,0,-80,1) + iconman.to(7,0,0,1) + iconman.to(4,1,10,1) + iconman.to(8,0,110,1) + iconman.to(9,0,-110,1) + iconman.to(1,0,100,1) + iconman.to(0,0,30,1) + iconman.to(3,1,10,1) + iconman.to(8,1,20,1) + iconman.to(9,1,20,1) + iconman.to(10,1,25,1) + iconman.to(10,0,-15,1) + iconman.assets=["bow","arrow"] + iconman.draw(icon) + pygame.display.set_icon(icon) + """ diff --git a/main.py b/main.py new file mode 100644 index 0000000..28237c6 --- /dev/null +++ b/main.py @@ -0,0 +1,592 @@ +import pygame +import sys +import math +import thread +import random +import numpy +import string +import os + +import noise +import tree +import creature +import utilities as u +import filter +import parse + +import pattern +import particle +import font +import pygame._view + +import settings + +pygame.init() +settings.init() + + + +size = width, height = 1280, 320 +buff = 200 +screen = pygame.display.set_mode([width/2,height+50])#,pygame.FULLSCREEN ) +canvas = pygame.Surface([width/2,height]) +pygame.display.set_caption("") + +x = 0 +treeDensity = 32 +landDensity = 32 + + +SPEED = 0.5 + +allloads = width/treeDensity +loaded = 0 + +Ls = [None]*4 +Lrs = [None]*4 + +gamestart = False + +COLOR_KEY = [255,0,255] + +fullscreen = False + +terrain = [0]*4 + +lspds = [0.1,0.2,0.5,1] +totalMade = [0]*4 + +scheme = [(70,69,63),(225,225,210)] + +icon = pygame.Surface([512,512]) +icon.set_colorkey(COLOR_KEY) + +def Icon(): + global icon, scheme + iconhorse = creature.Horse(200,0) + iconhorse.yo = 240 + iconhorse.color = scheme[0] + iconhorse.s=7 + iconman = creature.Man(250,0) + iconman.yo = 250 + iconman.s=6 + iconman.color = scheme[0] + iconman.mount(iconhorse) + for i in range(0,200): + iconman.animate() + iconhorse.animate() + icon.fill(scheme[1]) + pygame.draw.rect(icon,scheme[0],[0,0,512,512],50) + iconman.draw(icon) + iconhorse.draw(icon) + pygame.display.set_icon(icon) + +Icon() +#pygame.image.save(icon,"icon.png") + +def makeBGLayer(n): + global loaded, allloads, terrain, lspds, totalMade + print "Making Background..." + l = pygame.Surface([width+buff*2,height]) + l.fill(COLOR_KEY) + l.set_colorkey(COLOR_KEY) + + + + if terrain[n] == 0: + treesum = width/(0.0+len(Ls)*treeDensity) + for i in range(0,int(treesum)): + thetree = [ random.choice([tree.tree2]), + random.choice([tree.tree1,tree.tree1,tree.tree2]), + random.choice([tree.tree1,tree.tree4,tree.tree3]), + random.choice([tree.tree1,tree.tree4,tree.tree3]) ][n] + thetree(l,random.random()*width+buff,height,(120-n*30)+random.randrange(-10,10)) + loaded += 1 + elif terrain[n] == 1: + + treesum = (width/(0.0+len(Ls)*treeDensity)) + for i in range(0,int(math.ceil(treesum/2.0))): + thetree = [ random.choice([tree.tree1,tree.tree3]), + random.choice([tree.tree1,tree.tree3]), + random.choice([tree.tree1,tree.tree3]), + random.choice([tree.tree1,tree.tree3]) ][n] + thetree(l,random.random()*width+buff,height,(120-n*30)+random.randrange(-10,10)) + loaded += 2 + if n != 3: + poly = [] + poly.append([0,height]) + for i in range(buff,width+buff,landDensity): + poly.append([i,height-makeLand(i*0.05,n*0.5,500-n*90)]) + poly[1][1] = (poly[1][1]-height)/2.0+height + poly[-1][1] = (poly[-1][1]-height)/2.0+height + poly.append([width+buff*2,height]) + pygame.draw.polygon(l,(210-n*20,210-n*20,210-n*20),poly) + + totalMade[n] += 1 + if totalMade[n]%int(lspds[n]*10) == 0: + terrain[n] = (terrain[n]+1) % 2 + + + print(str(loaded)+"/"+str(allloads)) + return l + + +def mt(LN,*args): + global Ls, Lrs, loaded, allloads + allloads = len(args)*(width/(len(Ls)*treeDensity)) + loaded = 0 + if LN == 1: + for a in args: + Ls[a] = makeBGLayer(a) + elif LN == 2: + for a in args: + Lrs[a] = makeBGLayer(a) + +vine = pattern.Vine(0,160) + +screen.fill([240,240,240]) +def loadscreen(): + while loaded < allloads-1 and not gamestart: + #screen.fill([240,240,240]) + for i in range(2): + vine.grow(screen) + pygame.draw.rect(screen,(240,240,240),[0,170,100,20]) + u.text(screen,10,height/2+15,"Loading... "+str(loaded)+"/"+str(allloads),(180,180,180)) + #u.text(screen,100,height/2-14,"Loading... "+str(loaded)+"/"+str(allloads),(250,250,250)) + #u.text(screen,100,height/2-15,"Loading... "+str(loaded)+"/"+str(allloads),(180,180,180)) + u.line(screen,(180,180,180),[0,height/2],[(float(loaded)/allloads)*width/2,height/2],1) + #u.line(screen,(250,250,250),[0,height/2+1],[(float(loaded)/allloads)*width/2,height/2+1],1) + pygame.display.flip() + + +locs = [0,0,0,0] +locrs = [width,width,width,width] + +clock = pygame.time.Clock() + +horse = creature.Horse(100,0) +horse.yo = height +horse.s = 1 +horse.aspd = 0.09 +horse.color = (140,140,140) + +birds = [] +deers = [] +cranes = [] + +arrows = [] + +man = creature.Man(150,0) +man.yo = height +man.s = 0.7 +man.color = (140,140,140) +man.arrows = arrows +man.walk() + + + + + +pctrl = particle.ParticleCtrl() + + + +def makeBirds(n): + global birds + for i in range(0,n): + b = creature.Bird(random.randrange(width/2+10,width/2+60),0) + b.s = 0.5 + b.aspd = 0.3 + b.yo = height + b.color = (140,140,140) + b.dir = random.choice([1,-1]) + birds.append(b) + +def makeDeers(n): + global deers + for i in range(0,n): + r = random.randrange(-5,5) + deer = creature.Deer(width/2+landDensity+50+r*10,0,color = (160+r,160+r,160+r)) + deer.yo = height + deer.s = 1.1 + deer.aspd = 0.15 + deers.append(deer) + + +def makeCranes(n): + global cranes + for j in range(0,n): + r = random.randrange(-5,5) + crane = creature.Crane(width/2+landDensity+random.randrange(0,200),0) + crane.color = (180+r,180+r,180+r) + crane.yo = height-150-120*random.random() + crane.s = 0.5+random.random()*0.2 + crane.aspd = 0.05 + crane.dir = -1 + crane.t = (j/5.0)*200 + cranes.append(crane) + +makeBirds(10) + +def makeLand(n,m=0,maxheight = 20): + return max(noise.noise(n*0.1,m*0.5)*maxheight,2)-2 +#landDensity = 64 #inited before! +land = [0]*(((width)/2)/landDensity+2) +landloc = 0 +landni = 0 +for landni in range(0,len(land)): + land[landni]=makeLand(landni,maxheight=20+terrain[3]*120) + +def onLandY(instx): + if x== 0:ep = -0.01 + else:ep = 0.01 + lastAlt = land[int(((x-ep)%landDensity + instx)//landDensity)] + nextAlt = land[int(((x-ep)%landDensity + instx)//landDensity)+1] + return lastAlt+(nextAlt-lastAlt)*((((x-ep)%landDensity + instx)%landDensity)/landDensity) + + +gfont = font.GFont(10,2,) + +box = pygame.Surface([240,50]) +box.fill([0,0,0]) +box.set_alpha(50,pygame.RLEACCEL) + +keyseq = [] +commandlist = ["set time","set speed","spawn","fullscreen","restart","set terrain","set tree density","eval"] +T = 0 + + +def exe(command): + global T, SPEED,fullscreen,terrain,totalMade,landni,land, treeDensity + try: + com = parse.parse(command.split("-")[0],commandlist)[0] + par = command.split("-")[1:] + for i in range(0,len(par)): + par[i] = par[i].strip() + + if com == "set time": + T = int(par[0]) + settings.msg = ["TIME SET TO "+par[0]+".",settings.msgt] + elif com == "set speed": + SPEED = float(par[0]) + settings.msg = ["SPEED SET TO "+par[0]+".",settings.msgt] + elif com == "restart": + #os.execv(__file__, sys.argv) + python = sys.executable + os.execl(python, python, * sys.argv) + elif com == "fullscreen": + if len(par) == 0: + fullscreen = not fullscreen + else: + fullscreen = [False,True][int(par[0])] + if fullscreen: + pygame.display.set_mode([width/2,height+50],pygame.FULLSCREEN) + + else: + pygame.display.set_mode([width/2,height+50]) + pygame.display.set_caption("") + settings.msg = ["FULLSCREEN "+["OFF.","ON."][fullscreen],settings.msgt] + elif com == "spawn": + animal = par[0] + xn = int(par[1]) + if animal == "bird": + makeBirds(xn) + elif animal == "deer": + makeDeers(xn) + elif animal == "crane": + makeCranes(xn) + elif animal == "unicorn": + for i in range(xn): + r = random.randrange(-5,5) + unicorn = creature.Unicorn(width/2+landDensity+50+r*10,0) + unicorn.yo = height + unicorn.s = 1.0 + unicorn.aspd = 0.2 + unicorn.dir = -1 + unicorn.spd = 2 + deers.append(unicorn) + settings.msg = [str(xn)+" "+animal+" SPAWNED.",settings.msgt] + elif com == "set terrain": + #settings.msg = ["PLEASE WAIT",settings.msgt] + if int(par[0]) > 1: + raise + terrain = [int(par[0])]*4 + totalMade = [0]*4 + for landni in range(0,len(land)): + land[landni]=makeLand(landni,maxheight=20+terrain[3]*120) + mt(1, 3,2,1,0) + mt(2, 3,2,1,0) + settings.msg = ["TERRAIN SET TO "+par[0]+".",settings.msgt] + elif com == "set tree density": + print terrain + terrain = [terrain[3]]*4 + totalMade = [0]*4 + for landni in range(0,len(land)): + land[landni]=makeLand(landni,maxheight=20+terrain[3]*120) + treeDensity = int(width/float(par[0])) + mt(1, 3,2,1,0) + mt(2, 3,2,1,0) + elif com == "eval": + settings.msg = [str(eval(par[0])),settings.msgt] + except Exception, e: + print "%s" % e + settings.msg = ["COMMAND NOT EXECUTED.",settings.msgt] + +def main(): + global x, lspds, locs, gamestart, landloc, landni, fullscreen, birds, terrain, keyseq, T + gamestart = True + showconsole = False + while 1: + canvas.fill([240,240,240]) + for event in pygame.event.get(): + if event.type == pygame.QUIT: sys.exit() + man.keyupdowncontrol(event,horse) + + if event.type == pygame.KEYDOWN: + if showconsole: + k = pygame.key.name(event.key) + #print k + if k == "return": + exe("".join(keyseq)) + showconsole = False + elif k == "space": + keyseq.append(" ") + elif k == "-": + keyseq.append("-") + elif len(k) == 1: + keyseq.append(k) + elif event.key == pygame.K_BACKSPACE : + if len(keyseq) > 0: + keyseq.pop() + + if event.key == pygame.K_SLASH: + showconsole = not showconsole + if showconsole: + settings.msg = ["CONSOLE READY.",settings.msgt] + else: + settings.msg = ["",settings.msgt] + keyseq = [] + if event.key == pygame.K_f and not showconsole: + fullscreen = not fullscreen + if fullscreen: + pygame.display.set_mode([width/2,height+50],pygame.FULLSCREEN) + else: + pygame.display.set_mode([width/2,height+50]) + pygame.display.set_caption("") + #print(pygame.key.get_pressed()) + for i in range(0,len(Ls)): + if i == 2: + for c in cranes: + c.draw(canvas) + if i == 3:#+terrain: + """ + gfont.s = 10 + + gfont.w = 1 + gfont.color = (120,120,120) + + gfont.drawStr(canvas,"Hermit",300-x*0.7,260) + + gfont.s = 5 + gfont.w = 1 + gfont.color = (120,120,120) + gfont.drawStr(canvas,"by lingdong",450-x*0.7,280) + + """ + for d in deers: + d.draw(canvas) + + horse.draw(canvas) + man.draw(canvas) + for a in arrows: + a.draw(canvas) + for b in birds: + b.simpDraw(canvas) + + pctrl.draw(canvas) + + if Ls[i] != None: + canvas.blit(Ls[i],[locs[i]-x*lspds[i]-buff,0]) + + if locs[i]-x*lspds[i] < -width-buff: + locs[i] += width*2 + Ls[i] = None + thread.start_new_thread(mt,(1, i)) + + + if Lrs[i] != None: + canvas.blit(Lrs[i],[locrs[i]-x*lspds[i]-buff,0]) + + if locrs[i]-x*lspds[i] < -width-buff: + locrs[i] += width*2 + Lrs[i] = None + thread.start_new_thread(mt,(2, i)) + clock.tick() + T += 1 + u.text(canvas,10,10,"FPS: %.1f" % clock.get_fps(),(160,160,160)) + + man.keyholdcontrol() + + if (0 or pygame.key.get_pressed()[pygame.K_RIGHT]) and not man.status[0].endswith("ing"): + for a in arrows: + a.x -= SPEED + for b in birds: + b.x -= SPEED + for p in pctrl.particles: + p.x -= SPEED + for d in deers: + d.x-=SPEED*0.5 + for c in cranes: + c.x-=SPEED + x+=SPEED + horse.walk() + + if random.random()<0.0005: + makeBirds(random.randrange(6,12)) + if random.random() < 0.0005 and terrain[3] == 0: + makeDeers(1) + if random.random() < 0.001 and terrain[3] == 1: + makeCranes(random.randrange(1,5)) + + + else: + horse.rest() + + + u.polygon(canvas,(130,130,130),[[0,height]]+[[landloc-x+i*landDensity,height-land[i]] for i in range(0,len(land))]+[[width/2,height]]) + + + if -x+landloc<-landDensity: + landni += 1 + land.append(makeLand(landni,maxheight=20+terrain[3]*120)) + land.pop(0) + landloc += landDensity + + + man.yo = height-20-onLandY(man.x) + horse.yo = height-30-onLandY(horse.x) + + + + for d in deers: + + d.yo = height-30-onLandY(max(min(d.x,width/2),0)) + + if noise.noise(T*0.001,deers.index(d))<0.5: + d.x -= d.spd + d.walk() + else: + d.rest() + + if d.x<-100: + deers.remove(d) + + for c in cranes: + c.x -= 2*c.s + c.fly() + if c.x<-100: + cranes.remove(c) + + + for a in arrows: + #a.fly() + #print(a.x) + if a.x > width/2 or a.x < -10 or height-onLandY(a.x) >= a.calcHead()[1]: + a.fly() + else: + a.v[0] = 0 + a.v[1] = 0 + a.flicker = 0 + if a.x > width/2: + arrows.remove(a) + + for b in birds: + if b.health > 0: + if ((abs(man.x - b.x) < 100 and random.random()<0.05) or random.random()<0.0002) and b.on == 0: + b.on = 1 + ra = math.pi/20.0+random.random()*math.pi/6.0*2.1 + rl = random.choice([3,4,5]) + b.v=[rl*math.cos(ra),-rl*math.sin(ra)] + if b.on == 1: + b.simpFly() + + if abs(man.x - b.x) > 160 and random.random()<1: + b.v[1] = min(b.v[1]+0.05,0.4) + if b.y >= 2: + b.on = 0 + + else: + b.rest() + if 0 < b.x < width/2: + b.yo=height-3-onLandY(b.x) + + for a in arrows: + #print(u.dist(a.x,a.y,b.x,b.y+b.yo)) + if u.dist(a.x,a.y,b.x,b.y+b.yo) < b.s*30 and a.v[0] > 0: + a.v[0]/= 2 + b.arrow = a + b.health = 0 + b.x = a.calcFeather()[0] + b.y = a.calcFeather()[1] - b.yo + for i in range(0,12): + pctrl.particles.append(particle.Particle(a.calcFeather()[0],a.calcFeather()[1],[8*(random.random()-0.5),8*(random.random()-0.3)])) + + if b.x<0 or b.x>width or b.yo<0: + birds.remove(b) + else: + b.fall() + pctrl.emit() + + + man.animate() + horse.animate() + #array = [] + #screen.unlock() + screen.blit(canvas,[0,0]) + + + + reflection = canvas#pygame.transform.flip(canvas,False,True) + pygame.draw.rect(screen,(180,180,180),[0,height,width/2,50]) + for i in range(0,2*(screen.get_height()-height),2): + screen.blit(reflection,[(math.sin(i*0.5))*i*0.5+(noise.noise(pygame.time.get_ticks()*0.001,i*0.2)-0.5)*20,height+i-1],(0,height-i,width/2,1)) + + + + if settings.msg[0] != "": + screen.blit(box,[5,height+33-showconsole*20]) + u.text(screen,10,height+35-showconsole*20,settings.msg[0],(240,240,240)) + + + if settings.msg[1] <= 0 and not showconsole: + settings.msg[0] = "" + else: + settings.msg[1]-=1 + + if showconsole: + input = "".join(keyseq) + u.text(screen,10,height+25,">"+input.lower(),(240,240,240)) + u.text(screen,10,height+35," "+" | ".join(parse.parse(input.split("-")[0],commandlist)[:3]),(240,240,240)) + array = [pygame.surfarray.pixels_red(screen),pygame.surfarray.pixels_green(screen),pygame.surfarray.pixels_blue(screen)] + filter.filter(array,T) + array = [] + + #icon.blit(screen,[0,0],[0,0,512,512]) + #pygame.display.set_icon(icon) + pygame.display.flip() +#t1 = thread.start_new_thread( loadscreen, () ) +#mt(1, 3,2,1,0) + +t1 = thread.start_new_thread( mt, (1, 3,2,1,0) ) +loadscreen() + +while loaded=1.0): xi+=1; xf-=1 + if (yf>=1.0): yi+=1; yf-=1 + if (zf>=1.0): zi+=1; zf-=1 + return r + +def noiseDetail(lod, falloff): + if lod>0:perlin_octaves=lod + if falloff>0:perlin_amp_falloff=falloff + + +class LCG(): + def __init__(self): + self.m = 4294967296.0 + self.a = 1664525.0 + self.c = 1013904223.0 + self.seed = self.z = None + def setSeed(self,val=None): + self.z = self.seed = (math.random()*self.m if val == None else val) >> 0 + def getSeed(self): + return self.seed + def rand(self): + self.z = (self.a * self.z + self.c) % self.m + return self.z/self.m + + +def noiseSeed(seed): + lcg = LCG() + lcg.setSeed(seed) + perlin = [] + for i in range(0,PERLIN_SIZE+1): + perlin.append(lcg.rand()) + + diff --git a/parse.py b/parse.py new file mode 100644 index 0000000..f9d42c5 --- /dev/null +++ b/parse.py @@ -0,0 +1,42 @@ +def parse(command,allcommands): + allcommands.sort() + results = [] + if command == "": + return results + command = command.strip() + for c in allcommands: + if "".join([x[0] for x in c.split(" ")])==command: + if not c in results: + results.append(c) + for c in allcommands: + if c.startswith(command): + if not c in results: + results.append(c) + + for c in allcommands: + if (not False in [c.split(" ")[min(i,len(c.split(" "))-1)].startswith(command.split(" ")[i]) for i in range (len(command.split(" ")))]): + if not c in results: + results.append(c) + for c in allcommands: + if "".join(c.split(" ")).startswith(command): + if not c in results: + results.append(c) + for c in allcommands: + if "".join([x[0] for x in c.split(" ")]).startswith(command): + if not c in results: + results.append(c) + for c in allcommands: + for d in c.split(" "): + if d.startswith(command): + if not c in results: + results.append(c) + for c in allcommands: + if command in "".join(c.split(" ")[i][0] for i in range(0,len(c.split(" ")))): + if not c in results: + results.append(c) + + return results +if __name__ == "__main__": + while 1: + print(parse(raw_input("?"),[ + "kill horse","kill dog","khan","kill cat","open door","eat meat","restore full health","roast full chicken","raw fury","eat shit","eat mother","kill motherfucker"])) diff --git a/particle.py b/particle.py new file mode 100644 index 0000000..59600ff --- /dev/null +++ b/particle.py @@ -0,0 +1,41 @@ +import pygame +import sys +import math +import utilities as u +import copy +import random +import noise + +pygame.init() + +class Particle(): + def __init__(self,x,y,v): + self.x = x + self.y = y + self.v = v + self.g = 0.2 + + self.color = (100,100,100) + self.s = 1 + self.life = 50 + + def fly(self): + self.x += self.v[0] + self.y += self.v[1] + self.v[1] += self.g + self.life -= 1 + def draw(self,surf): + u.circle(surf,self.color,[self.x,self.y],self.s/2) + +class ParticleCtrl(): + def __init__(self): + self.particles = [] + def emit(self): + for p in self.particles: + p.fly() + if p.life <= 0: + self.particles.remove(p) + + def draw(self,surf): + for p in self.particles: + p.draw(surf) diff --git a/pattern.py b/pattern.py new file mode 100644 index 0000000..927ab70 --- /dev/null +++ b/pattern.py @@ -0,0 +1,78 @@ + +import pygame +import sys +import math +import utilities as u +import copy +import random +import noise + +pygame.init() + +class Dot(): + def __init__(self,x,y,a,r,adir=1,color = (180,180,180)): + self.x = x + self.y = y + self.a = a + self.adir = adir + self.r = r + self.s = 1 + self.color = color + self.t = 0 + self.f = 0 + def crawl(self): + self.x += self.s*math.cos(self.a) + self.y -= self.s*math.sin(self.a) + self.t += 1 + if self.t > self.r * 7: + try: + self.a += 0.0005*self.t**2*self.adir/self.r**3 + except:pass + else: + pass + #self.a+=1*(noise.noise(self.t*0.01)-0.5) + #self.r -= 1 + def draw(self,surf): + #u.circle(surf,(245,245,245),[self.x,self.y+1],self.s/2) + u.circle(surf,self.color,[self.x,self.y],self.s/2) + +class Vine(): + def __init__(self,x,y,color = (180,180,180)): + self.color =color + self.dots = [] + self.dots.append(Dot(x,y,0,10,color = self.color)) + + def grow(self,surf): + + for d in self.dots: + d.crawl() + d.draw(surf) + if d.t == int(d.r * 7) and random.random() < 0.5 and d.r > 2: + if len(self.dots) < 40: + self.dots.append(Dot(d.x,d.y,0,d.r*(0.5+0.7*random.random()),-d.adir,self.color)) + + if random.random() < 0.02 and d.r*50 > d.t > d.r * 6 and d.f < d.r/5 and d.r > 3: + if len(self.dots) < 40: + self.dots.append(Dot(d.x,d.y,0.5*(random.random()-0.5),d.r*(0.5+0.6*random.random()),-d.adir,self.color)) + d.f += 1 + + + if abs(d.t) > d.r*50: + self.dots.remove(d) + if len(self.dots) < 1: + self.dots.append(Dot(d.x,d.y,0.5*(random.random()-0.5),d.r*(0.5+0.6*random.random()),-d.adir,self.color)) + + + + +if __name__ == "__main__": + screen = pygame.display.set_mode([640,320]) + screen.fill([240,240,240]) + vine = Vine(0,160) + while 1: + for event in pygame.event.get(): + if event.type == pygame.QUIT: sys.exit() + u.text(screen,20,160+5,"Loading... ",(180,180,180)) + #screen.fill([240,240,240]) + vine.grow(screen) + pygame.display.flip() diff --git a/projectile.py b/projectile.py new file mode 100644 index 0000000..2e9fbde --- /dev/null +++ b/projectile.py @@ -0,0 +1,51 @@ +import pygame +import sys +import math +import utilities as u +import copy +import random +import noise + +pygame.init() + +class Arrow(): + def __init__(self,x,y): + self.color = (150,150,150) + self.x=x + self.y=y + self.l=20 + self.a=0 + self.spd=10 + self.v=[self.spd,0] + self.g=0.2 + #self.body = None + #self.state = 0 + + self.flicker = 1 + def calcA(self): + return math.degrees(math.atan2(self.v[1],self.v[0])) + + def calcV(self): + return [self.spd*math.cos(math.radians(self.a)), -self.spd*math.sin(math.radians(self.a))] + + + def calcHead(self): + return self.x+self.l*math.cos(math.radians(self.a)), self.y+self.l*math.sin(math.radians(self.a)) + + def calcFeather(self): + return self.x+self.l*0.3*math.cos(math.radians(self.a)), self.y+self.l*0.3*math.sin(math.radians(self.a)) + + + def draw(self,surf): + u.line(surf,[245,245,245],[self.x,self.y],self.calcFeather(),3) + u.line(surf,self.color,[self.x,self.y],self.calcHead(),random.randrange(0,2)*self.flicker+1) + + + def fly(self): + self.a=self.calcA() + + self.x += self.v[0] + self.y += self.v[1] + self.v[1]+=self.g + + diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..8b35146 --- /dev/null +++ b/settings.py @@ -0,0 +1,5 @@ + +def init(): + global msg,msgt + msg = ["",0] + msgt = 50 diff --git a/tree.py b/tree.py new file mode 100644 index 0000000..965bb63 --- /dev/null +++ b/tree.py @@ -0,0 +1,169 @@ +import pygame +import sys +import math +import random +import utilities as u + + +def drawTree(**p): + if p['depth'] < p['maxdepth']: + + if p['height'] <1:return + + dep = p['depth'] + p['width'] *= p['dwidth'](dep) + + + x0 = p['x']+math.cos(p['angle'])*p['trunk'] + y0 = p['y']-math.sin(p['angle'])*p['trunk'] + u.line(p['surf'],p['color'],[p['x'],p['y']],[x0,y0],p['width']) + + + p['width'] *= p['dwidth'](dep) + a1 = p['angle']-p['opening']*p['dopening'](dep) + a2 = p['angle']+p['opening']*p['dopening'](dep) + + h1 = p['height'] * p['dheight'](dep) + x1 = x0+math.cos(a1)*h1 + y1 = y0-math.sin(a1)*h1 + + h2 = p['height'] * p['dheight'](dep) + x2 = x0+math.cos(a2)*h2 + y2 = y0-math.sin(a2)*h2 + + #u.text(p['surf'],x1,y1,str(dep),(0,150,0)) + #u.text(p['surf'],x2,y2,str(dep),(0,150,0)) + + u.line(p['surf'],p['color'],[x0,y0],[x1,y1],p['width']) + u.line(p['surf'],p['color'],[x0,y0],[x2,y2],p['width']) + + + p['trunk'] *= p['dtrunk'](dep) + + p['depth'] += .5 + p['x'],p['y'],p['height'],p['angle'] = x1,y1,h1,a1-p['dangle'](dep) + + drawTree(**p) + + + p['depth'] += .5 + p['x'],p['y'],p['height'],p['angle'] = x2,y2,h2,a2+p['dangle'](dep) + drawTree(**p) + else: + return + + +def tree1(surf,x,y,shade=0): + drawTree(surf = surf, + x = x, + y = y, + angle = math.pi/2, + dangle = lambda dep: -(random.random()-0.5)*math.pi/3, + + trunk = 50, + dtrunk = lambda dep: 0.8*random.random(), + + width = 8, + dwidth = lambda dep: random.random()*0.2+0.8, + + height = 50, + dheight = lambda dep: random.random()*0.6+0.4, + + opening = math.pi/6, + dopening = lambda dep: 0.8 + random.random()*0.5, + + color = (100+shade,100+shade,100+shade), + depth = 0, + maxdepth = 10 + ) + +def tree2(surf,x,y,shade=0): + drawTree(surf = surf, + x = x, + y = y, + angle = math.pi/2, + dangle = lambda dep: 0, + + trunk = 20, + dtrunk = lambda dep: 0.9, + + width = 10, + dwidth = lambda dep: random.random()*0.3+0.7, + + height = 100, + dheight = lambda dep: random.random()*0.6+0.3, + + opening = math.pi/3, + dopening = lambda dep: ((dep*2)%2)*(0.8+random.random()*0.4),#*(dep<2), + + color = (100+shade,100+shade,100+shade), + depth = 0, + maxdepth = 12 + ) + +def tree3(surf,x,y,shade=0): + drawTree(surf = surf, + x = x, + y = y, + angle = math.pi/2, + dangle = lambda dep: -(random.random()-0.5)*math.pi/3, + + trunk = 0, + dtrunk = lambda dep: 0.8*random.random(), + + width = 6, + dwidth = lambda dep: random.random()*0.2+0.8, + + height = 100, + dheight = lambda dep: random.random()*0.7+0.2, + + opening = math.pi/3, + dopening = lambda dep: random.random()*2-1, + + color = (100+shade,100+shade,100+shade), + depth = 0, + maxdepth = 10 + ) + + + +def tree4(surf,x,y,shade=0): + drawTree(surf = surf, + x = x, + y = y, + angle = math.pi/2, + dangle = lambda dep: (-math.pi/6)+((random.random()-0.5)*(dep))*2, + + trunk = 50, + dtrunk = lambda dep: 0.8*random.random(), + + width = 8, + dwidth = lambda dep: random.random()*0.2+0.8, + + height = 50, + dheight = lambda dep: random.random()*0.5+0.5, + + opening = math.pi/5, + dopening = lambda dep: 0.8 + random.random()*0.5*dep*2, + + color = (100+shade,100+shade,100+shade), + depth = 0, + maxdepth = 10 + ) + +if __name__ == "__main__": + screen = pygame.display.set_mode([800,500]) + screen.fill([240,240,240]) + for i in range(0,4): + [tree1,tree2,tree3,tree4][i](screen,100+i*200,500,30) + + while 1: + for event in pygame.event.get(): + if event.type == pygame.QUIT: sys.exit() + if event.type == pygame.KEYDOWN: + if event.key==pygame.K_SPACE: + print event.key==pygame.K_SPACE + screen.fill([240,240,240]) + for i in range(0,4): + [tree1,tree2,tree3,tree4][i](screen,100+i*200,500,30) + pygame.display.flip() diff --git a/utilities.py b/utilities.py new file mode 100644 index 0000000..848884f --- /dev/null +++ b/utilities.py @@ -0,0 +1,36 @@ +import pygame +import math + +pygame.init() +sysf = pygame.font.SysFont(pygame.font.get_default_font(),16) + +def lmap(f,l): + return list(map(f,l)) + +def toInt(n): + return int(n) +def line(surface,color,start_pos,end_pos,width=1): + return pygame.draw.line(surface,color,lmap(toInt,start_pos),lmap(toInt,end_pos),int(math.ceil(width))) + +def polygon(surface,color,pointlist,width=0): + #print pointlist + return pygame.draw.polygon(surface,color,lmap(lambda l: lmap(toInt, l),pointlist),int(round(width))) + +def circle(surface,color,pos,radius,width=0): + return pygame.draw.circle(surface,color,lmap(toInt,pos),int(radius),int(round(width))) + + +def text(surf,x,y,t,color=(0,0,0),Font=sysf): + fs = Font.render(t,False,color) + surf.blit(fs,[x,y]) + +def triwave(t,a=2*math.pi): + t = float(t) + a = float(a) + return (2/a)*(t-a*int(t/a+0.5))*(-1)**int(t/a+0.5) + +def trapwave(x): + return ((8*math.sqrt(2))/(math.pi*math.pi)) * (math.sin(x)+math.sin(3*x)/9.0) + +def dist(x1,y1,x2,y2): + return math.sqrt((x1-x2)**2+(y1-y2)**2)