from tkinter import *
class Settings:
WIDTH = 800
HEIGHT = 600
class Game:
def init(self):
self.sm = StepMaster()
textPage = TextPage(["难忘的小事","long time ago","there's a little girl"])
textPage1 = TextPage(["suddenly a pig rushed out of the forest","then the pig hit a big tree and died","the hunter saw this.","He charried the dead pig to his village and feed his children."])
imgPage = ImgPage("figures/scene1.gif")
imgPage2 = ImgPage("figures/scene2.gif")
imgPage3 = ImgPage("figures/scene3.gif")
imgPage4 = ImgPage("figures/scene4.gif")
step0 = Step(textPage,imgPage,None)
step0.setConnections({"prev":0,"next":1})
self.sm.addStep(step0)
selBox = SelBox("which option will you choose?",["go back","kill it"])
step1 = Step(textPage,imgPage,selBox)
step1.setConnections({"prev":0,"next":-1,"1":0,"2":2})
self.sm.addStep(step1)
step2 = Step(None,imgPage2,None)
step2.setConnections({"prev":1,"next":3})
self.sm.addStep(step2)
step3 = Step(textPage1,imgPage2,None)
step3.setConnections({"prev":2,"next":4})
self.sm.addStep(step3)
selBox1 = SelBox("前方遭遇怪物,用什么职业出战?",["战士","法师","牧师","圣骑士"])
step4 = Step(textPage1,imgPage2,selBox1)
step4.setConnections({"prev":3,"next":-1,"1":5,"2":6,"3":4,"4":4})
self.sm.addStep(step4)
step5 = Step(None,imgPage3,None)
step5.setConnections({"prev":4,"next":5})
self.sm.addStep(step5)
step6 = Step(None,imgPage4,None)
step6.setConnections({"prev":4,"next":6})
self.sm.addStep(step6)
self.sm.start()
tk = Tk()
self.canvas = Canvas(tk,width=Settings.WIDTH,height=Settings.HEIGHT)
self.paint()
self.canvas.pack()
self.canvas.bind_all('<KeyPress-Right>',self.keyPressed)
self.canvas.bind_all('<KeyPress-Left>',self.keyPressed)
self.canvas.bind_all('<KeyPress-Up>',self.keyPressed)
self.canvas.bind_all('<KeyPress-Down>',self.keyPressed)
tk.mainloop()
def paint(self):
self.sm.paintCurrent(self.canvas)
self.canvas.pack()
def keyPressed(self,event):
print(event.keysym)
if event.keysym=="Left":
self.sm.leftKeyPressed()
elif event.keysym=="Right":
self.sm.rightKeyPressed()
elif event.keysym=="Up":
self.sm.upKeyPressed()
elif event.keysym=="Down":
self.sm.downKeyPressed()
self.paint()
class StepMaster:
def __init__(self):
self.current = -1
self.steps = []
def addStep(self,step):
self.steps.append(step)
def start(self):
self.current = 0
def paintCurrent(self,canvas):
currentStep = self.steps[self.current]
currentStep.paint(canvas)
def currentStep(self):
return self.steps[self.current]
def leftKeyPressed(self):
self.current = self.currentStep().prev()
def rightKeyPressed(self):
self.current = self.currentStep().next()
def upKeyPressed(self):
self.currentStep().up()
def downKeyPressed(self):
self.currentStep().down()
class Step:
def __init__(self,textPage,imgPage,selBox):
self.textPage = textPage
self.imgPage = imgPage
self.selBox = selBox
def setConnections(self,connections):
self.connections = connections
def paint(self,canvas):
if self.imgPage:
self.imgPage.paint(canvas)
if self.textPage:
self.textPage.paint(canvas)
if self.selBox:
self.selBox.paint(canvas)
def prev(self):
return self.connections["prev"]
def next(self):
if not self.selBox:
return self.connections["next"]
return self.connections[self.selBox.getChosen()]
def up(self):
if self.selBox:
self.selBox.optionUp()
def down(self):
if self.selBox:
self.selBox.optionDown()
class TextPage:
def __init__(self,contents):
self.contents = contents
def paint(self,canvas):
print("textpage painted")
j = Settings.HEIGHT*0.05
for line in self.contents:
canvas.create_text(0.5*Settings.WIDTH,j,text=line,fill="white")
j+=0.08*Settings.HEIGHT
class ImgPage:
def __init__(self,imgPath):
self.imgPath = imgPath;
def paint(self,canvas):
print("imgpage painted")
self.img = PhotoImage(file=self.imgPath)
canvas.create_image(0,0,anchor=NW,image=self.img)
class SelBox:
sbImg = None
def __init__(self,question,options):
self.question = question
self.options = options
self.chosen = 0
def paint(self,canvas):
print("selbox painted")
if not SelBox.sbImg:
SelBox.sbImg = PhotoImage(file = "figures/selbox.gif")
canvas.create_image(0,400,anchor=NW,image=SelBox.sbImg)
canvas.create_text(0.5*Settings.WIDTH,420,text=self.question)
j = 450+self.chosen*40
canvas.create_rectangle(0.1*Settings.WIDTH,j,0.9*Settings.WIDTH,j+20,fill="yellow")
j = 460
for option in self.options:
canvas.create_text(0.5*Settings.WIDTH,j,text=option)
j+=40
def getChosen(self):
return str(self.chosen+1)
def optionUp(self):
if self.chosen > 0:
self.chosen-=1
def optionDown(self):
if self.chosen < len(self.options) - 1:
self.chosen+=1
def main():
game = Game()
game.init()
main()
这个小程序是打算用于教学中的。学生只需要修改Game类的init方法,将定制的文本(TextPage),图片(ImgPage),还有选择栏(SelBox)组合成一个步(Step)对象,然后将Step交由StepMaster对象管理。Step对象需要设置步间跳转关系connecions,使得步与步之间可以有序跳转。
游戏的结构就是以步为基本单位,设置好跳转关系的步之间,可以根据SelBox的选项进行跳转,如当前步没有SelBox,则根据默认的翻页键跳转。
整个游戏就是一个交互式的电子小说,有图片,有文字,读者可以通过选择控制情节进展。
另:
1.文本默认是居中对齐,颜色为白色
2.图片必须是800*600的gif
3.选择栏选项一般不多于4个
4.音效模块并未实现
游戏界面:丑是丑了点,毕竟这么几行代码。。。(有同学能把界面调美观的直接满分~)
[img]http://dl2.iteye.com/upload/attachment/0100/7618/164a5c78-85b9-3fed-9aa4-eefec1733ce0.png[/img]