用python写了一个点灯的小游戏, 谢谢iceboy的reversi.py让我来篡改。
#!/usr/bin/python import Tkinter def init(): global _nonecolor, _wallcolor, _linecolor, _Bulb_lightcolor, _Default_bulbcolor, \ _Wrong_bulbcolor, _Default_numcolor, _Wrong_numcolor, _Defaultfont, _Row, map, _state _nonecolor='white' _wallcolor='black' _linecolor='gray' _Bulb_lightcolor='lightyellow' _Default_bulbcolor='green' _Wrong_bulbcolor='darkred' _Default_numcolor='white' _Wrong_numcolor='red' _Defaultfont='Lucida Console' _Row=8 map=None _state=gamestate.start def main(): #global map init() #map=loadmap('1.txt') app = App(None) app.title("Bulb") app.mainloop() class gamestate: none, start=range(2) class btype: none, wall, bulb=range(3) class block(): def __init__(self): self.type=btype.wall self.isBright=False self.number=-1 self.bulbcolor=_Default_bulbcolor self.numcolor=_Default_numcolor #self. def initMap(): m=[[]]*_Row for i in range(0,_Row): m[i]=[block()]*_Row return m def loadmap(fn): global _Row fd=open(fn,'r') lns=fd.read().replace('\r','\n').replace('\n\n','\n').split('\n') _Row=len(lns) if len(lns[0])>_Row: _Row=len(lns[0]) m=initMap() for i in range(0,_Row): for j in range(0,_Row): if len(lns)<i+1 or len(lns[i])<j+1: m[i][j]=block() m[i][j].type=btype.wall continue if lns[i][j]=='.': m[i][j]=block() m[i][j].type=btype.none elif lns[i][j]=='-': m[i][j]=block() m[i][j].type=btype.wall else: m[i][j]=block() m[i][j].type=btype.wall m[i][j].number=int(lns[i][j]) print _Row fd.close() return m def valid(x, y): if _state!=gamestate.start: return False return 0<=x<_Row and 0<=y<_Row and map[y][x].type!=btype.wall def getNeighbor(x, y): Neighbor=0 if valid(x-1,y) and map[y][x-1].type==btype.bulb: Neighbor+=1 if valid(x,y-1) and map[y-1][x].type==btype.bulb: Neighbor+=1 if valid(x+1,y) and map[y][x+1].type==btype.bulb: Neighbor+=1 if valid(x,y+1) and map[y+1][x].type==btype.bulb: Neighbor+=1 return Neighbor def refreshmap(): for y in range(0,_Row): for x in range(0,_Row): map[y][x].isBright=False map[y][x].bulbcolor=_Default_bulbcolor map[y][x].numcolor=_Default_numcolor for y in range(0,_Row): for x in range(0,_Row): if map[y][x].type==btype.bulb: map[y][x].isBright=True for sy in range(y,-1,-1): #up if map[sy][x].type==btype.wall: break map[sy][x].isBright=True if sy!=y and map[sy][x].type==btype.bulb: map[y][x].bulbcolor=_Wrong_bulbcolor map[sy][x].bulbcolor=_Wrong_bulbcolor for sx in range(x,-1,-1): #left if map[y][sx].type==btype.wall: break map[y][sx].isBright=True if sx!=x and map[y][sx].type==btype.bulb: map[y][x].bulbcolor=_Wrong_bulbcolor map[y][sx].bulbcolor=_Wrong_bulbcolor for sy in range(y,_Row): #down if map[sy][x].type==btype.wall: break map[sy][x].isBright=True if sy!=y and map[sy][x].type==btype.bulb: map[y][x].bulbcolor=_Wrong_bulbcolor map[sy][x].bulbcolor=_Wrong_bulbcolor for sx in range(x,_Row): #right if map[y][sx].type==btype.wall: break map[y][sx].isBright=True if sx!=x and map[y][sx].type==btype.bulb: map[y][x].bulbcolor=_Wrong_bulbcolor map[y][sx].bulbcolor=_Wrong_bulbcolor elif map[y][x].number>=0: if getNeighbor(x,y)>map[y][x].number: map[y][x].numcolor=_Wrong_numcolor a=None return def chkwin(): #count_not_bright=0 for y in range(0,_Row): for x in range(0,_Row): if map[y][x].type==btype.wall and map[y][x].numcolor==_Wrong_numcolor: return False if map[y][x].type==btype.bulb and map[y][x].bulbcolor==_Wrong_bulbcolor: return False if map[y][x].type!=btype.wall and not map[y][x].isBright: return False return True class Board(Tkinter.Canvas): def __init__(self, parent, label_var): Tkinter.Canvas.__init__(self, parent) self.parent = parent self.label_var = label_var self.bind("<Configure>", self.configure) self.bind("<Motion>", self.motion) self.bind("<Button-1>", self.click) self.configured = False self.new_game() def configure(self, event): self.bsize = float(min(event.width, event.height)) / float(_Row) - 1 print self.bsize self.bleft = (event.width - self.bsize * _Row) / 2 self.btop = (event.height - self.bsize * _Row) / 2 self.configured = True self.redraw() def redraw(self): if not self.configured: return self.delete(Tkinter.ALL) if self.bsize >= _Row: self.draw_bg() for y in range(0, _Row): for x in range(0, _Row): self.draw_one(x, y) def draw_bg(self): self.create_rectangle(self.bleft, self.btop, self.bleft + self.bsize*_Row, self.btop + self.bsize*_Row, width=0, fill=_nonecolor) for i in range(0, _Row+1): self.create_line(self.bleft, self.btop + self.bsize*i, self.bleft + self.bsize*_Row + 1, self.btop + self.bsize*i, fill=_linecolor) self.create_line(self.bleft + self.bsize*i, self.btop, self.bleft + self.bsize*i, self.btop + self.bsize*_Row + 1, fill=_linecolor) def draw_one(self, x, y): #print x,y,map[y][x].type left = self.bleft + self.bsize*x + 2 top = self.btop + self.bsize*y + 2 size = self.bsize - 4 if map[y][x].isBright: self.create_rectangle(left-1,top-1,left + size+1,top + size+1, fill=_Bulb_lightcolor, outline=_Bulb_lightcolor) if map[y][x].type==btype.wall: self.create_rectangle(left-1,top-1,left + size+1,top + size+1, fill=_wallcolor) if map[y][x].number>=0: self.create_text(left-1+self.bsize/2,top-1+self.bsize/2, text=str(map[y][x].number), fill=map[y][x].numcolor, font=(_Defaultfont, str(int(self.bsize*0.8)))) elif map[y][x].type==btype.bulb: self.create_oval(left, top, left + size, top + size, fill=map[y][x].bulbcolor) #self.create_rectangle(left-1,top-1,left + size+1,top + size+1, fill="black") def get_block(self, x, y): yy = int((y - self.btop) / self.bsize) xx = int((x - self.bleft) / self.bsize) if valid(xx, yy): return xx, yy else: return -1, -1 def set_cursor(self, cursor): if self.cget("cursor") <> cursor: self.config(cursor=cursor) def motion(self, event): x, y = self.get_block(event.x, event.y) if x != -1 :#and self.state.test(i, j): self.set_cursor("tcross") else: self.set_cursor("no") #print x,y def click(self, event): global _state if _state!=gamestate.start: return x, y = self.get_block(event.x, event.y) if x == -1: return if map[y][x].type==btype.none: map[y][x].type=btype.bulb elif map[y][x].type==btype.bulb: map[y][x].type=btype.none refreshmap() self.label_var.set(str(x)) self.redraw() if chkwin(): self.label_var.set('You win!') _state=gamestate.none def new_game(self): global _state, map map=loadmap('0.txt') _state=gamestate.start #self.state = State() self.label_var.set("") self.redraw() class App(Tkinter.Tk): def __init__(self, parent): Tkinter.Tk.__init__(self, parent) self.parent = parent self.label_var = Tkinter.StringVar() self.initialize() def initialize(self): #self.option_add("*tearOff", Tkinter.FALSE) self.grid() # create menu menu = Tkinter.Menu(self) game = Tkinter.Menu(menu) menu.add_cascade(label="Game", underline=0, menu=game) game.add_command(label="New", underline=0, command=self.new_game) game.add_separator() game.add_command(label="Exit", underline=1, command=self.exit_game) self.config(menu=menu) # create label label = Tkinter.Label(self, textvariable=self.label_var) label.grid(column=0, row=0, sticky="NSWE") # create board self.board = Board(self, self.label_var) self.board.grid(column=0, row=1, sticky="NSWE") self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(1, weight=1) # set geometry self.update() self.geometry(self.geometry()) def new_game(self): self.board.new_game() def exit_game(self): self.quit() if __name__ == "__main__": main()
需要有一个0.txt
....... .2.-.-. ....... .1...4. ....... .-.1.-. .......
发表评论