Created
May 26, 2012 19:56
-
-
Save DM-/2795106 to your computer and use it in GitHub Desktop.
Game code layout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #remember top left is 0,0 left right is X is first,up down is Y is second | |
| # whitespaces, dataflow, and DRY, also use a vector class for positions | |
| # newpos = simulateMovement(char, dir); if gmap[newpos.x][newpos.y] = stone | |
| import sys,pygame | |
| pygame.init() | |
| Grass = pygame.image.load("grass.png") | |
| Person = pygame.image.load("person.png") | |
| class Vector: | |
| def __init__(self, x, y): | |
| self.x = x; self.y = y | |
| def __sub__(self,D): | |
| x = self.x-D.x | |
| y = self.y-D.y | |
| return Vector(x,y) | |
| def __add__(self,A): | |
| x = self.x+A.x | |
| y = self.y+A.y | |
| return Vector(x,y) | |
| def __str__(self): | |
| x= str(self.x)+" "+str(self.y) | |
| return x | |
| def __repr__(self): | |
| x=[self.x,self.y] | |
| return str(x) | |
| def __cmp__(self,O): | |
| if self.x==O.x and self.y==O.y : | |
| return 0 | |
| else: | |
| return -1 | |
| # | |
| N=Vector(-1,0) | |
| S=Vector(1,0) | |
| W=Vector(0,-1) | |
| E=Vector(0,1) | |
| NW=Vector(-1,-1) | |
| NE=Vector(-1,1) | |
| SW=Vector(1,-1) | |
| SE=Vector(1,1) | |
| Dirs = [N,S,W,E,NW,NE,SW,SE] | |
| #Character class is for people,no ai so not monsters | |
| class char(): | |
| def __init__(self,gmap,X=0,Y=0,sign="0",name="Zeta"): | |
| self.posi=Vector(X,Y) | |
| self.sign=sign | |
| self.name=name | |
| self.locate=gmap | |
| def Go(self,dir): | |
| if CheckDir(self,dir,self.locate): | |
| self.posi += dir | |
| #Check movement before giving go-ahead,will add danger detection and stuff here | |
| #also maybe perception based noticing | |
| #It recognizes when two chars meet and map edge | |
| def CheckDir(char,dire,gmap): | |
| posi=char.posi | |
| dimi=gmap.dim[:] | |
| OtPosi=[] | |
| for i in gmap.InHere: | |
| OtPosi.append([i,i.posi]) | |
| if (posi+dire).x in [-1,dimi[0],dimi[1]] or (posi+dire).y in [-1,dimi[0],dimi[1]]: | |
| return False | |
| for I in OtPosi: | |
| if I[0]==char: | |
| pass | |
| elif (posi+dire)==I[1]: | |
| meet(char,i,gmap,posi+dire) | |
| return False | |
| return True | |
| #Function called when two chars meet: | |
| def meet(char1,char2,gmap,posi): | |
| floor=gmap.content[posi.x][posi.y] | |
| print("They met") | |
| #game map class | |
| # Done: need to be changed to know who is on it,and make fun to display them all | |
| # Can display without people, with only main char, and with all people on the | |
| # map | |
| class gmap(): | |
| def __init__(self,tomap=None,X=5,Y=5,char=None): | |
| tomap=[] | |
| char=[] | |
| if tomap: | |
| self.content=tomap | |
| self.dim=[0,0] | |
| for q in tomap: | |
| self.dim[1] +=1 | |
| for i in tomap[0]: | |
| self.dim[0] +=1 | |
| else: | |
| self.content=[] | |
| for q in range(X): | |
| self.content.append([]) | |
| for i in self.content: | |
| for z in range(Y): | |
| i.append("X") | |
| self.dim=[X,Y] | |
| self.InHere=char | |
| self.size=width,height=self.dim[1]*10,self.dim[0]*10 | |
| self.Screen = pygame.display.set_mode(self.size) | |
| def display(self,WhatToDisplay="Z"): | |
| if WhatToDisplay=="Z": | |
| WhatToDisplay=self.content | |
| for i in range(self.dim[1]): | |
| for z in range(self.dim[0]): | |
| if WhatToDisplay[i][z] == "X": | |
| self.Screen.blit(Grass,(z*10,i*10)) | |
| else: | |
| self.Screen.blit(Person,(z*10,i*10)) | |
| def MapWithChar(self,char): | |
| self.copy=list() | |
| for i in self.content: | |
| self.copy.append(i[:]) | |
| self.copy[char.posi.x][char.posi.y]=char.sign | |
| self.display(self.copy) | |
| pygame.display.update() | |
| def MapWithAll(self): | |
| self.copy=list() | |
| for i in self.content: | |
| self.copy.append(i[:]) | |
| for char in self.InHere: | |
| self.copy[char.posi.x][char.posi.y]=char.sign | |
| self.display(self.copy) | |
| pygame.display.update() | |
| def InputHandler(inpu,char,gmap,game): | |
| inpu=str(inpu) | |
| inpu=inpu.lower() | |
| if inpu=="False": | |
| print("please type something") | |
| else: | |
| inpu=list(inpu) | |
| inpu.extend([0,0,0,0,0]) | |
| if inpu[0]=="g": | |
| if inpu[1]=="n": | |
| if inpu[2]=="e": | |
| char.Go(NE) | |
| return 1 | |
| elif inpu[2]=="w": | |
| char.Go(NW) | |
| return 1 | |
| else: | |
| char.Go(N) | |
| return 1 | |
| elif inpu[1]=="s": | |
| if inpu[2]=="e": | |
| char.Go(SE) | |
| return 1 | |
| elif inpu[2]=="w": | |
| char.Go(SW) | |
| return 1 | |
| else: | |
| char.Go(S) | |
| return 1 | |
| elif inpu[1]=="e": | |
| char.Go(E) | |
| return 1 | |
| elif inpu[1]=="w": | |
| char.Go(W) | |
| return 1 | |
| elif inpu[0]=="h": | |
| if inpu[1]=="e": | |
| print("""Hello this is the help file,to move around type the letter g followed by the initial of the direction you want to go""") | |
| elif inpu[0]=="q": | |
| game.State=0 | |
| else: | |
| print("Cannot compute,type in something correctly") | |
| class Game(): | |
| def __init__(self,Map=[0,20,20],Hero="Zeta"): | |
| self.map=gmap(*Map) | |
| self.hero=char(gmap=self.map,name=Hero,X=5,Y=5) | |
| self.enemy=char(gmap=self.map,name="Evil",sign="O") | |
| self.map.InHere.extend([self.hero,self.enemy]) | |
| self.State=1 | |
| def playmove(self): | |
| print("""Hello,Welcome to Game,type help for help""") | |
| while self.State==1: | |
| self.map.MapWithAll() | |
| pygame.event.pump() | |
| WutDo=raw_input("What direction?") | |
| InputHandler(WutDo,self.hero,self.map,self) | |
| def main(): | |
| now=Game() | |
| now.playmove() | |
| if __name__ == '__main__': | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://imgur.com/a/zo1hX |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://imgur.com/a/GOgU0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

