■ tkinter 모듈을 사용해 미스터 스틱맨 게임을 만드는 방법을 보여준다.
▶ location.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
class Location: def __init__(self, x1 = 0, y1 = 0, x2 = 0, y2 = 0): self.x1 = x1 self.y1 = y1 self.x2 = x2 self.y2 = y2 def isWithinX(location1, location2): if (location1.x1 > location2.x1 and location1.x1 < location2.x2) or \ (location1.x2 > location2.x1 and location1.x2 < location2.x2) or \ (location2.x1 > location1.x1 and location2.x1 < location1.x2) or \ (location2.x2 > location1.x1 and location2.x2 < location1.x1): return True else: return False def isWithinY(location1, location2): if (location1.y1 > location2.y1 and location1.y1 < location2.y2) or \ (location1.y2 > location2.y1 and location1.y2 < location2.y2) or \ (location2.y1 > location1.y1 and location2.y1 < location1.y2) or \ (location2.y2 > location1.y1 and location2.y2 < location1.y1): return True else: return False def isLeftCollided(location1, location2): if isWithinY(location1, location2): if location1.x1 <= location2.x2 and location1.x1 >= location2.x1: return True return False def isRightCollided(location1, location2): if isWithinY(location1, location2): if location1.x2 >= location2.x1 and location1.x2 <= location2.x2: return True return False def isTopCollided(location1, location2): if isWithinX(location1, location2): if location1.y1 <= location2.y2 and location1.y1 >= location2.y1: return True return False def isBottomCollided(bottom, location1, location2): if isWithinX(location1, location2): baseY = location1.y2 + bottom if baseY >= location2.y1 and baseY <= location2.y2: return True return False |
▶ sprite.py
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Sprite: def __init__(self, game): self.game = game self.isFinished = False self.location = None def move(self): pass def getLocation(self): return self.location |
▶ platformsprite.py
1 2 3 4 5 6 7 8 9 10 11 12 |
from location import * from sprite import * class PlatformSprite(Sprite): def __init__(self, game, photoImage, x, y, width, height): Sprite.__init__(self, game) self.photoImage = photoImage self.image = game.canvas.create_image(x, y, image = self.photoImage, anchor = "nw") self.location = Location(x, y, x + width, y + height) |
▶ doorsprite.py
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from location import * from sprite import * class DoorSprite(Sprite): def __init__(self, game, photoImage, x, y, width, height): Sprite.__init__(self, game) self.photoImage = photoImage self.image = game.canvas.create_image(x, y, image = self.photoImage, anchor = "nw") self.location = Location(x, y, x + (width / 2), y + height) self.isFinished = True |
▶ stickmansprite.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
import time from tkinter import * from location import * from sprite import * class StickManSprite(Sprite): def __init__(self, game): Sprite.__init__(self, game) self.leftImageList = \ [ PhotoImage(file="./images/stick-L1.png"), PhotoImage(file="./images/stick-L2.png"), PhotoImage(file="./images/stick-L3.png") ] self.rightImageList = \ [ PhotoImage(file="./images/stick-R1.png"), PhotoImage(file="./images/stick-R2.png"), PhotoImage(file="./images/stick-R3.png") ] self.image = game.canvas.create_image(200, 470, image = self.leftImageList[0], anchor = "nw") self.x = -2 self.y = 0 self.currentImageIndex = 0 self.currentImageAddIndex = 1 self.jumpCount = 0 self.lastTime = time.time() self.location = Location() game.canvas.bind_all("<KeyPress-Left>" , self.moveLeft ) game.canvas.bind_all("<KeyPress-Right>", self.moveRight) game.canvas.bind_all("<space>" , self.jump ) def moveLeft(self, evt): if self.y == 0: self.x = -2 def moveRight(self, evt): if self.y == 0: self.x = 2 def jump(self, evt): if self.y == 0: self.y = -4 self.jumpCount = 0 def animate(self): if self.x != 0 and self.y == 0: if time.time() - self.lastTime > 0.1: self.lastTime = time.time() self.currentImageIndex += self.currentImageAddIndex if self.currentImageIndex >= 2: self.currentImageAddIndex = -1 if self.currentImageIndex <= 0: self.currentImageAddIndex = 1 if self.x < 0: if self.y != 0: self.game.canvas.itemconfig(self.image, image = self.leftImageList[2]) else: self.game.canvas.itemconfig(self.image, image = self.leftImageList[self.currentImageIndex]) elif self.x > 0: if self.y != 0: self.game.canvas.itemconfig(self.image, image = self.rightImageList[2]) else: self.game.canvas.itemconfig(self.image, image = self.rightImageList[self.currentImageIndex]) def getLocation(self): xyList = list(self.game.canvas.coords(self.image)) self.location.x1 = xyList[0] self.location.y1 = xyList[1] self.location.x2 = xyList[0] + 27 self.location.y2 = xyList[1] + 30 return self.location def move(self): self.animate() if self.y < 0: self.jumpCount += 1 if self.jumpCount > 20: self.y = 4 if self.y > 0: self.jumpCount -= 1 location = self.getLocation() left = True right = True top = True bottom = True falling = True if self.y > 0 and location.y2 >= self.game.canvasHeight: self.y = 0 bottom = False elif self.y < 0 and location.y1 <= 0: self.y = 0 top = False if self.x > 0 and location.x2 >= self.game.canvasWidth: self.x = 0 right = False elif self.x < 0 and location.x1 <= 0: self.x = 0 left = False for sprite in self.game.spriteList: if sprite == self: continue spriteLocation = sprite.getLocation() if top and self.y < 0 and isTopCollided(location, spriteLocation): self.y = -self.y top = False if bottom and self.y > 0 and isBottomCollided(self.y, location, spriteLocation): self.y = spriteLocation.y1 - location.y2 if self.y < 0: self.y = 0 bottom = False top = False if bottom and falling and self.y == 0 and location.y2 < self.game.canvasHeight and isBottomCollided(1, location, spriteLocation): falling = False if left and self.x < 0 and isLeftCollided(location, spriteLocation): self.x = 0 left = False if sprite.isFinished: self.game.isRunning = False if right and self.x > 0 and isRightCollided(location, spriteLocation): self.x = 0 right = False if sprite.isFinished: self.game.isRunning = False if falling and bottom and self.y == 0 and location.y2 < self.game.canvasHeight: self.y = 4 self.game.canvas.move(self.image, self.x, self.y) |
▶ game.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import time from tkinter import * class Game: def __init__(self): self.tk = Tk() self.tk.title("TestProject") self.tk.resizable(0, 0) self.tk.wm_attributes("-topmost", 1) self.canvas = Canvas(self.tk, width = 500, height = 500, highlightthickness = 0) self.canvas.pack() self.tk.update() self.canvasWidth = 500 self.canvasHeight = 500 self.backgroundImage = PhotoImage(file="./images/background.png") backgroundImageWidth = self.backgroundImage.width() backgroundImageHeight = self.backgroundImage.height() for x in range(0, 5): for y in range(0, 5): self.canvas.create_image(x * backgroundImageWidth, y * backgroundImageHeight, image = self.backgroundImage, anchor = "nw") self.spriteList = [] self.isRunning = True def mainloop(self): while 1: if self.isRunning == True: for sprite in self.spriteList: sprite.move() self.tk.update_idletasks() self.tk.update() time.sleep(0.01) |
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import time from tkinter import * from platformsprite import * from doorsprite import * from stickmansprite import * from game import * game = Game() platformSprite1 = PlatformSprite(game, PhotoImage(file = "./images/platform1.png"), 0 , 480, 100, 10) platformSprite2 = PlatformSprite(game, PhotoImage(file = "./images/platform1.png"), 150, 440, 100, 10) platformSprite3 = PlatformSprite(game, PhotoImage(file = "./images/platform1.png"), 300, 400, 100, 10) platformSprite4 = PlatformSprite(game, PhotoImage(file = "./images/platform1.png"), 300, 160, 100, 10) platformSprite5 = PlatformSprite(game, PhotoImage(file = "./images/platform2.png"), 175, 350, 66 , 10) platformSprite6 = PlatformSprite(game, PhotoImage(file = "./images/platform2.png"), 50 , 300, 66 , 10) platformSprite7 = PlatformSprite(game, PhotoImage(file = "./images/platform2.png"), 170, 120, 66 , 10) platformSprite8 = PlatformSprite(game, PhotoImage(file = "./images/platform2.png"), 45 , 60 , 66 , 10) platformSprite9 = PlatformSprite(game, PhotoImage(file = "./images/platform3.png"), 170, 250, 32 , 10) platformSprite10 = PlatformSprite(game, PhotoImage(file = "./images/platform3.png"), 230, 200, 32 , 10) game.spriteList.append(platformSprite1 ) game.spriteList.append(platformSprite2 ) game.spriteList.append(platformSprite3 ) game.spriteList.append(platformSprite4 ) game.spriteList.append(platformSprite5 ) game.spriteList.append(platformSprite6 ) game.spriteList.append(platformSprite7 ) game.spriteList.append(platformSprite8 ) game.spriteList.append(platformSprite9 ) game.spriteList.append(platformSprite10) doorSprite = DoorSprite(game, PhotoImage(file = "./images/door1.png"), 45, 30, 40, 35) game.spriteList.append(doorSprite) stickManSprite = StickManSprite(game) game.spriteList.append(stickManSprite) game.mainloop() |