火柴人
关键字: Python, 小游戏, GIMP, python for kids
Classes
- class Game(object):
- class Coords(object):
- class sprites(object):
- class PlatFromSprites(sprites):
- class StickSprites(sprites):
- class DoorSprite(sprites):
Functions
- Global
- def within_x(coo1, coo2):
- def within_y(coo1, coo2):
- def crash_left(coo1, coo2):
- def crash_right(coo1, coo2):
- def crash_top(coo1, coo2):
- def crash_bottom(delty, coo1, coo2):
- Non-Global
- class Game(object):
- def init(self):
- def mainloop(self):
- class Coords(object):
- def init(self, x1 = 0, y1 = 0, x2 = 0, y2 = 0):
- class sprites(object):
- def init(self, game):
- def move(self):
- def coords(self):
- class PlatFromSprites(sprites):
- def init(self, game, photo_image, x, y, width, height):
- class StickSprites(sprites):
- def init(self, game):
- def turn_left(self, evt):
- def turn_right(self, evt):
- def jump(self, evt):
- def animate(self):
- def coords(self):
- def move(self):
- class DoorSprite(sprites):
- def init(self, game, photo_image, x, y, width, height):
Main
if __name__ == '__main__':
g = Game()
platform1 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair1.gif"), 0, 480, 100, 10)
platform2 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair1.gif"), 150, 440, 100, 10)
platform3 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair1.gif"), 300, 400, 100, 10)
platform4 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair1.gif"), 300, 160, 100, 10)
g.sprites.append(platform1)
g.sprites.append(platform2)
g.sprites.append(platform3)
g.sprites.append(platform4)
platform5 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair2.gif"), 175, 350, 66, 10)
platform6 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair2.gif"), 50, 300, 66, 10)
platform7 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair2.gif"), 170, 120, 66, 10)
platform8 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair2.gif"), 45, 60, 66, 10)
g.sprites.append(platform5)
g.sprites.append(platform6)
g.sprites.append(platform7)
g.sprites.append(platform8)
platform9 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair3.gif"), 170, 300, 32, 10)
platform10 = PlatFromSprites(g, PhotoImage(file="E:\\game\\pic\\stair3.gif"), 230, 200, 32, 10)
door = DoorSprite(g, PhotoImage(file = "E:\\game\\pic\\door2.gif"), 45, 30, 40, 35)
g.sprites.append(door)
st = StickSprites(g)
g.sprites.append(st)
g.mainloop()
main()
Results
- Screenshot of this little game
when it begins
when it was running
Appendix
- How to get these gifs within my program ?
- Firstly, draw these gifs with the drawing tool of Windows OS and be sure get the right size. In this program the matchman should be 27 * 30 pixel.
- Secondly, make the background of these gifs transparent with GIMP. The method of “transparent” are as follows:
- 1 Open your gif with GIMP
- 2 Select all (Ctrl A)
- 3 Reverse (Ctrl I)
- 4 Copy
- 5 New File, note that choosing background as transparent while you build the new file.
- 6 Paste
- 7 Save
- 8 Export as XXX.gif
- You may down GIMP freely here Down GIMP 2
- Does this program has any bugs?
- Unfortunately, it is. The Matchman can’t go to the 6 board in this game.
- Source code of this program