├── .gitattributes
├── .idea
├── .gitignore
├── inspectionProfiles
│ └── profiles_settings.xml
├── misc.xml
├── modules.xml
└── py_samples.iml
├── Appendix
├── _DS_Store
├── block_game.py
├── maze_game.py
├── pen.png
├── study_words.py
└── wall.png
├── Chapter10
├── pg_bg.png
├── pg_chara0.png
├── pg_chara1.png
├── pg_slime.png
├── pygame_bgm.ogg
├── pygame_draw.py
├── pygame_image.py
├── pygame_image2.py
├── pygame_key.py
├── pygame_korean.py
├── pygame_mouse.py
├── pygame_music.py
├── pygame_se.ogg
└── pygame_system.py
├── Chapter11
├── battle_message.py
├── battle_start.py
├── battle_turn.py
├── btlbg.png
├── column11.py
├── dungeon_maker.py
├── effect_a.png
├── enemy1.png
├── enemy2.png
├── enemy3.png
├── enemy4.png
├── floor.png
├── maze_maker.py
├── player.png
├── walk_in_dungeon.py
└── wall.png
├── Chapter12
├── column12_file_read.py
├── column12_file_write.py
├── column12_int.py
├── image
│ ├── apple.png
│ ├── blaze_gem.png
│ ├── btlbg.png
│ ├── cocoon.png
│ ├── dark.png
│ ├── effect_a.png
│ ├── effect_b.png
│ ├── enemy0.png
│ ├── enemy1.png
│ ├── enemy2.png
│ ├── enemy3.png
│ ├── enemy4.png
│ ├── enemy5.png
│ ├── enemy6.png
│ ├── enemy7.png
│ ├── enemy8.png
│ ├── enemy9.png
│ ├── floor.png
│ ├── meat.png
│ ├── mychr0.png
│ ├── mychr1.png
│ ├── mychr2.png
│ ├── mychr3.png
│ ├── mychr4.png
│ ├── mychr5.png
│ ├── mychr6.png
│ ├── mychr7.png
│ ├── mychr8.png
│ ├── parameter.png
│ ├── potion.png
│ ├── spoiled.png
│ ├── stairs.png
│ ├── tbox.png
│ ├── title.png
│ ├── wall.png
│ └── wall2.png
├── one_hour_dungeon.py
├── sound
│ ├── ohd_bgm_battle.ogg
│ ├── ohd_bgm_field.ogg
│ ├── ohd_bgm_title.ogg
│ ├── ohd_jin_gameover.ogg
│ ├── ohd_jin_levup.ogg
│ ├── ohd_jin_win.ogg
│ ├── ohd_se_attack.ogg
│ ├── ohd_se_blaze.ogg
│ └── ohd_se_potion.ogg
├── test.txt
└── test2.txt
├── Chapter13
├── .DS_Store
├── list1302_1.py
├── list1302_2.py
├── list1302_3.py
├── list1302_4.py
├── list1303_1.py
├── list1303_2.py
├── list1303_3.py
├── list1304_1.py
├── ninja.png
└── swordsman.png
├── Chapter3
├── list0301_1.py
├── list0301_2.py
├── list0302_1.py
├── list0303_1.py
├── list0303_2.py
├── list0304_1.py
├── list0304_2.py
├── list0304_3.py
├── list0304_4.py
├── list0305_1.py
├── list0305_2.py
└── list0305_3.py
├── Chapter4
├── list0402_1.py
├── list0402_2.py
├── list0403_1.py
├── list0403_2.py
├── list0403_3.py
├── list0403_4.py
├── list0404_1.py
├── list0404_2.py
├── list0404_3.py
└── list0404_4.py
├── Chapter5
├── list0502_1.py
├── list0502_2.py
├── list0502_3.py
├── list0502_4.py
├── list0503_1.py
├── list0503_2.py
├── list0503_3.py
├── list0503_4.py
├── list0503_5.py
├── list0504_1.py
├── list0504_2.py
├── list0504_3.py
├── list0504_4.py
└── list0504_5.py
├── Chapter6
├── column06.py
├── hyunju.png
├── list0601_1.py
├── list0601_2.py
├── list0602_1.py
├── list0602_2.py
├── list0603_1.py
├── list0603_2.py
├── list0604_1.py
├── list0604_2.py
├── list0605_1.py
├── list0605_2.py
├── list0605_3.py
└── miko.png
├── Chapter7
├── list0701_1.py
├── list0701_2.py
├── list0702_1.py
├── list0703_1.py
├── list0703_2.py
├── list0703_3.py
├── list0704_1.py
├── list0705_1.py
├── list0705_2.py
├── list0705_3.py
├── list0705_4.py
└── mina.png
├── Chapter8
├── cat00.png
├── cat01.png
├── cat02.png
├── cat03.png
├── column08.py
├── list0801_1.py
├── list0802_1.py
├── list0803_1.py
├── list0803_2.py
├── list0803_3.py
├── list0804_1.py
├── list0805_1.py
├── list0806_1.py
├── list0806_2.py
├── list0806_3.py
├── mimi.png
└── mimi_s.png
├── Chapter9
├── column09.py
├── list0902_1.py
├── list0903_1.py
├── list0904_1.py
├── list0905_1.py
├── list0906_1.py
├── list0907_1.py
├── list0908_1.py
├── list0909_1.py
├── neko1.png
├── neko2.png
├── neko3.png
├── neko4.png
├── neko5.png
├── neko6.png
├── neko_bg.png
├── neko_cursor.png
├── neko_niku.png
└── neko_pzl.py
└── README.md
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/py_samples.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Appendix/_DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Appendix/_DS_Store
--------------------------------------------------------------------------------
/Appendix/block_game.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import random
3 |
4 | FNT = ("Times New Roman", 20, "bold")
5 |
6 | key = ""
7 | keyoff = False
8 | idx = 0
9 | tmr = 0
10 | stage = 0
11 | score = 0
12 | bar_x = 0
13 | bar_y = 540
14 | ball_x = 0
15 | ball_y = 0
16 | ball_xp = 0
17 | ball_yp = 0
18 | is_clr = True
19 |
20 | block = []
21 | for i in range(5):
22 | block.append([1] * 10)
23 | for i in range(10):
24 | block.append([0] * 10)
25 |
26 | def key_down(e):
27 | global key
28 | key = e.keysym
29 |
30 | def key_up(e):
31 | global keyoff
32 | keyoff = True
33 |
34 | def draw_block():
35 | global is_clr
36 | is_clr = True
37 | cvs.delete("BG")
38 | for y in range(15):
39 | for x in range(10):
40 | gx = x * 80
41 | gy = y * 40
42 | if block[y][x] == 1:
43 | cvs.create_rectangle(gx + 1, gy + 4, gx + 79, gy + 32, fill=block_color(x, y), width=0, tag="BG")
44 | is_clr = False
45 | cvs.create_text(200, 20, text="STAGE " + str(stage), fill="white", font=FNT, tag="BG")
46 | cvs.create_text(600, 20, text="SCORE " + str(score), fill="white", font=FNT, tag="BG")
47 |
48 | def block_color(x, y): # format() 명령으로 16진수 값 변환 가능
49 | col = "#{0:x}{1:x}{2:x}".format(15 - x - int(y / 3), x + 1, y * 3 + 3)
50 | return col
51 |
52 | def draw_bar():
53 | cvs.delete("BAR")
54 | cvs.create_rectangle(bar_x - 80, bar_y - 12, bar_x + 80, bar_y + 12, fill="silver", width=0, tag="BAR")
55 | cvs.create_rectangle(bar_x - 78, bar_y - 14, bar_x + 78, bar_y + 14, fill="silver", width=0, tag="BAR")
56 | cvs.create_rectangle(bar_x - 78, bar_y - 12, bar_x + 78, bar_y + 12, fill="white", width=0, tag="BAR")
57 |
58 | def move_bar():
59 | global bar_x
60 | if key == "Left" and bar_x > 80:
61 | bar_x = bar_x - 40
62 | if key == "Right" and bar_x < 720:
63 | bar_x = bar_x + 40
64 |
65 | def draw_ball():
66 | cvs.delete("BALL")
67 | cvs.create_oval(ball_x - 20, ball_y - 20, ball_x + 20, ball_y + 20, fill="gold", outline="orange", width=2, tag="BALL")
68 | cvs.create_oval(ball_x - 16, ball_y - 16, ball_x + 12, ball_y + 12, fill="yellow", width=0, tag="BALL")
69 |
70 | def move_ball():
71 | global idx, tmr, score, ball_x, ball_y, ball_xp, ball_yp
72 | ball_x = ball_x + ball_xp
73 | if ball_x < 20:
74 | ball_x = 20
75 | ball_xp = -ball_xp
76 | if ball_x > 780:
77 | ball_x = 780
78 | ball_xp = -ball_xp
79 | x = int(ball_x / 80)
80 | y = int(ball_y / 40)
81 | if block[y][x] == 1:
82 | block[y][x] = 0
83 | ball_xp = -ball_xp
84 | score = score + 10
85 |
86 | ball_y = ball_y + ball_yp
87 | if ball_y >= 600:
88 | idx = 2
89 | tmr = 0
90 | return
91 | if ball_y < 20:
92 | ball_y = 20
93 | ball_yp = -ball_yp
94 | x = int(ball_x / 80)
95 | y = int(ball_y / 40)
96 | if block[y][x] == 1:
97 | block[y][x] = 0
98 | ball_yp = -ball_yp
99 | score = score + 10
100 |
101 | if bar_y - 40 <= ball_y and ball_y <= bar_y:
102 | if bar_x - 80 <= ball_x and ball_x <= bar_x + 80:
103 | ball_yp = -10
104 | score = score + 1
105 | elif bar_x - 100 <= ball_x and ball_x <= bar_x - 80:
106 | ball_yp = -10
107 | ball_xp = random.randint(-20, -10)
108 | score = score + 2
109 | elif bar_x + 80 <= ball_x and ball_x <= bar_x + 100:
110 | ball_yp = -10
111 | ball_xp = random.randint(10, 20)
112 | score = score + 2
113 |
114 | def main_proc():
115 | global key, keyoff
116 | global idx, tmr, stage, score
117 | global bar_x, ball_x, ball_y, ball_xp, ball_yp
118 | if idx == 0:
119 | tmr = tmr + 1
120 | if tmr == 1:
121 | stage = 1
122 | score = 0
123 | if tmr == 2:
124 | ball_x = 160
125 | ball_y = 240
126 | ball_xp = 10
127 | ball_yp = 10
128 | bar_x = 400
129 | draw_block()
130 | draw_ball()
131 | draw_bar()
132 | cvs.create_text(400, 300, text="START", fill="cyan", font=FNT, tag="TXT")
133 | if tmr == 30:
134 | cvs.delete("TXT")
135 | idx = 1
136 | elif idx == 1:
137 | move_ball()
138 | move_bar()
139 | draw_block()
140 | draw_ball()
141 | draw_bar()
142 | if is_clr == True:
143 | idx = 3
144 | tmr = 0
145 | elif idx == 2:
146 | tmr = tmr + 1
147 | if tmr == 1:
148 | cvs.create_text(400, 260, text="GAME OVER", fill="red", font=FNT, tag="TXT")
149 | if tmr == 15:
150 | cvs.create_text(300, 340, text="[R]eplay", fill="cyan", font=FNT, tag="TXT")
151 | cvs.create_text(500, 340, text="[N]ew game", fill="yellow", font=FNT, tag="TXT")
152 | if key == "r":
153 | cvs.delete("TXT")
154 | idx = 0
155 | tmr = 1
156 | if key == "n":
157 | cvs.delete("TXT")
158 | for y in range(5):
159 | for x in range(10):
160 | block[y][x] = 1
161 | idx = 0
162 | tmr = 0
163 | elif idx == 3:
164 | tmr = tmr + 1
165 | if tmr == 1:
166 | cvs.create_text(400, 260, text="STAGE CLEAR", fill="lime", font=FNT, tag="TXT")
167 | if tmr == 15:
168 | cvs.create_text(400, 340, text="NEXT [SPACE]", fill="cyan", font=FNT, tag="TXT")
169 | if key == "space":
170 | cvs.delete("TXT")
171 | for y in range(5):
172 | for x in range(10):
173 | block[y][x] = 1
174 | idx = 0
175 | tmr = 1
176 | stage = stage + 1
177 |
178 | if keyoff == True:
179 | keyoff = False
180 | if key != "":
181 | key = ""
182 |
183 | root.after(50, main_proc)
184 |
185 | root = tkinter.Tk()
186 | root.title("블록 격파 게임")
187 | root.resizable(False, False)
188 | root.bind("", key_down)
189 | root.bind("", key_up)
190 | cvs = tkinter.Canvas(root, width=800, height=600, bg="black")
191 | cvs.pack()
192 | main_proc()
193 | root.mainloop()
194 |
--------------------------------------------------------------------------------
/Appendix/maze_game.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import tkinter.messagebox
3 |
4 | idx = 0
5 | tmr = 0
6 | stage = 1
7 | ix = 0
8 | iy = 0
9 | key = 0
10 |
11 | def key_down(e):
12 | global key
13 | key = e.keysym
14 |
15 | def key_up(e):
16 | global key
17 | key = 0
18 |
19 | maze = [[], [], [], [], [], [], [], []]
20 |
21 | def stage_data():
22 | global ix, iy
23 | global maze # 리스트 전체를 변경하는 경우 전역 변수 선언 필요
24 | if stage == 1:
25 | ix = 1
26 | iy = 1
27 | # 0: 길, 1: 칠해진 통로, 9: 벽
28 | maze = [
29 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
30 | [9, 0, 9, 0, 0, 0, 9, 0, 0, 9],
31 | [9, 0, 9, 0, 9, 0, 9, 0, 0, 9],
32 | [9, 0, 9, 0, 9, 0, 9, 0, 9, 9],
33 | [9, 0, 9, 0, 9, 0, 9, 0, 0, 9],
34 | [9, 0, 9, 0, 9, 0, 9, 9, 0, 9],
35 | [9, 0, 0, 0, 9, 0, 0, 0, 0, 9],
36 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
37 | ]
38 | if stage == 2:
39 | ix = 8
40 | iy = 6
41 | maze = [
42 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
43 | [9, 0, 0, 0, 0, 0, 0, 0, 0, 9],
44 | [9, 0, 0, 0, 0, 0, 0, 9, 0, 9],
45 | [9, 0, 0, 9, 9, 0, 0, 9, 0, 9],
46 | [9, 0, 0, 9, 9, 0, 0, 9, 0, 9],
47 | [9, 9, 9, 9, 9, 0, 0, 9, 0, 9],
48 | [9, 9, 9, 9, 9, 0, 0, 0, 0, 9],
49 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
50 | ]
51 | if stage == 3:
52 | ix = 3
53 | iy = 3
54 | maze = [
55 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
56 | [9, 9, 9, 0, 0, 0, 0, 9, 9, 9],
57 | [9, 9, 0, 0, 0, 0, 0, 0, 9, 9],
58 | [9, 0, 0, 0, 0, 0, 0, 0, 0, 9],
59 | [9, 0, 9, 0, 0, 0, 0, 0, 0, 9],
60 | [9, 0, 0, 0, 0, 0, 0, 0, 9, 9],
61 | [9, 9, 0, 0, 0, 0, 0, 9, 9, 9],
62 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
63 | ]
64 | if stage == 4:
65 | ix = 4
66 | iy = 3
67 | maze = [
68 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
69 | [9, 0, 0, 0, 0, 0, 0, 0, 0, 9],
70 | [9, 0, 0, 0, 9, 0, 0, 0, 0, 9],
71 | [9, 0, 0, 0, 0, 0, 0, 0, 0, 9],
72 | [9, 0, 0, 9, 0, 0, 0, 9, 0, 9],
73 | [9, 0, 0, 0, 0, 0, 0, 9, 0, 0],
74 | [9, 0, 0, 0, 0, 0, 0, 0, 0, 9],
75 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
76 | ]
77 | if stage == 5:
78 | ix = 1
79 | iy = 6
80 | maze = [
81 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
82 | [9, 0, 0, 0, 0, 0, 0, 0, 0, 9],
83 | [9, 0, 9, 0, 0, 0, 0, 0, 0, 9],
84 | [9, 0, 0, 0, 0, 0, 9, 9, 0, 9],
85 | [9, 0, 0, 0, 0, 9, 9, 9, 0, 9],
86 | [9, 0, 0, 9, 0, 0, 0, 0, 0, 9],
87 | [9, 0, 0, 0, 0, 0, 0, 0, 0, 9],
88 | [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
89 | ]
90 | maze[iy][ix] = 1
91 |
92 | def draw_bg():
93 | for y in range(8):
94 | for x in range(10):
95 | gx = 80 * x
96 | gy = 80 * y
97 | if maze[y][x] == 0:
98 | cvs.create_rectangle(gx, gy, gx + 80, gy + 80, fill="white", width=0, tag="BG")
99 | if maze[y][x] == 9:
100 | cvs.create_image(gx + 40, gy + 40, image=wall, tag="BG")
101 | cvs.create_text(120, 40, text="STAGE " + str(stage), fill="white", font=("Times New Roman", 30, "bold"), tag="BG")
102 | gx = 80 * ix
103 | gy = 80 * iy
104 | cvs.create_rectangle(gx, gy, gx + 80, gy + 80, fill="pink", width=0, tag="BG")
105 | cvs.create_image(gx + 60, gy + 20, image=pen, tag="PEN")
106 |
107 | def erase_bg():
108 | cvs.delete("BG")
109 | cvs.delete("PEN")
110 |
111 | def move_pen():
112 | global idx, tmr, ix, iy, key
113 | bx = ix
114 | by = iy
115 | if key == "Left" and maze[iy][ix - 1] == 0:
116 | ix = ix - 1
117 | if key == "Right" and maze[iy][ix + 1] == 0:
118 | ix = ix + 1
119 | if key == "Up" and maze[iy - 1][ix] == 0:
120 | iy = iy - 1
121 | if key == "Down" and maze[iy + 1][ix] == 0:
122 | iy = iy + 1
123 | if ix != bx or iy != by:
124 | maze[iy][ix] = 2
125 | gx = 80 * ix
126 | gy = 80 * iy
127 | cvs.create_rectangle(gx, gy, gx + 80, gy + 80, fill="pink", width=0, tag="BG")
128 | cvs.delete("PEN")
129 | cvs.create_image(gx + 60, gy + 20, image=pen, tag="PEN")
130 | if key == "g" or key == "G" or key == "Shift_L":
131 | key = 0
132 | ret = tkinter.messagebox.askyesno("포기", "다시 하겠습니까?")
133 | # root.focus_force() # for Mac
134 | if ret == True:
135 | stage_data()
136 | erase_bg()
137 | draw_bg()
138 |
139 | def count_tile():
140 | cnt = 0
141 | for y in range(8):
142 | for x in range(10):
143 | if maze[y][x] == 0:
144 | cnt = cnt + 1
145 | return cnt
146 |
147 | def game_main():
148 | global idx, tmr, stage
149 | if idx == 0: # 초기화
150 | stage_data()
151 | draw_bg()
152 | idx = 1
153 | if idx == 1: # 펜 이동과 클리어 판정
154 | move_pen()
155 | if count_tile() == 0:
156 | txt = "STAGE CLEAR"
157 | if stage == 5:
158 | txt = "ALL STAGE CLEAR!"
159 | cvs.create_text(400, 320, text=txt, fill="white", font=("Times New Roman", 40, "bold"), tag="BG")
160 | idx = 2
161 | tmr = 0
162 | if idx == 2: # 스테이지 클리어
163 | tmr = tmr + 1
164 | if tmr == 30:
165 | if stage < 5:
166 | stage = stage + 1
167 | stage_data()
168 | erase_bg()
169 | draw_bg()
170 | idx = 1
171 | root.after(200, game_main)
172 |
173 | root = tkinter.Tk()
174 | root.title("한 번에 미로 칠하기 게임")
175 | root.resizable(False, False)
176 | root.bind("", key_down)
177 | root.bind("", key_up)
178 | cvs = tkinter.Canvas(root, width=800, height=640)
179 | cvs.pack()
180 | pen = tkinter.PhotoImage(file="pen.png")
181 | wall = tkinter.PhotoImage(file="wall.png")
182 | game_main()
183 | root.mainloop()
184 |
--------------------------------------------------------------------------------
/Appendix/pen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Appendix/pen.png
--------------------------------------------------------------------------------
/Appendix/study_words.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | FNT1 = ("Times New Roman", 12)
4 | FNT2 = ("Times New Roman", 24)
5 |
6 | WORDS = [
7 | "apple", "사과",
8 | "book", "책",
9 | "cat", "고양이",
10 | "dog", "개",
11 | "egg", "계란",
12 | "fire", "불",
13 | "gold", "금색",
14 | "head", "머리",
15 | "ice", "얼음",
16 | "juice", "주스",
17 | "king", "왕",
18 | "lemon", "레몬",
19 | "mother", "엄마",
20 | "notebook", "공책",
21 | "orange", "오렌지",
22 | "pen", "펜",
23 | "queen", "여왕",
24 | "room", "방",
25 | "sport", "운동",
26 | "time", "시간",
27 | "user", "사용자",
28 | "vet", "수의사",
29 | "window", "창",
30 | "xanadu", "무릉도원",
31 | "yellow", "노랑",
32 | "zoo", "동물원"
33 | ]
34 | MAX = int(len(WORDS) / 2)
35 | score = 0
36 | word_num = 0
37 | yourword = ""
38 | koff = False # 1 문자씩 입력하기 위한 플래그
39 |
40 | def key_down(e):
41 | global score, word_num, yourword, koff
42 | if koff == True:
43 | koff = False
44 | kcode = e.keycode
45 | ksym = e.keysym
46 | if 65 <= kcode and kcode <= 90: # 영문자 대문자
47 | yourword = yourword + chr(kcode + 32)
48 | if 97 <= kcode and kcode <= 122: # 영문자 소문자
49 | yourword = yourword[:-1] # 맨 끝 1 문자 삭제
50 | if ksym == "Delete" or ksym == "BackSpace":
51 | yourword = yourword[:-1] # 맨 끝 한 문자 삭제
52 | input_label["text"] = yourword
53 | if ksym == "Return":
54 | if input_label["text"] == english_label["text"]:
55 | score = score + 1
56 | set_label()
57 |
58 | def key_up(e):
59 | global koff
60 | koff = True
61 |
62 | def set_label():
63 | global word_num, yourword
64 | score_label["text"] = score
65 | english_label["text"] = WORDS[word_num * 2]
66 | korean_label["text"] = WORDS[word_num * 2 + 1]
67 | input_label["text"] = ""
68 | word_num = (word_num + 1) % MAX
69 | yourword = ""
70 |
71 | root = tkinter.Tk()
72 | root.title("영어 학습 애플리케이션")
73 | root.geometry("400x200")
74 | root.resizable(False, False)
75 | root.bind("", key_down)
76 | root.bind("", key_up)
77 | root["bg"] = "#DEF"
78 |
79 | score_label = tkinter.Label(font=FNT1, bg="#DEF", fg="#4C0")
80 | score_label.pack()
81 | english_label = tkinter.Label(font=FNT2, bg="#DEF")
82 | english_label.pack()
83 | korean_label = tkinter.Label(font=FNT1, bg="#DEF", fg="#444")
84 | korean_label.pack()
85 | input_label = tkinter.Label(font=FNT2, bg="#DEF")
86 | input_label.pack()
87 | howto_label = tkinter.Label(text="영어 단어를 입력하고 [Enter] 키를 누릅니다.\n입력을 수정할 때는 [Delete] 혹은 [BackSpace]", font=FNT1, bg="#FFF", fg="#ABC")
88 | howto_label.pack()
89 |
90 | set_label()
91 | root.mainloop()
92 |
--------------------------------------------------------------------------------
/Appendix/wall.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Appendix/wall.png
--------------------------------------------------------------------------------
/Chapter10/pg_bg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter10/pg_bg.png
--------------------------------------------------------------------------------
/Chapter10/pg_chara0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter10/pg_chara0.png
--------------------------------------------------------------------------------
/Chapter10/pg_chara1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter10/pg_chara1.png
--------------------------------------------------------------------------------
/Chapter10/pg_slime.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter10/pg_slime.png
--------------------------------------------------------------------------------
/Chapter10/pygame_bgm.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter10/pygame_bgm.ogg
--------------------------------------------------------------------------------
/Chapter10/pygame_draw.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 | import math
4 |
5 | WHITE = (255, 255, 255)
6 | BLACK = (0, 0, 0)
7 | RED = (255, 0, 0)
8 | GREEN = (0, 255, 0)
9 | BLUE = (0, 0, 255)
10 | GOLD = (255, 216, 0)
11 | SILVER = (192, 192, 192)
12 | COPPER = (192, 112, 48)
13 |
14 | def main():
15 | pygame.init()
16 | pygame.display.set_caption("첫 번째 Pygame: 도형")
17 | screen = pygame.display.set_mode((800, 600))
18 | clock = pygame.time.Clock()
19 | tmr = 0
20 |
21 | while True:
22 | tmr = tmr + 1
23 | for event in pygame.event.get():
24 | if event.type == pygame.QUIT:
25 | pygame.quit()
26 | sys.exit()
27 |
28 | screen.fill(BLACK)
29 |
30 | pygame.draw.line(screen, RED, [0, 0], [100, 200], 10)
31 | pygame.draw.lines(screen, BLUE, False, [[50, 300], [150, 400], [50, 500]])
32 |
33 | pygame.draw.rect(screen, RED, [200, 50, 120, 80])
34 | pygame.draw.rect(screen, GREEN, [200, 200, 60, 180], 5)
35 | pygame.draw.polygon(screen, BLUE, [[250, 400], [200, 500], [300, 500]], 10)
36 |
37 | pygame.draw.circle(screen, GOLD, [400, 100], 60)
38 | pygame.draw.ellipse(screen, SILVER, [400 - 80, 300 - 40, 160, 80])
39 | pygame.draw.ellipse(screen, COPPER, [400 - 40, 500 - 80, 80, 160], 20)
40 |
41 | ang = math.pi * tmr / 36
42 | pygame.draw.arc(screen, BLUE, [600 - 100, 300 - 200, 200, 400], 0, math.pi * 2)
43 | pygame.draw.arc(screen, WHITE, [600 - 100, 300 - 200, 200, 400], ang, ang + math.pi / 2, 8)
44 |
45 | pygame.display.update()
46 | clock.tick(10)
47 |
48 | if __name__ == '__main__':
49 | main()
50 |
--------------------------------------------------------------------------------
/Chapter10/pygame_image.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 |
4 | def main():
5 | pygame.init()
6 | pygame.display.set_caption("첫 번째 Pygame: 이미지 표시")
7 | screen = pygame.display.set_mode((640, 360))
8 | clock = pygame.time.Clock()
9 | img_bg = pygame.image.load("pg_bg.png")
10 | img_chara = [
11 | pygame.image.load("pg_chara0.png"),
12 | pygame.image.load("pg_chara1.png")
13 | ]
14 | tmr = 0
15 |
16 | while True:
17 | tmr = tmr + 1
18 | for event in pygame.event.get():
19 | if event.type == pygame.QUIT:
20 | pygame.quit()
21 | sys.exit()
22 | if event.type == pygame.KEYDOWN:
23 | if event.key == pygame.K_F1:
24 | screen = pygame.display.set_mode((640, 360), pygame.FULLSCREEN)
25 | if event.key == pygame.K_F2 or event.key == pygame.K_ESCAPE:
26 | screen = pygame.display.set_mode((640, 360))
27 |
28 | x = tmr%160
29 | for i in range(5):
30 | screen.blit(img_bg, [i*160-x, 0])
31 | screen.blit(img_chara[tmr%2], [224, 160])
32 | pygame.display.update()
33 | clock.tick(5)
34 |
35 | if __name__ == '__main__':
36 | main()
37 |
--------------------------------------------------------------------------------
/Chapter10/pygame_image2.py:
--------------------------------------------------------------------------------
1 | import pygame
2 |
3 | WHITE = (255, 255, 255)
4 | NAVY = (0, 0, 128)
5 |
6 |
7 | def main():
8 | pygame.init()
9 | pygame.display.set_caption("첫번째 Pygame: 화면 회전, 확대/축소")
10 | screen = pygame.display.set_mode((600, 400))
11 | clock = pygame.time.Clock()
12 | font = pygame.font.Font(None, 40)
13 | img = pygame.image.load("pg_slime.png")
14 | tmr = 0
15 |
16 | while True:
17 | tmr = tmr + 1
18 | for event in pygame.event.get():
19 | if event.type == pygame.QUIT:
20 | pygame.quit()
21 |
22 | ang = (tmr % 36) * 10
23 | per = (tmr % 100) / 20
24 | img_s = pygame.transform.scale(img, [70, 92])
25 | img_r = pygame.transform.rotate(img, ang)
26 | img_rz = pygame.transform.rotozoom(img, ang, per)
27 |
28 | screen.fill(NAVY)
29 | screen.blit(img, [100, 100])
30 | screen.blit(img_s, [200, 100])
31 | screen.blit(img_r, [300, 100])
32 | screen.blit(img_rz, [400, 100])
33 |
34 | txt = font.render(str(tmr), True, WHITE)
35 | screen.blit(txt, [50, 50])
36 |
37 | pygame.display.update()
38 | clock.tick(10)
39 |
40 |
41 | if __name__ == '__main__':
42 | main()
43 |
--------------------------------------------------------------------------------
/Chapter10/pygame_key.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 |
4 | WHITE = (255, 255, 255)
5 | BLACK = (0, 0, 0)
6 | RED = (255, 0, 0)
7 | GREEN = (0, 255, 0)
8 | BLUE = (0, 0, 255)
9 |
10 | def main():
11 | pygame.init()
12 | pygame.display.set_caption("첫 번째 Pygame: 키 입력 ")
13 | screen = pygame.display.set_mode((800, 600))
14 | clock = pygame.time.Clock()
15 | font = pygame.font.Font(None, 60)
16 |
17 | while True:
18 | for event in pygame.event.get():
19 | if event.type == pygame.QUIT:
20 | pygame.quit()
21 | sys.exit()
22 |
23 | key = pygame.key.get_pressed()
24 | txt1 = font.render("UP" + str(key[pygame.K_UP]) + " DOWN" + str(key[pygame.K_DOWN]), True, WHITE, GREEN)
25 | txt2 = font.render("LEFT" + str(key[pygame.K_LEFT]) + " RIGHT" + str(key[pygame.K_RIGHT]), True, WHITE, BLUE)
26 | txt3 = font.render("SPACE" + str(key[pygame.K_SPACE]) + " ENTER" + str(key[pygame.K_RETURN]), True, WHITE, RED)
27 |
28 | screen.fill(BLACK)
29 | screen.blit(txt1, [100, 100])
30 | screen.blit(txt2, [100, 200])
31 | screen.blit(txt3, [100, 300])
32 | pygame.display.update()
33 | clock.tick(10)
34 |
35 | if __name__ == '__main__':
36 | main()
37 |
--------------------------------------------------------------------------------
/Chapter10/pygame_korean.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 |
4 | WHITE = (255, 255, 255)
5 | BLACK = (0, 0, 0)
6 |
7 | def main():
8 | pygame.init()
9 | pygame.display.set_caption("Pygame에서 한국어 표시하기")
10 | screen = pygame.display.set_mode((800, 600))
11 | clock = pygame.time.Clock()
12 | font = pygame.font.SysFont("malgungothic", 80) # 시스템 폰트 사용 시
13 | # font = pygame.font.Font("font.ttf", 80) # 별도 폰트 사용 시
14 | tmr = 0
15 |
16 | while True:
17 | tmr = tmr + 1
18 | for event in pygame.event.get():
19 | if event.type == pygame.QUIT:
20 | pygame.quit()
21 | sys.exit()
22 |
23 | txt = font.render("한국어 표시 " + str(tmr), True, WHITE)
24 | screen.fill(BLACK)
25 | screen.blit(txt, [100, 200])
26 | pygame.display.update()
27 | clock.tick(10)
28 |
29 | if __name__ == '__main__':
30 | main()
31 |
--------------------------------------------------------------------------------
/Chapter10/pygame_mouse.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 |
4 | BLACK = (0, 0, 0)
5 | LBLUE = (0, 192, 255)
6 | PINK = (255, 0, 224)
7 |
8 | def main():
9 | pygame.init()
10 | pygame.display.set_caption("첫 번째 Pygame: 마우스 입력")
11 | screen = pygame.display.set_mode((800, 600))
12 | clock = pygame.time.Clock()
13 | font = pygame.font.Font(None, 60)
14 |
15 | while True:
16 | for event in pygame.event.get():
17 | if event.type == pygame.QUIT:
18 | pygame.quit()
19 | sys.exit()
20 |
21 | mouseX, mouseY = pygame.mouse.get_pos()
22 | txt1 = font.render("{},{}".format(mouseX, mouseY), True, LBLUE)
23 |
24 | mBtn1, mBtn2, mBtn3 = pygame.mouse.get_pressed()
25 | txt2 = font.render("{}:{}:{}".format(mBtn1, mBtn2, mBtn3), True, PINK)
26 |
27 | screen.fill(BLACK)
28 | screen.blit(txt1, [100, 100])
29 | screen.blit(txt2, [100, 200])
30 | pygame.display.update()
31 | clock.tick(10)
32 |
33 | if __name__ == '__main__':
34 | main()
35 |
--------------------------------------------------------------------------------
/Chapter10/pygame_music.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 |
4 | WHITE = (255, 255, 255)
5 | BLACK = (0, 0, 0)
6 | CYAN = (0, 255, 255)
7 |
8 | def main():
9 | pygame.init()
10 | pygame.display.set_caption("첫 번째 Pygame: 사운드 출력")
11 | screen = pygame.display.set_mode((800, 600))
12 | clock = pygame.time.Clock()
13 | font = pygame.font.Font(None, 40)
14 |
15 | try:
16 | pygame.mixer.music.load("pygame_bgm.ogg")
17 | se = pygame.mixer.Sound("pygame_se.ogg")
18 | except:
19 | print("ogg 파일이 맞지 않거나, 오디오 기기가 접속되어 있지 않습니다")
20 |
21 | while True:
22 | for event in pygame.event.get():
23 | if event.type == pygame.QUIT:
24 | pygame.quit()
25 | sys.exit()
26 |
27 | key = pygame.key.get_pressed()
28 | if key[pygame.K_p] == 1:
29 | if pygame.mixer.music.get_busy() == False:
30 | pygame.mixer.music.play(-1)
31 | if key[pygame.K_s] == 1:
32 | if pygame.mixer.music.get_busy() == True:
33 | pygame.mixer.music.stop()
34 | if key[pygame.K_SPACE] == 1:
35 | se.play()
36 |
37 | pos = pygame.mixer.music.get_pos()
38 | txt1 = font.render("BGM pos" + str(pos), True, WHITE)
39 | txt2 = font.render("[P]lay bgm : [S]top bgm : [SPACE] se", True, CYAN)
40 | screen.fill(BLACK)
41 | screen.blit(txt1, [100, 100])
42 | screen.blit(txt2, [100, 200])
43 | pygame.display.update()
44 | clock.tick(10)
45 |
46 | if __name__ == '__main__':
47 | main()
48 |
--------------------------------------------------------------------------------
/Chapter10/pygame_se.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter10/pygame_se.ogg
--------------------------------------------------------------------------------
/Chapter10/pygame_system.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 |
4 | WHITE = (255, 255, 255)
5 | BLACK = (0, 0, 0)
6 |
7 | def main():
8 | pygame.init()
9 | pygame.display.set_caption("첫 번째 Pygame")
10 | screen = pygame.display.set_mode((800, 600))
11 | clock = pygame.time.Clock()
12 | font = pygame.font.Font(None, 80)
13 | tmr = 0
14 |
15 | while True:
16 | tmr = tmr + 1
17 | for event in pygame.event.get():
18 | if event.type == pygame.QUIT:
19 | pygame.quit()
20 | sys.exit()
21 |
22 | txt = font.render(str(tmr), True, WHITE)
23 | screen.fill(BLACK)
24 | screen.blit(txt, [300, 200])
25 | pygame.display.update()
26 | clock.tick(10)
27 |
28 | if __name__ == '__main__':
29 | main()
30 |
--------------------------------------------------------------------------------
/Chapter11/battle_message.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 |
4 | WHITE = (255, 255, 255)
5 | BLACK = (0, 0, 0)
6 |
7 | imgBtlBG = pygame.image.load("btlbg.png")
8 | imgEnemy = pygame.image.load("enemy1.png")
9 | emy_x = 440 - imgEnemy.get_width() / 2
10 | emy_y = 560 - imgEnemy.get_height()
11 |
12 | message = [""] * 10
13 | def init_message():
14 | for i in range(10):
15 | message[i] = ""
16 |
17 | def set_message(msg):
18 | for i in range(10):
19 | if message[i] == "":
20 | message[i] = msg
21 | return
22 | for i in range(9):
23 | message[i] = message[i + 1]
24 | message[9] = msg
25 |
26 | def draw_text(bg, txt, x, y, fnt, col):
27 | sur = fnt.render(txt, True, BLACK)
28 | bg.blit(sur, [x + 1, y + 2])
29 | sur = fnt.render(txt, True, col)
30 | bg.blit(sur, [x, y])
31 |
32 | def draw_battle(bg, fnt):
33 | bg.blit(imgBtlBG, [0, 0])
34 | bg.blit(imgEnemy, [emy_x, emy_y])
35 | for i in range(10):
36 | draw_text(bg, message[i], 600, 100 + i * 50, fnt, WHITE)
37 |
38 | def main():
39 | pygame.init()
40 | pygame.display.set_caption("전투 중 메시지")
41 | screen = pygame.display.set_mode((880, 720))
42 | clock = pygame.time.Clock()
43 | font = pygame.font.Font(None, 40)
44 |
45 | init_message()
46 |
47 | while True:
48 | for event in pygame.event.get():
49 | if event.type == pygame.QUIT:
50 | pygame.quit()
51 | sys.exit()
52 | if event.type == pygame.KEYDOWN:
53 | set_message("KEYDOWN " + str(event.key))
54 |
55 | draw_battle(screen, font)
56 | pygame.display.update()
57 | clock.tick(5)
58 |
59 | if __name__ == '__main__':
60 | main()
61 |
--------------------------------------------------------------------------------
/Chapter11/battle_start.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 |
4 | WHITE = (255, 255, 255)
5 |
6 | imgBtlBG = pygame.image.load("btlbg.png")
7 | imgEnemy = None
8 |
9 | emy_num = 0
10 | emy_x = 0
11 | emy_y = 0
12 |
13 | def init_battle():
14 | global imgEnemy, emy_num, emy_x, emy_y
15 | emy_num = emy_num + 1
16 | if emy_num == 5:
17 | emy_num = 1
18 | imgEnemy = pygame.image.load("enemy" + str(emy_num) + ".png")
19 | emy_x = 440 - imgEnemy.get_width() / 2
20 | emy_y = 560 - imgEnemy.get_height()
21 |
22 | def draw_battle(bg, fnt):
23 | bg.blit(imgBtlBG, [0, 0])
24 | bg.blit(imgEnemy, [emy_x, emy_y])
25 | sur = fnt.render("enemy" + str(emy_num) + ".png", True, WHITE)
26 | bg.blit(sur, [360, 580])
27 |
28 | def main():
29 | pygame.init()
30 | pygame.display.set_caption("전투 개시 처리")
31 | screen = pygame.display.set_mode((880, 720))
32 | clock = pygame.time.Clock()
33 | font = pygame.font.Font(None, 40)
34 |
35 | init_battle()
36 |
37 | while True:
38 | for event in pygame.event.get():
39 | if event.type == pygame.QUIT:
40 | pygame.quit()
41 | sys.exit()
42 | if event.type == pygame.KEYDOWN:
43 | if event.key == pygame.K_SPACE:
44 | init_battle()
45 |
46 | draw_battle(screen, font)
47 | pygame.display.update()
48 | clock.tick(5)
49 |
50 | if __name__ == '__main__':
51 | main()
52 |
--------------------------------------------------------------------------------
/Chapter11/battle_turn.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 | import random
4 | from pygame.locals import *
5 |
6 | WHITE = (255, 255, 255)
7 | BLACK = (0, 0, 0)
8 |
9 | imgBtlBG = pygame.image.load("btlbg.png")
10 | imgEffect = pygame.image.load("effect_a.png")
11 | imgEnemy = pygame.image.load("enemy4.png")
12 | emy_x = 440 - imgEnemy.get_width() / 2
13 | emy_y = 560 - imgEnemy.get_height()
14 | emy_step = 0
15 | emy_blink = 0
16 | dmg_eff = 0
17 | COMMAND = ["[A]ttack", "[P]otion", "[B]laze gem", "[R]un"]
18 |
19 | message = [""] * 10
20 | def init_message():
21 | for i in range(10):
22 | message[i] = ""
23 |
24 | def set_message(msg):
25 | for i in range(10):
26 | if message[i] == "":
27 | message[i] = msg
28 | return
29 | for i in range(9):
30 | message[i] = message[i + 1]
31 | message[9] = msg
32 |
33 | def draw_text(bg, txt, x, y, fnt, col):
34 | sur = fnt.render(txt, True, BLACK)
35 | bg.blit(sur, [x + 1, y + 2])
36 | sur = fnt.render(txt, True, col)
37 | bg.blit(sur, [x, y])
38 |
39 | def draw_battle(bg, fnt):
40 | global emy_blink, dmg_eff
41 | bx = 0
42 | by = 0
43 | if dmg_eff > 0:
44 | dmg_eff = dmg_eff - 1
45 | bx = random.randint(-20, 20)
46 | by = random.randint(-10, 10)
47 | bg.blit(imgBtlBG, [bx, by])
48 | if emy_blink % 2 == 0:
49 | bg.blit(imgEnemy, [emy_x, emy_y + emy_step])
50 | if emy_blink > 0:
51 | emy_blink = emy_blink - 1
52 | for i in range(10):
53 | draw_text(bg, message[i], 600, 100 + i * 50, fnt, WHITE)
54 |
55 | def battle_command(bg, fnt):
56 | for i in range(4):
57 | draw_text(bg, COMMAND[i], 20, 360 + 60 * i, fnt, WHITE)
58 |
59 | def main():
60 | global emy_step, emy_blink, dmg_eff
61 | idx = 10
62 | tmr = 0
63 |
64 | pygame.init()
65 | pygame.display.set_caption("턴 처리")
66 | screen = pygame.display.set_mode((880, 720))
67 | clock = pygame.time.Clock()
68 | font = pygame.font.Font(None, 30)
69 |
70 | init_message()
71 |
72 | while True:
73 | for event in pygame.event.get():
74 | if event.type == QUIT:
75 | pygame.quit()
76 | sys.exit()
77 |
78 | draw_battle(screen, font)
79 | tmr = tmr + 1
80 | key = pygame.key.get_pressed()
81 |
82 | if idx == 10: # 전투 개시
83 | if tmr == 1: set_message("Encounter!")
84 | if tmr == 6:
85 | idx = 11
86 | tmr = 0
87 |
88 | elif idx == 11: # 플레이어 입력 대기
89 | if tmr == 1: set_message("Your turn.")
90 | battle_command(screen, font)
91 | if key[K_a] == 1 or key[K_SPACE] == 1:
92 | idx = 12
93 | tmr = 0
94 |
95 | elif idx == 12: # 플레이어 공격
96 | if tmr == 1: set_message("You attack!")
97 | if 2 <= tmr and tmr <= 4:
98 | screen.blit(imgEffect, [700 - tmr * 120, -100 + tmr * 120])
99 | if tmr == 5:
100 | emy_blink = 5
101 | set_message("***pts of damage!")
102 | if tmr == 16:
103 | idx = 13
104 | tmr = 0
105 |
106 | elif idx == 13: # 적 턴, 적 공격
107 | if tmr == 1: set_message("Enemy turn.")
108 | if tmr == 5:
109 | set_message("Enemy attack!")
110 | emy_step = 30
111 | if tmr == 9:
112 | set_message("***pts of damage!")
113 | dmg_eff = 5
114 | emy_step = 0
115 | if tmr == 20:
116 | idx = 11
117 | tmr = 0
118 |
119 | pygame.display.update()
120 | clock.tick(5)
121 |
122 | if __name__ == '__main__':
123 | main()
124 |
--------------------------------------------------------------------------------
/Chapter11/btlbg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter11/btlbg.png
--------------------------------------------------------------------------------
/Chapter11/column11.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 | import random
4 |
5 | WHITE = (255, 255, 255)
6 | BLACK = (0, 0, 0)
7 |
8 | def main():
9 | pygame.init()
10 | pygame.display.set_caption("반투명과 스크롤")
11 | screen = pygame.display.set_mode((800, 600))
12 | clock = pygame.time.Clock()
13 |
14 | surface_a = pygame.Surface((800, 600))
15 | surface_a.fill(BLACK)
16 | surface_a.set_alpha(32)
17 |
18 | CHIP_MAX = 50
19 | cx = [0] * CHIP_MAX
20 | cy = [0] * CHIP_MAX
21 | xp = [0] * CHIP_MAX
22 | yp = [0] * CHIP_MAX
23 | for i in range(CHIP_MAX):
24 | cx[i] = random.randint(0, 800)
25 | cy[i] = random.randint(0, 600)
26 |
27 | while True:
28 | for event in pygame.event.get():
29 | if event.type == pygame.QUIT:
30 | pygame.quit()
31 | sys.exit()
32 |
33 | screen.scroll(1, 4)
34 | screen.blit(surface_a, [0, 0])
35 |
36 | mx, my = pygame.mouse.get_pos()
37 | pygame.draw.rect(screen, WHITE, [mx - 4, my - 4, 8, 8])
38 |
39 | for i in range(CHIP_MAX):
40 | if mx < cx[i] and xp[i] > -20: xp[i] = xp[i] - 1
41 | if mx > cx[i] and xp[i] < 20: xp[i] = xp[i] + 1
42 | if my < cy[i] and yp[i] > -16: yp[i] = yp[i] - 1
43 | if my > cy[i] and yp[i] < 16: yp[i] = yp[i] + 1
44 | cx[i] = cx[i] + xp[i]
45 | cy[i] = cy[i] + yp[i]
46 | pygame.draw.circle(screen, (0, 64, 192), [cx[i], cy[i]], 12)
47 | pygame.draw.circle(screen, (0, 128, 224), [cx[i], cy[i]], 9)
48 | pygame.draw.circle(screen, (192, 224, 255), [cx[i], cy[i]], 6)
49 |
50 | pygame.display.update()
51 | clock.tick(30)
52 |
53 | if __name__ == '__main__':
54 | main()
55 |
--------------------------------------------------------------------------------
/Chapter11/dungeon_maker.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 | import random
4 |
5 | BLACK = (0, 0, 0)
6 | CYAN = (0, 255, 255)
7 | GRAY = (96, 96, 96)
8 |
9 | MAZE_W = 11
10 | MAZE_H = 9
11 | maze = []
12 | for y in range(MAZE_H):
13 | maze.append([0] * MAZE_W)
14 |
15 | DUNGEON_W = MAZE_W * 3
16 | DUNGEON_H = MAZE_H * 3
17 | dungeon = []
18 | for y in range(DUNGEON_H):
19 | dungeon.append([0] * DUNGEON_W)
20 |
21 | imgWall = pygame.image.load("wall.png")
22 | imgFloor = pygame.image.load("floor.png")
23 |
24 | def make_dungeon(): # 던전 자동 생성
25 | XP = [0, 1, 0, -1]
26 | YP = [-1, 0, 1, 0]
27 | # 주변 벽
28 | for x in range(MAZE_W):
29 | maze[0][x] = 1
30 | maze[MAZE_H - 1][x] = 1
31 | for y in range(1, MAZE_H - 1):
32 | maze[y][0] = 1
33 | maze[y][MAZE_W - 1] = 1
34 | # 내부를 아무것도 없는 상태로
35 | for y in range(1, MAZE_H - 1):
36 | for x in range(1, MAZE_W - 1):
37 | maze[y][x] = 0
38 | # 기둥
39 | for y in range(2, MAZE_H - 2, 2):
40 | for x in range(2, MAZE_W - 2, 2):
41 | maze[y][x] = 1
42 | # 기둥으로부터 상하좌우로 벽 생성
43 | for y in range(2, MAZE_H - 2, 2):
44 | for x in range(2, MAZE_W - 2, 2):
45 | d = random.randint(0, 3)
46 | if x > 2: # 2번째 열부터 왼쪽으로는 벽을 만들지 않음
47 | d = random.randint(0, 2)
48 | maze[y + YP[d]][x + XP[d]] = 1
49 |
50 | # 미로에서 던전 생성
51 | # 전체를 벽으로 만듬
52 | for y in range(DUNGEON_H):
53 | for x in range(DUNGEON_W):
54 | dungeon[y][x] = 9
55 | # 방과 미로 배치
56 | for y in range(1, MAZE_H - 1):
57 | for x in range(1, MAZE_W - 1):
58 | dx = x * 3 + 1
59 | dy = y * 3 + 1
60 | if maze[y][x] == 0:
61 | if random.randint(0, 99) < 20: # 방 생성
62 | for ry in range(-1, 2):
63 | for rx in range(-1, 2):
64 | dungeon[dy + ry][dx + rx] = 0
65 | else: # 통로 생성
66 | dungeon[dy][dx] = 0
67 | if maze[y - 1][x] == 0:
68 | dungeon[dy - 1][dx] = 0
69 | if maze[y + 1][x] == 0:
70 | dungeon[dy + 1][dx] = 0
71 | if maze[y][x - 1] == 0:
72 | dungeon[dy][dx - 1] = 0
73 | if maze[y][x + 1] == 0:
74 | dungeon[dy][dx + 1] = 0
75 |
76 | def main():
77 | pygame.init()
78 | pygame.display.set_caption("던전 생성")
79 | screen = pygame.display.set_mode((1056, 432))
80 | clock = pygame.time.Clock()
81 |
82 | make_dungeon()
83 |
84 | while True:
85 | for event in pygame.event.get():
86 | if event.type == pygame.QUIT:
87 | pygame.quit()
88 | sys.exit()
89 | if event.type == pygame.KEYDOWN:
90 | if event.key == pygame.K_SPACE:
91 | make_dungeon()
92 |
93 | # 확인용 미로 표시
94 | for y in range(MAZE_H):
95 | for x in range(MAZE_W):
96 | X = x * 48
97 | Y = y * 48
98 | if maze[y][x] == 0:
99 | pygame.draw.rect(screen, CYAN, [X, Y, 48, 48])
100 | if maze[y][x] == 1:
101 | pygame.draw.rect(screen, GRAY, [X, Y, 48, 48])
102 |
103 | # 던전 그리기
104 | for y in range(DUNGEON_H):
105 | for x in range(DUNGEON_W):
106 | X = x * 16 + 528
107 | Y = y * 16
108 | if dungeon[y][x] == 0:
109 | screen.blit(imgFloor, [X, Y])
110 | if dungeon[y][x] == 9:
111 | screen.blit(imgWall, [X, Y])
112 |
113 | pygame.display.update()
114 | clock.tick(2)
115 |
116 | if __name__ == '__main__':
117 | main()
118 |
--------------------------------------------------------------------------------
/Chapter11/effect_a.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter11/effect_a.png
--------------------------------------------------------------------------------
/Chapter11/enemy1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter11/enemy1.png
--------------------------------------------------------------------------------
/Chapter11/enemy2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter11/enemy2.png
--------------------------------------------------------------------------------
/Chapter11/enemy3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter11/enemy3.png
--------------------------------------------------------------------------------
/Chapter11/enemy4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter11/enemy4.png
--------------------------------------------------------------------------------
/Chapter11/floor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter11/floor.png
--------------------------------------------------------------------------------
/Chapter11/maze_maker.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 | import random
4 |
5 | CYAN = (0, 255, 255)
6 | GRAY = (96, 96, 96)
7 |
8 | MAZE_W = 11
9 | MAZE_H = 9
10 | maze = []
11 | for y in range(MAZE_H):
12 | maze.append([0] * MAZE_W)
13 |
14 | def make_maze():
15 | XP = [0, 1, 0, -1]
16 | YP = [-1, 0, 1, 0]
17 |
18 | # 주변 벽
19 | for x in range(MAZE_W):
20 | maze[0][x] = 1
21 | maze[MAZE_H - 1][x] = 1
22 | for y in range(1, MAZE_H - 1):
23 | maze[y][0] = 1
24 | maze[y][MAZE_W - 1] = 1
25 |
26 | # 안을 아무것도 없는 상태로
27 | for y in range(1, MAZE_H - 1):
28 | for x in range(1, MAZE_W - 1):
29 | maze[y][x] = 0
30 |
31 | # 기둥
32 | for y in range(2, MAZE_H - 2, 2):
33 | for x in range(2, MAZE_W - 2, 2):
34 | maze[y][x] = 1
35 |
36 | # 기둥에서 상하좌우로 벽 생성
37 | for y in range(2, MAZE_H - 2, 2):
38 | for x in range(2, MAZE_W - 2, 2):
39 | d = random.randint(0, 3)
40 | if x > 2: # 2번째 열부터 왼쪽으로는 벽을 만들지 않음
41 | d = random.randint(0, 2)
42 | maze[y + YP[d]][x + XP[d]] = 1
43 |
44 |
45 | def main():
46 | pygame.init()
47 | pygame.display.set_caption("미로 생성")
48 | screen = pygame.display.set_mode((528, 432))
49 | clock = pygame.time.Clock()
50 |
51 | make_maze()
52 |
53 | while True:
54 | for event in pygame.event.get():
55 | if event.type == pygame.QUIT:
56 | pygame.quit()
57 | sys.exit()
58 | if event.type == pygame.KEYDOWN:
59 | if event.key == pygame.K_SPACE:
60 | make_maze()
61 |
62 | for y in range(MAZE_H):
63 | for x in range(MAZE_W):
64 | W = 48
65 | H = 48
66 | X = x * W
67 | Y = y * H
68 | if maze[y][x] == 0: # 미로
69 | pygame.draw.rect(screen, CYAN, [X, Y, W, H])
70 | if maze[y][x] == 1: # 벽
71 | pygame.draw.rect(screen, GRAY, [X, Y, W, H])
72 |
73 | pygame.display.update()
74 | clock.tick(2)
75 |
76 | if __name__ == '__main__':
77 | main()
78 |
--------------------------------------------------------------------------------
/Chapter11/player.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter11/player.png
--------------------------------------------------------------------------------
/Chapter11/walk_in_dungeon.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 | import random
4 |
5 | BLACK = (0, 0, 0)
6 |
7 | MAZE_W = 11
8 | MAZE_H = 9
9 | maze = []
10 | for y in range(MAZE_H):
11 | maze.append([0] * MAZE_W)
12 |
13 | DUNGEON_W = MAZE_W * 3
14 | DUNGEON_H = MAZE_H * 3
15 | dungeon = []
16 | for y in range(DUNGEON_H):
17 | dungeon.append([0] * DUNGEON_W)
18 |
19 | imgWall = pygame.image.load("wall.png")
20 | imgFloor = pygame.image.load("floor.png")
21 | imgPlayer = pygame.image.load("player.png")
22 |
23 | pl_x = 4
24 | pl_y = 4
25 |
26 | def make_dungeon(): # 던전 자동 생성
27 | XP = [0, 1, 0, -1]
28 | YP = [-1, 0, 1, 0]
29 | # 주변 벽
30 | for x in range(MAZE_W):
31 | maze[0][x] = 1
32 | maze[MAZE_H - 1][x] = 1
33 | for y in range(1, MAZE_H - 1):
34 | maze[y][0] = 1
35 | maze[y][MAZE_W - 1] = 1
36 | # 안을 아무것도 없는 상태로
37 | for y in range(1, MAZE_H - 1):
38 | for x in range(1, MAZE_W - 1):
39 | maze[y][x] = 0
40 | # 기둥
41 | for y in range(2, MAZE_H - 2, 2):
42 | for x in range(2, MAZE_W - 2, 2):
43 | maze[y][x] = 1
44 | # 기둥에서 상하좌우로 벽 생성
45 | for y in range(2, MAZE_H - 2, 2):
46 | for x in range(2, MAZE_W - 2, 2):
47 | d = random.randint(0, 3)
48 | if x > 2: # 2번째 열부터 왼쪽으로 벽을 만들지 않음
49 | d = random.randint(0, 2)
50 | maze[y + YP[d]][x + XP[d]] = 1
51 |
52 | # 미로에서 던전 생성
53 | # 전체를 벽으로
54 | for y in range(DUNGEON_H):
55 | for x in range(DUNGEON_W):
56 | dungeon[y][x] = 9
57 | # 방과 미로 배치
58 | for y in range(1, MAZE_H - 1):
59 | for x in range(1, MAZE_W - 1):
60 | dx = x * 3 + 1
61 | dy = y * 3 + 1
62 | if maze[y][x] == 0:
63 | if random.randint(0, 99) < 20: # 방 생성
64 | for ry in range(-1, 2):
65 | for rx in range(-1, 2):
66 | dungeon[dy + ry][dx + rx] = 0
67 | else: # 미로 생성
68 | dungeon[dy][dx] = 0
69 | if maze[y - 1][x] == 0:
70 | dungeon[dy - 1][dx] = 0
71 | if maze[y + 1][x] == 0:
72 | dungeon[dy + 1][dx] = 0
73 | if maze[y][x - 1] == 0:
74 | dungeon[dy][dx - 1] = 0
75 | if maze[y][x + 1] == 0:
76 | dungeon[dy][dx + 1] = 0
77 |
78 | def draw_dungeon(bg): # 던전 표시
79 | bg.fill(BLACK)
80 | for y in range(-5, 6):
81 | for x in range(-5, 6):
82 | X = (x + 5) * 16
83 | Y = (y + 5) * 16
84 | dx = pl_x + x
85 | dy = pl_y + y
86 | if 0 <= dx and dx < DUNGEON_W and 0 <= dy and dy < DUNGEON_H:
87 | if dungeon[dy][dx] == 0:
88 | bg.blit(imgFloor, [X, Y])
89 | if dungeon[dy][dx] == 9:
90 | bg.blit(imgWall, [X, Y])
91 | if x == 0 and y == 0: # 주인공 표시
92 | bg.blit(imgPlayer, [X, Y - 8])
93 |
94 | def move_player(): # 주인공 이동
95 | global pl_x, pl_y
96 | key = pygame.key.get_pressed()
97 | if key[pygame.K_UP] == 1:
98 | if dungeon[pl_y - 1][pl_x] != 9: pl_y = pl_y - 1
99 | if key[pygame.K_DOWN] == 1:
100 | if dungeon[pl_y + 1][pl_x] != 9: pl_y = pl_y + 1
101 | if key[pygame.K_LEFT] == 1:
102 | if dungeon[pl_y][pl_x - 1] != 9: pl_x = pl_x - 1
103 | if key[pygame.K_RIGHT] == 1:
104 | if dungeon[pl_y][pl_x + 1] != 9: pl_x = pl_x + 1
105 |
106 | def main():
107 | pygame.init()
108 | pygame.display.set_caption("던전 내 걷기")
109 | screen = pygame.display.set_mode((176, 176))
110 | clock = pygame.time.Clock()
111 |
112 | make_dungeon()
113 |
114 | while True:
115 | for event in pygame.event.get():
116 | if event.type == pygame.QUIT:
117 | pygame.quit()
118 | sys.exit()
119 |
120 | move_player()
121 | draw_dungeon(screen)
122 | pygame.display.update()
123 | clock.tick(5)
124 |
125 | if __name__ == '__main__':
126 | main()
127 |
--------------------------------------------------------------------------------
/Chapter11/wall.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter11/wall.png
--------------------------------------------------------------------------------
/Chapter12/column12_file_read.py:
--------------------------------------------------------------------------------
1 | file = open("test2.txt", 'r')
2 | rl = file.readlines()
3 | file.close()
4 | for i in rl:
5 | print(i.rstrip("\n"))
6 |
--------------------------------------------------------------------------------
/Chapter12/column12_file_write.py:
--------------------------------------------------------------------------------
1 | file = open("test.txt", 'w')
2 | for i in range(10):
3 | file.write("line " + str(i) + "\n")
4 | file.close()
5 |
--------------------------------------------------------------------------------
/Chapter12/column12_int.py:
--------------------------------------------------------------------------------
1 | num1 = "1000"
2 | print(num1 + num1)
3 | num2 = int(num1)
4 | print(num2 + num2)
5 |
--------------------------------------------------------------------------------
/Chapter12/image/apple.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/apple.png
--------------------------------------------------------------------------------
/Chapter12/image/blaze_gem.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/blaze_gem.png
--------------------------------------------------------------------------------
/Chapter12/image/btlbg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/btlbg.png
--------------------------------------------------------------------------------
/Chapter12/image/cocoon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/cocoon.png
--------------------------------------------------------------------------------
/Chapter12/image/dark.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/dark.png
--------------------------------------------------------------------------------
/Chapter12/image/effect_a.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/effect_a.png
--------------------------------------------------------------------------------
/Chapter12/image/effect_b.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/effect_b.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy0.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy1.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy2.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy3.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy4.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy5.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy6.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy7.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy8.png
--------------------------------------------------------------------------------
/Chapter12/image/enemy9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/enemy9.png
--------------------------------------------------------------------------------
/Chapter12/image/floor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/floor.png
--------------------------------------------------------------------------------
/Chapter12/image/meat.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/meat.png
--------------------------------------------------------------------------------
/Chapter12/image/mychr0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/mychr0.png
--------------------------------------------------------------------------------
/Chapter12/image/mychr1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/mychr1.png
--------------------------------------------------------------------------------
/Chapter12/image/mychr2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/mychr2.png
--------------------------------------------------------------------------------
/Chapter12/image/mychr3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/mychr3.png
--------------------------------------------------------------------------------
/Chapter12/image/mychr4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/mychr4.png
--------------------------------------------------------------------------------
/Chapter12/image/mychr5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/mychr5.png
--------------------------------------------------------------------------------
/Chapter12/image/mychr6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/mychr6.png
--------------------------------------------------------------------------------
/Chapter12/image/mychr7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/mychr7.png
--------------------------------------------------------------------------------
/Chapter12/image/mychr8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/mychr8.png
--------------------------------------------------------------------------------
/Chapter12/image/parameter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/parameter.png
--------------------------------------------------------------------------------
/Chapter12/image/potion.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/potion.png
--------------------------------------------------------------------------------
/Chapter12/image/spoiled.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/spoiled.png
--------------------------------------------------------------------------------
/Chapter12/image/stairs.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/stairs.png
--------------------------------------------------------------------------------
/Chapter12/image/tbox.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/tbox.png
--------------------------------------------------------------------------------
/Chapter12/image/title.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/title.png
--------------------------------------------------------------------------------
/Chapter12/image/wall.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/wall.png
--------------------------------------------------------------------------------
/Chapter12/image/wall2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/image/wall2.png
--------------------------------------------------------------------------------
/Chapter12/one_hour_dungeon.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import sys
3 | import random
4 | from pygame.locals import *
5 |
6 | # 색 정의
7 | WHITE = (255, 255, 255)
8 | BLACK = (0, 0, 0)
9 | RED = (255, 0, 0)
10 | CYAN = (0, 255, 255)
11 | BLINK = [(224, 255, 255), (192, 240, 255), (128, 224, 255), (64, 192, 255), (128, 224, 255), (192, 240, 255)]
12 |
13 | # 이미지 로딩
14 | imgTitle = pygame.image.load("image/title.png")
15 | imgWall = pygame.image.load("image/wall.png")
16 | imgWall2 = pygame.image.load("image/wall2.png")
17 | imgDark = pygame.image.load("image/dark.png")
18 | imgPara = pygame.image.load("image/parameter.png")
19 | imgBtlBG = pygame.image.load("image/btlbg.png")
20 | imgEnemy = pygame.image.load("image/enemy0.png")
21 | imgItem = [
22 | pygame.image.load("image/potion.png"),
23 | pygame.image.load("image/blaze_gem.png"),
24 | pygame.image.load("image/spoiled.png"),
25 | pygame.image.load("image/apple.png"),
26 | pygame.image.load("image/meat.png")
27 | ]
28 | imgFloor = [
29 | pygame.image.load("image/floor.png"),
30 | pygame.image.load("image/tbox.png"),
31 | pygame.image.load("image/cocoon.png"),
32 | pygame.image.load("image/stairs.png")
33 | ]
34 | imgPlayer = [
35 | pygame.image.load("image/mychr0.png"),
36 | pygame.image.load("image/mychr1.png"),
37 | pygame.image.load("image/mychr2.png"),
38 | pygame.image.load("image/mychr3.png"),
39 | pygame.image.load("image/mychr4.png"),
40 | pygame.image.load("image/mychr5.png"),
41 | pygame.image.load("image/mychr6.png"),
42 | pygame.image.load("image/mychr7.png"),
43 | pygame.image.load("image/mychr8.png")
44 | ]
45 | imgEffect = [
46 | pygame.image.load("image/effect_a.png"),
47 | pygame.image.load("image/effect_b.png")
48 | ]
49 |
50 | # 변수 선언
51 | speed = 1
52 | idx = 0
53 | tmr = 0
54 | floor = 0
55 | fl_max = 1
56 | welcome = 0
57 |
58 | pl_x = 0
59 | pl_y = 0
60 | pl_d = 0
61 | pl_a = 0
62 | pl_lifemax = 0
63 | pl_life = 0
64 | pl_str = 0
65 | food = 0
66 | potion = 0
67 | blazegem = 0
68 | treasure = 0
69 |
70 | emy_name = ""
71 | emy_lifemax = 0
72 | emy_life = 0
73 | emy_str = 0
74 | emy_x = 0
75 | emy_y = 0
76 | emy_step = 0
77 | emy_blink = 0
78 |
79 | dmg_eff = 0
80 | btl_cmd = 0
81 |
82 | COMMAND = ["[A] Attack", "[P]otion", "[B]laze gem", "[R]un"]
83 | TRE_NAME = ["Potion", "Blaze gem", "Food spoiled.", "Food +20", "Food +100"]
84 | EMY_NAME = [
85 | "Green slime", "Red slime", "Axe beast", "Ogre", "Sword man",
86 | "Death hornet", "Signal slime", "Devil plant", "Twin killer", "Hell"
87 | ]
88 |
89 | MAZE_W = 11
90 | MAZE_H = 9
91 | maze = []
92 | for y in range(MAZE_H):
93 | maze.append([0] * MAZE_W)
94 |
95 | DUNGEON_W = MAZE_W * 3
96 | DUNGEON_H = MAZE_H * 3
97 | dungeon = []
98 | for y in range(DUNGEON_H):
99 | dungeon.append([0] * DUNGEON_W)
100 |
101 | def make_dungeon(): # 던전 자동 생성
102 | XP = [0, 1, 0, -1]
103 | YP = [-1, 0, 1, 0]
104 | # 테두리 벽
105 | for x in range(MAZE_W):
106 | maze[0][x] = 1
107 | maze[MAZE_H - 1][x] = 1
108 | for y in range(1, MAZE_H - 1):
109 | maze[y][0] = 1
110 | maze[y][MAZE_W - 1] = 1
111 | # 가운데를 아무것도 없는 상태로 만듬
112 | for y in range(1, MAZE_H - 1):
113 | for x in range(1, MAZE_W - 1):
114 | maze[y][x] = 0
115 | # 기둥
116 | for y in range(2, MAZE_H - 2, 2):
117 | for x in range(2, MAZE_W - 2, 2):
118 | maze[y][x] = 1
119 | # 기둥에서 상하좌우로 벽 생성
120 | for y in range(2, MAZE_H - 2, 2):
121 | for x in range(2, MAZE_W - 2, 2):
122 | d = random.randint(0, 3)
123 | if x > 2: # 2열부터 왼쪽에는 벽을 생성하지 않음
124 | d = random.randint(0, 2)
125 | maze[y + YP[d]][x + XP[d]] = 1
126 |
127 | # 미로에서 던전 생성
128 | # 전체를 벽으로
129 | for y in range(DUNGEON_H):
130 | for x in range(DUNGEON_W):
131 | dungeon[y][x] = 9
132 | # 방과 통로 배치
133 | for y in range(1, MAZE_H - 1):
134 | for x in range(1, MAZE_W - 1):
135 | dx = x * 3 + 1
136 | dy = y * 3 + 1
137 | if maze[y][x] == 0:
138 | if random.randint(0, 99) < 20: # 방 생성
139 | for ry in range(-1, 2):
140 | for rx in range(-1, 2):
141 | dungeon[dy + ry][dx + rx] = 0
142 | else: # 통로 생성
143 | dungeon[dy][dx] = 0
144 | if maze[y - 1][x] == 0: dungeon[dy - 1][dx] = 0
145 | if maze[y + 1][x] == 0: dungeon[dy + 1][dx] = 0
146 | if maze[y][x - 1] == 0: dungeon[dy][dx - 1] = 0
147 | if maze[y][x + 1] == 0: dungeon[dy][dx + 1] = 0
148 |
149 | def draw_dungeon(bg, fnt): # 던전 표시
150 | bg.fill(BLACK)
151 | for y in range(-4, 6):
152 | for x in range(-5, 6):
153 | X = (x + 5) * 80
154 | Y = (y + 4) * 80
155 | dx = pl_x + x
156 | dy = pl_y + y
157 | if 0 <= dx and dx < DUNGEON_W and 0 <= dy and dy < DUNGEON_H:
158 | if dungeon[dy][dx] <= 3:
159 | bg.blit(imgFloor[dungeon[dy][dx]], [X, Y])
160 | if dungeon[dy][dx] == 9:
161 | bg.blit(imgWall, [X, Y - 40])
162 | if dy >= 1 and dungeon[dy - 1][dx] == 9:
163 | bg.blit(imgWall2, [X, Y - 80])
164 | if x == 0 and y == 0: # 주인공 캐릭터 표시
165 | bg.blit(imgPlayer[pl_a], [X, Y - 40])
166 | bg.blit(imgDark, [0, 0]) # 네 모서리가 어두운 이미지 겹침
167 | draw_para(bg, fnt) # 주인공 능력치 표시
168 |
169 | def put_event(): # 길에 이벤트 배치
170 | global pl_x, pl_y, pl_d, pl_a
171 | # 계단 배치
172 | while True:
173 | x = random.randint(3, DUNGEON_W - 4)
174 | y = random.randint(3, DUNGEON_H - 4)
175 | if (dungeon[y][x] == 0):
176 | for ry in range(-1, 2): # 계단 주변을 길로 만듬
177 | for rx in range(-1, 2):
178 | dungeon[y + ry][x + rx] = 0
179 | dungeon[y][x] = 3
180 | break
181 | # 보물 상자와 누에고치 배치
182 | for i in range(60):
183 | x = random.randint(3, DUNGEON_W - 4)
184 | y = random.randint(3, DUNGEON_H - 4)
185 | if (dungeon[y][x] == 0):
186 | dungeon[y][x] = random.choice([1, 2, 2, 2, 2])
187 | # 플레이어 초기 위치
188 | while True:
189 | pl_x = random.randint(3, DUNGEON_W - 4)
190 | pl_y = random.randint(3, DUNGEON_H - 4)
191 | if (dungeon[pl_y][pl_x] == 0):
192 | break
193 | pl_d = 1
194 | pl_a = 2
195 |
196 | def move_player(key): # 주인공 이동
197 | global idx, tmr, pl_x, pl_y, pl_d, pl_a, pl_life, food, potion, blazegem, treasure
198 |
199 | if dungeon[pl_y][pl_x] == 1: # 보물상자에 닿음
200 | dungeon[pl_y][pl_x] = 0
201 | treasure = random.choice([0, 0, 0, 1, 1, 1, 1, 1, 1, 2])
202 | if treasure == 0:
203 | potion = potion + 1
204 | if treasure == 1:
205 | blazegem = blazegem + 1
206 | if treasure == 2:
207 | food = int(food / 2)
208 | idx = 3
209 | tmr = 0
210 | return
211 | if dungeon[pl_y][pl_x] == 2: # 누에고치에 닿음
212 | dungeon[pl_y][pl_x] = 0
213 | r = random.randint(0, 99)
214 | if r < 40: # 식량
215 | treasure = random.choice([3, 3, 3, 4])
216 | if treasure == 3: food = food + 20
217 | if treasure == 4: food = food + 100
218 | idx = 3
219 | tmr = 0
220 | else: # 적 출현
221 | idx = 10
222 | tmr = 0
223 | return
224 | if dungeon[pl_y][pl_x] == 3: # 계단에 닿음
225 | idx = 2
226 | tmr = 0
227 | return
228 |
229 | # 방향 키로 상하좌우 이동
230 | x = pl_x
231 | y = pl_y
232 | if key[K_UP] == 1:
233 | pl_d = 0
234 | if dungeon[pl_y - 1][pl_x] != 9:
235 | pl_y = pl_y - 1
236 | if key[K_DOWN] == 1:
237 | pl_d = 1
238 | if dungeon[pl_y + 1][pl_x] != 9:
239 | pl_y = pl_y + 1
240 | if key[K_LEFT] == 1:
241 | pl_d = 2
242 | if dungeon[pl_y][pl_x - 1] != 9:
243 | pl_x = pl_x - 1
244 | if key[K_RIGHT] == 1:
245 | pl_d = 3
246 | if dungeon[pl_y][pl_x + 1] != 9:
247 | pl_x = pl_x + 1
248 | pl_a = pl_d * 2
249 | if pl_x != x or pl_y != y: # 이동 시 식량 및 체력 계산
250 | pl_a = pl_a + tmr % 2 # 이동 시 걷기 애니메이션
251 | if food > 0:
252 | food = food - 1
253 | if pl_life < pl_lifemax:
254 | pl_life = pl_life + 1
255 | else:
256 | pl_life = pl_life - 5
257 | if pl_life <= 0:
258 | pl_life = 0
259 | pygame.mixer.music.stop()
260 | idx = 9
261 | tmr = 0
262 |
263 | def draw_text(bg, txt, x, y, fnt, col): # 그림자 포함한 문자 표시
264 | sur = fnt.render(txt, True, BLACK)
265 | bg.blit(sur, [x + 1, y + 2])
266 | sur = fnt.render(txt, True, col)
267 | bg.blit(sur, [x, y])
268 |
269 | def draw_para(bg, fnt): # 주인공 능력 표시
270 | X = 30
271 | Y = 600
272 | bg.blit(imgPara, [X, Y])
273 | col = WHITE
274 | if pl_life < 10 and tmr % 2 == 0: col = RED
275 | draw_text(bg, "{}/{}".format(pl_life, pl_lifemax), X + 128, Y + 6, fnt, col)
276 | draw_text(bg, str(pl_str), X + 128, Y + 33, fnt, WHITE)
277 | col = WHITE
278 | if food == 0 and tmr % 2 == 0: col = RED
279 | draw_text(bg, str(food), X + 128, Y + 60, fnt, col)
280 | draw_text(bg, str(potion), X + 266, Y + 6, fnt, WHITE)
281 | draw_text(bg, str(blazegem), X + 266, Y + 33, fnt, WHITE)
282 |
283 | def init_battle(): # 전투 시작 준비
284 | global imgEnemy, emy_name, emy_lifemax, emy_life, emy_str, emy_x, emy_y
285 | typ = random.randint(0, floor)
286 | if floor >= 10:
287 | typ = random.randint(0, 9)
288 | lev = random.randint(1, floor)
289 | imgEnemy = pygame.image.load("image/enemy" + str(typ) + ".png")
290 | emy_name = EMY_NAME[typ] + " LV" + str(lev)
291 | emy_lifemax = 60 * (typ + 1) + (lev - 1) * 10
292 | emy_life = emy_lifemax
293 | emy_str = int(emy_lifemax / 8)
294 | emy_x = 440 - imgEnemy.get_width() / 2
295 | emy_y = 560 - imgEnemy.get_height()
296 |
297 | def draw_bar(bg, x, y, w, h, val, ma): # 적 체력 표시 게이지
298 | pygame.draw.rect(bg, WHITE, [x - 2, y - 2, w + 4, h + 4])
299 | pygame.draw.rect(bg, BLACK, [x, y, w, h])
300 | if val > 0:
301 | pygame.draw.rect(bg, (0, 128, 255), [x, y, w * val / ma, h])
302 |
303 | def draw_battle(bg, fnt): # 전투 화면 표시
304 | global emy_blink, dmg_eff
305 | bx = 0
306 | by = 0
307 | if dmg_eff > 0:
308 | dmg_eff = dmg_eff - 1
309 | bx = random.randint(-20, 20)
310 | by = random.randint(-10, 10)
311 | bg.blit(imgBtlBG, [bx, by])
312 | if emy_life > 0 and emy_blink % 2 == 0:
313 | bg.blit(imgEnemy, [emy_x, emy_y + emy_step])
314 | draw_bar(bg, 340, 580, 200, 10, emy_life, emy_lifemax)
315 | if emy_blink > 0:
316 | emy_blink = emy_blink - 1
317 | for i in range(10): # 전투 메시지 표시
318 | draw_text(bg, message[i], 600, 100 + i * 50, fnt, WHITE)
319 | draw_para(bg, fnt) # 주인공 능력 표시
320 |
321 | def battle_command(bg, fnt, key): # 커맨드 입력 및 표시
322 | global btl_cmd
323 | ent = False
324 | if key[K_a]: # A 키
325 | btl_cmd = 0
326 | ent = True
327 | if key[K_p]: # P 키
328 | btl_cmd = 1
329 | ent = True
330 | if key[K_b]: # B 키
331 | btl_cmd = 2
332 | ent = True
333 | if key[K_r]: # R 키
334 | btl_cmd = 3
335 | ent = True
336 | if key[K_UP] and btl_cmd > 0: # ↑ 키
337 | btl_cmd -= 1
338 | if key[K_DOWN] and btl_cmd < 3: # ↓ 키
339 | btl_cmd += 1
340 | if key[K_SPACE] or key[K_RETURN]:
341 | ent = True
342 | for i in range(4):
343 | c = WHITE
344 | if btl_cmd == i: c = BLINK[tmr % 6]
345 | draw_text(bg, COMMAND[i], 20, 360 + i * 60, fnt, c)
346 | return ent
347 |
348 | # 전투 메시지 표시 처리
349 | message = [""] * 10
350 | def init_message():
351 | for i in range(10):
352 | message[i] = ""
353 |
354 | def set_message(msg):
355 | for i in range(10):
356 | if message[i] == "":
357 | message[i] = msg
358 | return
359 | for i in range(9):
360 | message[i] = message[i + 1]
361 | message[9] = msg
362 |
363 | def main(): # 메인 처리
364 | global speed, idx, tmr, floor, fl_max, welcome
365 | global pl_a, pl_lifemax, pl_life, pl_str, food, potion, blazegem
366 | global emy_life, emy_step, emy_blink, dmg_eff
367 | dmg = 0
368 | lif_p = 0
369 | str_p = 0
370 |
371 | pygame.init()
372 | pygame.display.set_caption("One hour Dungeon")
373 | screen = pygame.display.set_mode((880, 720))
374 | clock = pygame.time.Clock()
375 | font = pygame.font.Font(None, 40)
376 | fontS = pygame.font.Font(None, 30)
377 |
378 | se = [ # 효과음 및 징글
379 | pygame.mixer.Sound("sound/ohd_se_attack.ogg"),
380 | pygame.mixer.Sound("sound/ohd_se_blaze.ogg"),
381 | pygame.mixer.Sound("sound/ohd_se_potion.ogg"),
382 | pygame.mixer.Sound("sound/ohd_jin_gameover.ogg"),
383 | pygame.mixer.Sound("sound/ohd_jin_levup.ogg"),
384 | pygame.mixer.Sound("sound/ohd_jin_win.ogg")
385 | ]
386 |
387 | while True:
388 | for event in pygame.event.get():
389 | if event.type == QUIT:
390 | pygame.quit()
391 | sys.exit()
392 | if event.type == KEYDOWN:
393 | if event.key == K_s:
394 | speed = speed + 1
395 | if speed == 4:
396 | speed = 1
397 |
398 | tmr = tmr + 1
399 | key = pygame.key.get_pressed()
400 |
401 | if idx == 0: # 타이틀 화면
402 | if tmr == 1:
403 | pygame.mixer.music.load("sound/ohd_bgm_title.ogg")
404 | pygame.mixer.music.play(-1)
405 | screen.fill(BLACK)
406 | screen.blit(imgTitle, [40, 60])
407 | if fl_max >= 2:
408 | draw_text(screen, "You reached floor {}.".format(fl_max), 300, 460, font, CYAN)
409 | draw_text(screen, "Press Space Key", 320, 560, font, BLINK[tmr % 6])
410 | if key[K_SPACE] == 1:
411 | make_dungeon()
412 | put_event()
413 | floor = 1
414 | welcome = 15
415 | pl_lifemax = 300
416 | pl_life = pl_lifemax
417 | pl_str = 100
418 | food = 300
419 | potion = 0
420 | blazegem = 0
421 | idx = 1
422 | pygame.mixer.music.load("sound/ohd_bgm_field.ogg")
423 | pygame.mixer.music.play(-1)
424 |
425 | elif idx == 1: # 플레이어 이동
426 | move_player(key)
427 | draw_dungeon(screen, fontS)
428 | draw_text(screen, "floor {} ({},{})".format(floor, pl_x, pl_y), 60, 40, fontS, WHITE)
429 | if welcome > 0:
430 | welcome = welcome - 1
431 | draw_text(screen, "Welcome to floor {}.".format(floor), 300, 180, font, CYAN)
432 |
433 | elif idx == 2: # 화면 전환
434 | draw_dungeon(screen, fontS)
435 | if 1 <= tmr and tmr <= 5:
436 | h = 80 * tmr
437 | pygame.draw.rect(screen, BLACK, [0, 0, 880, h])
438 | pygame.draw.rect(screen, BLACK, [0, 720 - h, 880, h])
439 | if tmr == 5:
440 | floor = floor + 1
441 | if floor > fl_max:
442 | fl_max = floor
443 | welcome = 15
444 | make_dungeon()
445 | put_event()
446 | if 6 <= tmr and tmr <= 9:
447 | h = 80 * (10 - tmr)
448 | pygame.draw.rect(screen, BLACK, [0, 0, 880, h])
449 | pygame.draw.rect(screen, BLACK, [0, 720 - h, 880, h])
450 | if tmr == 10:
451 | idx = 1
452 |
453 | elif idx == 3: # 아이템 입수 혹은 함정
454 | draw_dungeon(screen, fontS)
455 | screen.blit(imgItem[treasure], [320, 220])
456 | draw_text(screen, TRE_NAME[treasure], 380, 240, font, WHITE)
457 | if tmr == 10:
458 | idx = 1
459 |
460 | elif idx == 9: # 게임 오버
461 | if tmr <= 30:
462 | PL_TURN = [2, 4, 0, 6]
463 | pl_a = PL_TURN[tmr % 4]
464 | if tmr == 30: pl_a = 8 # 쓰러진 그림
465 | draw_dungeon(screen, fontS)
466 | elif tmr == 31:
467 | se[3].play()
468 | draw_text(screen, "You died.", 360, 240, font, RED)
469 | draw_text(screen, "Game over.", 360, 380, font, RED)
470 | elif tmr == 100:
471 | idx = 0
472 | tmr = 0
473 |
474 | elif idx == 10: # 전투 시작
475 | if tmr == 1:
476 | pygame.mixer.music.load("sound/ohd_bgm_battle.ogg")
477 | pygame.mixer.music.play(-1)
478 | init_battle()
479 | init_message()
480 | elif tmr <= 4:
481 | bx = (4 - tmr) * 220
482 | by = 0
483 | screen.blit(imgBtlBG, [bx, by])
484 | draw_text(screen, "Encounter!", 350, 200, font, WHITE)
485 | elif tmr <= 16:
486 | draw_battle(screen, fontS)
487 | draw_text(screen, emy_name + " appear!", 300, 200, font, WHITE)
488 | else:
489 | idx = 11
490 | tmr = 0
491 |
492 | elif idx == 11: # 플레이어 턴(입력 대기)
493 | draw_battle(screen, fontS)
494 | if tmr == 1: set_message("Your turn.")
495 | if battle_command(screen, font, key) == True:
496 | if btl_cmd == 0:
497 | idx = 12
498 | tmr = 0
499 | if btl_cmd == 1 and potion > 0:
500 | idx = 20
501 | tmr = 0
502 | if btl_cmd == 2 and blazegem > 0:
503 | idx = 21
504 | tmr = 0
505 | if btl_cmd == 3:
506 | idx = 14
507 | tmr = 0
508 |
509 | elif idx == 12: # 플레이어 공격
510 | draw_battle(screen, fontS)
511 | if tmr == 1:
512 | set_message("You attack!")
513 | se[0].play()
514 | dmg = pl_str + random.randint(0, 9)
515 | if 2 <= tmr and tmr <= 4:
516 | screen.blit(imgEffect[0], [700 - tmr * 120, -100 + tmr * 120])
517 | if tmr == 5:
518 | emy_blink = 5
519 | set_message(str(dmg) + "pts of damage!")
520 | if tmr == 11:
521 | emy_life = emy_life - dmg
522 | if emy_life <= 0:
523 | emy_life = 0
524 | idx = 16
525 | tmr = 0
526 | if tmr == 16:
527 | idx = 13
528 | tmr = 0
529 |
530 | elif idx == 13: # 적 턴, 적 공격
531 | draw_battle(screen, fontS)
532 | if tmr == 1:
533 | set_message("Enemy turn.")
534 | if tmr == 5:
535 | set_message(emy_name + " attack!")
536 | se[0].play()
537 | emy_step = 30
538 | if tmr == 9:
539 | dmg = emy_str + random.randint(0, 9)
540 | set_message(str(dmg) + "pts of damage!")
541 | dmg_eff = 5
542 | emy_step = 0
543 | if tmr == 15:
544 | pl_life = pl_life - dmg
545 | if pl_life < 0:
546 | pl_life = 0
547 | idx = 15
548 | tmr = 0
549 | if tmr == 20:
550 | idx = 11
551 | tmr = 0
552 |
553 | elif idx == 14: # 도망 가능한가?
554 | draw_battle(screen, fontS)
555 | if tmr == 1: set_message("...")
556 | if tmr == 2: set_message("......")
557 | if tmr == 3: set_message(".........")
558 | if tmr == 4: set_message("............")
559 | if tmr == 5:
560 | if random.randint(0, 99) < 60:
561 | idx = 22
562 | else:
563 | set_message("You failed to flee.")
564 | if tmr == 10:
565 | idx = 13
566 | tmr = 0
567 |
568 | elif idx == 15: # 패배
569 | draw_battle(screen, fontS)
570 | if tmr == 1:
571 | pygame.mixer.music.stop()
572 | set_message("You lose.")
573 | if tmr == 11:
574 | idx = 9
575 | tmr = 29
576 |
577 | elif idx == 16: # 승리
578 | draw_battle(screen, fontS)
579 | if tmr == 1:
580 | set_message("You win!")
581 | pygame.mixer.music.stop()
582 | se[5].play()
583 | if tmr == 28:
584 | idx = 22
585 | if random.randint(0, emy_lifemax) > random.randint(0, pl_lifemax):
586 | idx = 17
587 | tmr = 0
588 |
589 | elif idx == 17: # 레벨업
590 | draw_battle(screen, fontS)
591 | if tmr == 1:
592 | set_message("Level up!")
593 | se[4].play()
594 | lif_p = random.randint(10, 20)
595 | str_p = random.randint(5, 10)
596 | if tmr == 21:
597 | set_message("Max life + " + str(lif_p))
598 | pl_lifemax = pl_lifemax + lif_p
599 | if tmr == 26:
600 | set_message("Str + " + str(str_p))
601 | pl_str = pl_str + str_p
602 | if tmr == 50:
603 | idx = 22
604 |
605 | elif idx == 20: # Potion
606 | draw_battle(screen, fontS)
607 | if tmr == 1:
608 | set_message("Potion!")
609 | se[2].play()
610 | if tmr == 6:
611 | pl_life = pl_lifemax
612 | potion = potion - 1
613 | if tmr == 11:
614 | idx = 13
615 | tmr = 0
616 |
617 | elif idx == 21: # Blaze gem
618 | draw_battle(screen, fontS)
619 | img_rz = pygame.transform.rotozoom(imgEffect[1], 30 * tmr, (12 - tmr) / 8)
620 | X = 440 - img_rz.get_width() / 2
621 | Y = 360 - img_rz.get_height() / 2
622 | screen.blit(img_rz, [X, Y])
623 | if tmr == 1:
624 | set_message("Blaze gem!")
625 | se[1].play()
626 | if tmr == 6:
627 | blazegem = blazegem - 1
628 | if tmr == 11:
629 | dmg = 1000
630 | idx = 12
631 | tmr = 4
632 |
633 | elif idx == 22: # 전투 종료
634 | pygame.mixer.music.load("sound/ohd_bgm_field.ogg")
635 | pygame.mixer.music.play(-1)
636 | idx = 1
637 |
638 | draw_text(screen, "[S]peed " + str(speed), 740, 40, fontS, WHITE)
639 |
640 | pygame.display.update()
641 | clock.tick(4 + 2 * speed)
642 |
643 | if __name__ == '__main__':
644 | main()
645 |
--------------------------------------------------------------------------------
/Chapter12/sound/ohd_bgm_battle.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/sound/ohd_bgm_battle.ogg
--------------------------------------------------------------------------------
/Chapter12/sound/ohd_bgm_field.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/sound/ohd_bgm_field.ogg
--------------------------------------------------------------------------------
/Chapter12/sound/ohd_bgm_title.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/sound/ohd_bgm_title.ogg
--------------------------------------------------------------------------------
/Chapter12/sound/ohd_jin_gameover.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/sound/ohd_jin_gameover.ogg
--------------------------------------------------------------------------------
/Chapter12/sound/ohd_jin_levup.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/sound/ohd_jin_levup.ogg
--------------------------------------------------------------------------------
/Chapter12/sound/ohd_jin_win.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/sound/ohd_jin_win.ogg
--------------------------------------------------------------------------------
/Chapter12/sound/ohd_se_attack.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/sound/ohd_se_attack.ogg
--------------------------------------------------------------------------------
/Chapter12/sound/ohd_se_blaze.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/sound/ohd_se_blaze.ogg
--------------------------------------------------------------------------------
/Chapter12/sound/ohd_se_potion.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter12/sound/ohd_se_potion.ogg
--------------------------------------------------------------------------------
/Chapter12/test.txt:
--------------------------------------------------------------------------------
1 | line 0
2 | line 1
3 | line 2
4 | line 3
5 | line 4
6 | line 5
7 | line 6
8 | line 7
9 | line 8
10 | line 9
11 |
--------------------------------------------------------------------------------
/Chapter12/test2.txt:
--------------------------------------------------------------------------------
1 | 파이썬으로
2 | 만드는
3 | 게임 개발
4 |
--------------------------------------------------------------------------------
/Chapter13/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter13/.DS_Store
--------------------------------------------------------------------------------
/Chapter13/list1302_1.py:
--------------------------------------------------------------------------------
1 | class GameCharacter:
2 | def __init__(self, job, life):
3 | self.job = job
4 | self.life = life
5 |
--------------------------------------------------------------------------------
/Chapter13/list1302_2.py:
--------------------------------------------------------------------------------
1 | class GameCharacter:
2 | def __init__(self, job, life):
3 | self.job = job
4 | self.life = life
5 |
6 | warrior = GameCharacter("전사", 100)
7 | print(warrior.job)
8 | print(warrior.life)
9 |
--------------------------------------------------------------------------------
/Chapter13/list1302_3.py:
--------------------------------------------------------------------------------
1 | class GameCharacter:
2 | def __init__(self, job, life):
3 | self.job = job
4 | self.life = life
5 |
6 | def info(self):
7 | print(self.job)
8 | print(self.life)
9 |
10 | warrior = GameCharacter("전사", 100)
11 | warrior.info()
12 |
--------------------------------------------------------------------------------
/Chapter13/list1302_4.py:
--------------------------------------------------------------------------------
1 | class GameCharacter:
2 | def __init__(self, job, life):
3 | self.job = job
4 | self.life = life
5 |
6 | def info(self):
7 | print(self.job)
8 | print(self.life)
9 |
10 | human1 = GameCharacter("전사", 120)
11 | human1.info()
12 |
13 | human2 = GameCharacter("마법사", 80)
14 | human2.info()
15 |
--------------------------------------------------------------------------------
/Chapter13/list1303_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | FNT = ("Times New Roman", 30)
3 |
4 | class GameCharacter:
5 | def __init__(self, job, life, imgfile):
6 | self.job = job
7 | self.life = lifew
8 | self.img = tkinter.PhotoImage(file=imgfile)
9 |
10 | def draw(self):
11 | canvas.create_image(200, 280, image=self.img)
12 | canvas.create_text(300, 400, text=self.job, font=FNT, fill="red")
13 | canvas.create_text(300, 480, text=self.life, font=FNT, fill="blue")
14 |
15 | root = tkinter.Tk()
16 | root.title("tkinter를 사용한 객체 지향 프로그래밍")
17 | canvas = tkinter.Canvas(root, width=400, height=560, bg="white")
18 | canvas.pack()
19 |
20 | character = GameCharacter("검사", 200, "swordsman.png")
21 | character.draw()
22 |
23 | root.mainloop()
24 |
--------------------------------------------------------------------------------
/Chapter13/list1303_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | FNT = ("Times New Roman", 30)
3 |
4 | class GameCharacter:
5 | def __init__(self, job, life, imgfile):
6 | self.job = job
7 | self.life = life
8 | self.img = tkinter.PhotoImage(file=imgfile)
9 |
10 | def draw(self, x, y):
11 | canvas.create_image(x + 200, y + 280, image=self.img)
12 | canvas.create_text(x + 300, y + 400, text=self.job, font=FNT, fill="red")
13 | canvas.create_text(x + 300, y + 480, text=self.life, font=FNT, fill="blue")
14 |
15 | root = tkinter.Tk()
16 | root.title("tkinter를 사용한 객체 지향 프로그래밍")
17 | canvas = tkinter.Canvas(root, width=800, height=560, bg="white")
18 | canvas.pack()
19 |
20 | character = [
21 | GameCharacter("검사", 200, "swordsman.png"),
22 | GameCharacter("닌자", 160, "ninja.png")
23 | ]
24 | character[0].draw(0, 0)
25 | character[1].draw(400, 0)
26 |
27 | root.mainloop()
28 |
--------------------------------------------------------------------------------
/Chapter13/list1303_3.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import time
3 | FNT = ("Times New Roman", 24)
4 |
5 | class GameCharacter:
6 | def __init__(self, name, life, x, y, imgfile, tagname):
7 | self.name = name
8 | self.life = life
9 | self.lmax = life
10 | self.x = x
11 | self.y = y
12 | self.img = tkinter.PhotoImage(file=imgfile)
13 | self.tagname = tagname
14 |
15 | def draw(self):
16 | x = self.x
17 | y = self.y
18 | canvas.create_image(x, y, image=self.img, tag=self.tagname)
19 | canvas.create_text(x, y + 120, text=self.name, font=FNT, fill="red", tag=self.tagname)
20 | canvas.create_text(x, y + 200, text="life{}/{}".format(self.life, self.lmax), font=FNT, fill="lime",
21 | tag=self.tagname)
22 |
23 | def attack(self):
24 | di = 1
25 | if self.x >= 400:
26 | di = -1
27 | for i in range(5): # 공격 동작(옆으로 움직임)
28 | canvas.coords(self.tagname, self.x + i * 10 * di, self.y)
29 | canvas.update()
30 | time.sleep(0.1)
31 | canvas.coords(self.tagname, self.x, self.y)
32 |
33 | def damage(self):
34 | for i in range(5): # 데미지(화면 깜빡임)
35 | self.draw()
36 | canvas.update()
37 | time.sleep(0.1)
38 | canvas.delete(self.tagname)
39 | canvas.update()
40 | time.sleep(0.1)
41 | self.life = self.life - 30
42 | if self.life > 0:
43 | self.draw()
44 | else:
45 | print(self.name + "은 쓰러졌다... ")
46 |
47 | def click_left():
48 | character[0].attack()
49 | character[1].damage()
50 |
51 | def click_right():
52 | character[1].attack()
53 | character[0].damage()
54 |
55 | root = tkinter.Tk()
56 | root.title("객체 지향을 활용한 전투")
57 | canvas = tkinter.Canvas(root, width=800, height=600, bg="white")
58 | canvas.pack()
59 |
60 | btn_left = tkinter.Button(text="공격 →", command=click_left)
61 | btn_left.place(x=160, y=560)
62 | btn_right = tkinter.Button(text="← 공격", command=click_right)
63 | btn_right.place(x=560, y=560)
64 |
65 | character = [
66 | GameCharacter("태양의 검사 「가이아」", 200, 200, 280, "swordsman.png", "LC"),
67 | GameCharacter("어둠의 닌자 「한조」", 160, 600, 280, "ninja.png", "RC")
68 | ]
69 | character[0].draw()
70 | character[1].draw()
71 |
72 | root.mainloop()
73 |
--------------------------------------------------------------------------------
/Chapter13/list1304_1.py:
--------------------------------------------------------------------------------
1 | class Human:
2 | def __init__(self, name, life):
3 | self.name = name
4 | self.life = life
5 |
6 | def info(self):
7 | print(self.name)
8 | print(self.life)
9 |
10 |
11 | class Soldier(Human):
12 | def __init__(self, name, life, weapon):
13 | super().__init__(name, life)
14 | self.weapon = weapon
15 |
16 | def info(self):
17 | print("내 이름은 " + self.name)
18 | print("내 체력은 {}".format(self.life))
19 |
20 | def talk(self):
21 | print(self.weapon + "을(를) 장비하고, 모험을 시작합니다.")
22 |
23 |
24 | man = Human("톰(일반인)", 50)
25 | man.info()
26 | print("----------")
27 | prince = Soldier("알렉스(왕자)", 200, "빛의 검")
28 | prince.info()
29 | prince.talk()
30 |
--------------------------------------------------------------------------------
/Chapter13/ninja.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter13/ninja.png
--------------------------------------------------------------------------------
/Chapter13/swordsman.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter13/swordsman.png
--------------------------------------------------------------------------------
/Chapter3/list0301_1.py:
--------------------------------------------------------------------------------
1 | score = 0
2 | print(score)
3 | score = score + 100
4 | print(score)
5 |
--------------------------------------------------------------------------------
/Chapter3/list0301_2.py:
--------------------------------------------------------------------------------
1 | job = "초보 검사"
2 | print("당신의 직업은 " + job)
3 | print("클래스를 변경했다!")
4 | job = "신출내기 용사"
5 | print("새로운 직업은 " + job)
6 |
--------------------------------------------------------------------------------
/Chapter3/list0302_1.py:
--------------------------------------------------------------------------------
1 | enemy = ["슬라임", "해골 병사", "마법사"]
2 | print(enemy[0])
3 | print(enemy[1])
4 | print(enemy[2])
5 |
--------------------------------------------------------------------------------
/Chapter3/list0303_1.py:
--------------------------------------------------------------------------------
1 | life = 0
2 | if life <= 0:
3 | print("게임 오버입니다")
4 | if life > 0:
5 | print("게임을 계속합니다")
6 |
--------------------------------------------------------------------------------
/Chapter3/list0303_2.py:
--------------------------------------------------------------------------------
1 | gold = 100
2 | if gold == 0:
3 | print("소지금이 없습니다")
4 | else:
5 | print("구입을 계속하시겠습니까?")
6 |
--------------------------------------------------------------------------------
/Chapter3/list0304_1.py:
--------------------------------------------------------------------------------
1 | for i in range(10):
2 | print(i)
3 |
--------------------------------------------------------------------------------
/Chapter3/list0304_2.py:
--------------------------------------------------------------------------------
1 | for i in range(1, 5):
2 | print(i)
3 |
--------------------------------------------------------------------------------
/Chapter3/list0304_3.py:
--------------------------------------------------------------------------------
1 | for i in range(10, 0, -2):
2 | print(i)
3 |
--------------------------------------------------------------------------------
/Chapter3/list0304_4.py:
--------------------------------------------------------------------------------
1 | i = 0
2 | while i < 5:
3 | print(i)
4 | i = i + 1
5 |
--------------------------------------------------------------------------------
/Chapter3/list0305_1.py:
--------------------------------------------------------------------------------
1 | def win():
2 | print("당신이 승리했습니다!")
3 |
4 |
5 | win()
6 |
--------------------------------------------------------------------------------
/Chapter3/list0305_2.py:
--------------------------------------------------------------------------------
1 | def recover(val):
2 | print("당신의 체력은 ")
3 | print(val)
4 | print("회복했다!")
5 |
6 | recover(100)
7 |
--------------------------------------------------------------------------------
/Chapter3/list0305_3.py:
--------------------------------------------------------------------------------
1 | def add(a, b):
2 | return a + b
3 |
4 | c = add(1, 2)
5 | print(c)
6 |
--------------------------------------------------------------------------------
/Chapter4/list0402_1.py:
--------------------------------------------------------------------------------
1 | import calendar
2 |
3 | print(calendar.month(2020, 2))
4 |
--------------------------------------------------------------------------------
/Chapter4/list0402_2.py:
--------------------------------------------------------------------------------
1 | import calendar
2 |
3 | print(calendar.isleap(2020))
4 |
--------------------------------------------------------------------------------
/Chapter4/list0403_1.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 | print(datetime.date.today())
4 |
--------------------------------------------------------------------------------
/Chapter4/list0403_2.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 | print(datetime.datetime.now())
4 |
--------------------------------------------------------------------------------
/Chapter4/list0403_3.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 | d = datetime.datetime.now()
4 | print(d.hour)
5 | print(d.minute)
6 | print(d.second)
7 |
--------------------------------------------------------------------------------
/Chapter4/list0403_4.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 | today = datetime.date.today()
4 | birth = datetime.date(1971, 2, 2)
5 | print(today - birth)
6 |
--------------------------------------------------------------------------------
/Chapter4/list0404_1.py:
--------------------------------------------------------------------------------
1 | import random
2 |
3 | r = random.random()
4 | print(r)
5 |
--------------------------------------------------------------------------------
/Chapter4/list0404_2.py:
--------------------------------------------------------------------------------
1 | import random
2 |
3 | r = random.randint(1, 6)
4 | print(r)
5 |
--------------------------------------------------------------------------------
/Chapter4/list0404_3.py:
--------------------------------------------------------------------------------
1 | import random
2 |
3 | srp = random.choice(["가위", "바위", "보"])
4 | print(srp)
5 |
--------------------------------------------------------------------------------
/Chapter4/list0404_4.py:
--------------------------------------------------------------------------------
1 | import random
2 |
3 | cnt = 0
4 | while True:
5 | r = random.randint(1, 100)
6 | print(r)
7 | cnt = cnt + 1
8 | if r == 77:
9 | break
10 | print(str(cnt) + "번째에 희귀 캐릭터 겟!")
11 |
--------------------------------------------------------------------------------
/Chapter5/list0502_1.py:
--------------------------------------------------------------------------------
1 | print("유경자 씨의 남편의 이름은?")
2 | ans = input()
3 | if ans == "정현철":
4 | print("정답입니다")
5 | else:
6 | print("틀렸습니다")
7 |
--------------------------------------------------------------------------------
/Chapter5/list0502_2.py:
--------------------------------------------------------------------------------
1 | print("유경자 씨의 남편의 이름은?")
2 | ans = input()
3 | if ans == "정현철" or ans == "Hyunchul Jung":
4 | print("정답입니다")
5 | else:
6 | print("틀렸습니다")
7 |
--------------------------------------------------------------------------------
/Chapter5/list0502_3.py:
--------------------------------------------------------------------------------
1 | QUESTION = [
2 | "유경자 씨의 남편의 이름은?",
3 | "이경수 씨의 딸의 이름은?",
4 | "민주희 씨는 이경수 씨와 어떤 관계입니까?"]
5 | R_ANS = ["정현철", "이현지", "조카"]
6 | for i in range(3):
7 | print(QUESTION[i])
8 | ans = input()
9 | if ans == R_ANS[i]:
10 | print("정답입니다")
11 | else:
12 | print("틀렸습니다")
13 |
--------------------------------------------------------------------------------
/Chapter5/list0502_4.py:
--------------------------------------------------------------------------------
1 | QUESTION = [
2 | "유경자 씨의 남편의 이름은?",
3 | "이경수의 딸의 이름은?",
4 | "민주희 씨는 이경수 씨와 어떤 관계입니까?"]
5 | R_ANS = ["정현철", "이현지", "조카"]
6 | R_ANS2 = ["Hyunchul Jung", "Hyunji Lee", "niece"]
7 |
8 | for i in range(3):
9 | print(QUESTION[i])
10 | ans = input()
11 | if ans == R_ANS[i] or ans == R_ANS2[i]:
12 | print("정답입니다")
13 | else:
14 | print("틀렸습니다")
15 |
--------------------------------------------------------------------------------
/Chapter5/list0503_1.py:
--------------------------------------------------------------------------------
1 | pl_pos = 6
2 | def board():
3 | print("•" * (pl_pos - 1) + "P" + "•" * (30 - pl_pos))
4 |
5 | board()
6 |
--------------------------------------------------------------------------------
/Chapter5/list0503_2.py:
--------------------------------------------------------------------------------
1 | pl_pos = 6
2 | com_pos = 3
3 | def board():
4 | print("•" * (pl_pos - 1) + "P" + "•" * (30 - pl_pos))
5 | print("•" * (com_pos - 1) + "C" + "•" * (30 - com_pos))
6 | board()
7 |
--------------------------------------------------------------------------------
/Chapter5/list0503_3.py:
--------------------------------------------------------------------------------
1 | pl_pos = 1
2 | com_pos = 1
3 | def board():
4 | print("•" * (pl_pos - 1) + "P" + "•" * (30 - pl_pos))
5 | print("•" * (com_pos - 1) + "C" + "•" * (30 - com_pos))
6 | while True:
7 | board()
8 | input("Enter를 누르면 말이 움직입니다")
9 | pl_pos = pl_pos + 1
10 | com_pos = com_pos + 2
11 |
--------------------------------------------------------------------------------
/Chapter5/list0503_4.py:
--------------------------------------------------------------------------------
1 | import random
2 | pl_pos = 1
3 | com_pos = 1
4 | def board():
5 | print("•" * (pl_pos - 1) + "P" + "•" * (30 - pl_pos))
6 | print("•" * (com_pos - 1) + "C" + "•" * (30 - com_pos))
7 | while True:
8 | board()
9 | input("Enter를 누르면 말이 움직입니다")
10 | pl_pos = pl_pos + random.randint(1, 6)
11 | com_pos = com_pos + random.randint(1, 6)
12 |
--------------------------------------------------------------------------------
/Chapter5/list0503_5.py:
--------------------------------------------------------------------------------
1 | import random
2 | pl_pos = 1
3 | com_pos = 1
4 | def board():
5 | print("•" * (pl_pos - 1) + "P" + "•" * (30 - pl_pos) + "Goal")
6 | print("•" * (com_pos - 1) + "C" + "•" * (30 - com_pos) + "Goal")
7 |
8 | board()
9 | print("주사위 게임, 스타트!")
10 | while True:
11 | input("Enter를 누르면 여러분의 말이 움직입니다")
12 | pl_pos = pl_pos + random.randint(1, 6)
13 | if pl_pos > 30:
14 | pl_pos = 30
15 | board()
16 | if pl_pos == 30:
17 | print("당신이 승리했습니다!")
18 | break
19 | input("Enter를 누르면 컴퓨터의 말이 움직입니다")
20 | com_pos = com_pos + random.randint(1, 6)
21 | if com_pos > 30:
22 | com_pos = 30
23 | board()
24 | if com_pos == 30:
25 | print("컴퓨터가 승리했습니다!")
26 | break
27 |
--------------------------------------------------------------------------------
/Chapter5/list0504_1.py:
--------------------------------------------------------------------------------
1 | ALP = ["A", "B", "C", "D", "E", "F", "G"]
2 | for i in ALP:
3 | print(i)
4 |
--------------------------------------------------------------------------------
/Chapter5/list0504_2.py:
--------------------------------------------------------------------------------
1 | import random
2 | ALP = ["A", "B", "C", "D", "E", "F", "G"]
3 | r = random.choice(ALP)
4 | alp = ""
5 | for i in ALP:
6 | if i != r:
7 | alp = alp + i
8 | print(alp)
9 |
--------------------------------------------------------------------------------
/Chapter5/list0504_3.py:
--------------------------------------------------------------------------------
1 | import random
2 | ALP = ["A", "B", "C", "D", "E", "F", "G"]
3 | r = random.choice(ALP)
4 | alp = ""
5 | for i in ALP:
6 | if i != r:
7 | alp = alp + i
8 | print(alp)
9 | ans = input("빠진 알파벳은?")
10 | if ans == r:
11 | print("정답입니다")
12 | else:
13 | print("틀렸습니다")
14 |
--------------------------------------------------------------------------------
/Chapter5/list0504_4.py:
--------------------------------------------------------------------------------
1 | import random
2 | import datetime
3 | ALP = ["A", "B", "C", "D", "E", "F", "G"]
4 | r = random.choice(ALP)
5 | alp = ""
6 | for i in ALP:
7 | if i != r:
8 | alp = alp + i
9 | print(alp)
10 | st = datetime.datetime.now()
11 | ans = input("빠진 알파벳은?")
12 | if ans == r:
13 | print("정답입니다")
14 | et = datetime.datetime.now()
15 | print((et - st).seconds)
16 | else:
17 | print("틀렸습니다")
18 |
--------------------------------------------------------------------------------
/Chapter5/list0504_5.py:
--------------------------------------------------------------------------------
1 | import random
2 | import datetime
3 | ALP = [
4 | "A", "B", "C", "D", "E", "F", "G",
5 | "H", "I", "J", "K", "L", "M", "N",
6 | "O", "P", "Q", "R", "S", "T", "U",
7 | "V", "W", "X", "Y", "Z"
8 | ]
9 | r = random.choice(ALP)
10 | alp = ""
11 | for i in ALP:
12 | if i != r:
13 | alp = alp + i
14 | print(alp)
15 | st = datetime.datetime.now()
16 | ans = input("빠진 알파벳은?")
17 | if ans == r:
18 | print("정답입니다")
19 | et = datetime.datetime.now()
20 | print(str((et - st).seconds) + "초 걸렸습니다")
21 | else:
22 | print("틀렸습니다")
23 |
--------------------------------------------------------------------------------
/Chapter6/column06.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("캔버스에 도형 그리기")
4 | root.geometry("500x400")
5 | cvs = tkinter.Canvas(root, width=500, height=400, bg="white")
6 | cvs.pack()
7 | cvs.create_text(250, 25, text="문자열", fill="green", font=("Times New Roman", 24))
8 | cvs.create_line(30, 30, 70, 80, fill="navy", width=5)
9 | cvs.create_line(120, 20, 80, 50, 200, 80, 140, 120, fill="blue", smooth=True)
10 | cvs.create_rectangle(40, 140, 160, 200, fill="lime")
11 | cvs.create_rectangle(60, 240, 120, 360, fill="pink", outline="red", width=5)
12 | cvs.create_oval(250 - 40, 100 - 40, 250 + 40, 100 + 40, fill="silver", outline="purple")
13 | cvs.create_oval(250 - 80, 200 - 40, 250 + 80, 200 + 40, fill="cyan", width=0)
14 | cvs.create_polygon(250, 250, 150, 350, 350, 350, fill="magenta", width=0)
15 | cvs.create_arc(400 - 50, 100 - 50, 400 + 50, 100 + 50, fill="yellow", start=30, extent=300)
16 | cvs.create_arc(400 - 50, 250 - 50, 400 + 50, 250 + 50, fill="gold", start=0, extent=120, style=tkinter.CHORD)
17 | cvs.create_arc(400 - 50, 350 - 50, 400 + 50, 350 + 50, outline="orange", start=0, extent=120, style=tkinter.ARC)
18 | cvs.mainloop()
19 |
--------------------------------------------------------------------------------
/Chapter6/hyunju.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter6/hyunju.png
--------------------------------------------------------------------------------
/Chapter6/list0601_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.mainloop()
4 |
--------------------------------------------------------------------------------
/Chapter6/list0601_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("첫 번째 윈도우")
4 | root.geometry("800x600")
5 | root.mainloop()
6 |
--------------------------------------------------------------------------------
/Chapter6/list0602_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("첫 번째 라벨")
4 | root.geometry("800x600")
5 | label = tkinter.Label(root, text="라벨 문자열", font=("System", 24))
6 | label.place(x=200, y=100)
7 | root.mainloop()
8 |
--------------------------------------------------------------------------------
/Chapter6/list0602_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import tkinter.font
3 | root = tkinter.Tk()
4 | print(tkinter.font.families())
5 |
--------------------------------------------------------------------------------
/Chapter6/list0603_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("첫 번째 버튼")
4 | root.geometry("800x600")
5 | button = tkinter.Button(root, text="버튼 문자열", font=("Times New Roman", 24))
6 | button.place(x=200, y=100)
7 | root.mainloop()
8 |
--------------------------------------------------------------------------------
/Chapter6/list0603_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | def click_btn():
4 | button["text"] = "클릭했습니다"
5 |
6 | root = tkinter.Tk()
7 | root.title("첫 번째 버튼")
8 | root.geometry("800x600")
9 | button = tkinter.Button(root, text="클릭하십시오", font=("Times New Roman", 24), command=click_btn)
10 | button.place(x=200, y=100)
11 | root.mainloop()
12 |
--------------------------------------------------------------------------------
/Chapter6/list0604_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | root = tkinter.Tk()
4 | root.title("첫 번째 캔버스")
5 | canvas = tkinter.Canvas(root, width=400, height=600, bg="skyblue")
6 | canvas.pack()
7 | root.mainloop()
8 |
--------------------------------------------------------------------------------
/Chapter6/list0604_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | root = tkinter.Tk()
4 | root.title("첫번째 캔버스")
5 | canvas = tkinter.Canvas(root, width=400, height=600)
6 | canvas.pack()
7 | gazou = tkinter.PhotoImage(file="hyunju.png")
8 | canvas.create_image(200, 300, image=gazou)
9 | root.mainloop()
10 |
--------------------------------------------------------------------------------
/Chapter6/list0605_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("제비뽑기 프로그램")
4 | root.resizable(False, False)
5 | canvas = tkinter.Canvas(root, width=800, height=600)
6 | canvas.pack()
7 | gazou = tkinter.PhotoImage(file="miko.png")
8 | canvas.create_image(400, 300, image=gazou)
9 | root.mainloop()
10 |
--------------------------------------------------------------------------------
/Chapter6/list0605_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("제비뽑기 프로그램")
4 | root.resizable(False, False)
5 | canvas = tkinter.Canvas(root, width=800, height=600)
6 | canvas.pack()
7 | gazou = tkinter.PhotoImage(file="miko.png")
8 | canvas.create_image(400, 300, image=gazou)
9 | label = tkinter.Label(root, text="??", font=("Times New Roman", 120), bg="white")
10 | label.place(x=380, y=60)
11 | button = tkinter.Button(root, text="제비뽑기", font=("Times New Roman", 36), fg="skyblue")
12 | button.place(x=360, y=400)
13 | root.mainloop()
14 |
--------------------------------------------------------------------------------
/Chapter6/list0605_3.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import random
3 |
4 | def click_btn():
5 | label["text"] = random.choice(["대길", "중길", "소길", "흉"])
6 | label.update()
7 |
8 | root = tkinter.Tk()
9 | root.title("제비뽑기 프로그램")
10 | root.resizable(False, False)
11 | canvas = tkinter.Canvas(root, width=800, height=600)
12 | canvas.pack()
13 | gazou = tkinter.PhotoImage(file="miko.png")
14 | canvas.create_image(400, 300, image=gazou)
15 | label = tkinter.Label(root, text="??", font=("Times New Roman", 120), bg="white")
16 | label.place(x=380, y=60)
17 | button = tkinter.Button(root, text="제비뽑기", font=("Times New Roman", 36), command=click_btn, fg="skyblue")
18 | button.place(x=360, y=400)
19 | root.mainloop()
20 |
--------------------------------------------------------------------------------
/Chapter6/miko.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter6/miko.png
--------------------------------------------------------------------------------
/Chapter7/list0701_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("첫 번째 텍스트 입력 필드")
4 | root.geometry("400x200")
5 | entry = tkinter.Entry(width=20)
6 | entry.place(x=10, y=10)
7 | root.mainloop()
8 |
--------------------------------------------------------------------------------
/Chapter7/list0701_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | def click_btn():
4 | txt = entry.get()
5 | button["text"] = txt
6 |
7 | root = tkinter.Tk()
8 | root.title("첫 번째 테스트 입력 필드")
9 | root.geometry("400x200")
10 | entry = tkinter.Entry(width=20)
11 | entry.place(x=20, y=20)
12 | button = tkinter.Button(text="문자열 얻기", command=click_btn)
13 | button.place(x=20, y=100)
14 | root.mainloop()
15 |
--------------------------------------------------------------------------------
/Chapter7/list0702_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | def click_btn():
4 | text.insert(tkinter.END, "몬스터가 나타났다!")
5 |
6 | root = tkinter.Tk()
7 | root.title("여러 행 텍스트 입력")
8 | root.geometry("400x200")
9 | button = tkinter.Button(text="메시지", command=click_btn)
10 | button.pack()
11 | text = tkinter.Text()
12 | text.pack()
13 | root.mainloop()
14 |
--------------------------------------------------------------------------------
/Chapter7/list0703_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("체크 버튼 다루기")
4 | root.geometry("400x200")
5 | cbtn = tkinter.Checkbutton(text="체크 버튼")
6 | cbtn.pack()
7 | root.mainloop()
8 |
--------------------------------------------------------------------------------
/Chapter7/list0703_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("처음부터 체크된 상태 만들기")
4 | root.geometry("400x200")
5 | cval = tkinter.BooleanVar()
6 | cval.set(True)
7 | cbtn = tkinter.Checkbutton(text="체크 버튼", variable=cval)
8 | cbtn.pack()
9 | root.mainloop()
10 |
--------------------------------------------------------------------------------
/Chapter7/list0703_3.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | def check():
4 | if cval.get() == True:
5 | print("체크되어 있습니다")
6 | else:
7 | print("체크되어 있지 않습니다")
8 |
9 | root = tkinter.Tk()
10 | root.title("체크 상태 확인")
11 | root.geometry("400x200")
12 | cval = tkinter.BooleanVar()
13 | cval.set(False)
14 | cbtn = tkinter.Checkbutton(text="체크 버튼", variable=cval, command=check)
15 | cbtn.pack()
16 | root.mainloop()
17 |
--------------------------------------------------------------------------------
/Chapter7/list0704_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import tkinter.messagebox
3 |
4 | def click_btn():
5 | tkinter.messagebox.showinfo("정보", "버튼을 눌렀습니다")
6 |
7 | root = tkinter.Tk()
8 | root.title("첫 번째 메시지 박스")
9 | root.geometry("400x200")
10 | btn = tkinter.Button(text="테스트", command=click_btn)
11 | btn.pack()
12 | root.mainloop()
13 |
--------------------------------------------------------------------------------
/Chapter7/list0705_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | root = tkinter.Tk()
4 | root.title("고양이 지수 진단 게임")
5 | root.resizable(False, False)
6 | canvas = tkinter.Canvas(root, width=800, height=600)
7 | canvas.pack()
8 | gazou = tkinter.PhotoImage(file="mina.png")
9 | canvas.create_image(400, 300, image=gazou)
10 | button = tkinter.Button(text="진단하기", font=("Times New Roman", 32), bg="lightgreen")
11 | button.place(x=400, y=480)
12 | text = tkinter.Text(width=40, height=5, font=("Times New Roman", 16))
13 | text.place(x=320, y=30)
14 | root.mainloop()
15 |
--------------------------------------------------------------------------------
/Chapter7/list0705_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | root = tkinter.Tk()
4 | root.title("고양이 지수 판단 게임")
5 | root.resizable(False, False)
6 | canvas = tkinter.Canvas(root, width=800, height=600)
7 | canvas.pack()
8 | gazou = tkinter.PhotoImage(file="mina.png")
9 | canvas.create_image(400, 300, image=gazou)
10 | button = tkinter.Button(text="진단하기", font=("Times New Roman", 32), bg="lightgreen")
11 | button.place(x=400, y=480)
12 | text = tkinter.Text(width=40, height=5, font=("Times New Roman", 16))
13 | text.place(x=320, y=30)
14 |
15 | bvar = [None] * 7
16 | cbtn = [None] * 7
17 | ITEM = [
18 | "높은 곳이 좋다",
19 | "공을 보면 굴리고 싶어진다",
20 | "깜짝 놀라면 털이 곤두선다",
21 | "쥐구멍이 마음에 든다",
22 | "개에게 적대감을 느낀다",
23 | "생선 뼈를 발라 먹고 싶다",
24 | "밤, 기운이 난다"
25 | ]
26 | for i in range(7):
27 | bvar[i] = tkinter.BooleanVar()
28 | bvar[i].set(False)
29 | cbtn[i] = tkinter.Checkbutton(text=ITEM[i], font=("Times New Roman", 12), variable=bvar[i], bg="#dfe")
30 | cbtn[i].place(x=400, y=160 + 40 * i)
31 | root.mainloop()
32 |
--------------------------------------------------------------------------------
/Chapter7/list0705_3.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | def click_btn():
4 | pts = 0
5 | for i in range(7):
6 | if bvar[i].get() == True:
7 | pts = pts + 1
8 | text.delete("1.0", tkinter.END)
9 | text.insert("1.0", "체크된 수는 " + str(pts))
10 |
11 | root = tkinter.Tk()
12 | root.title("고양이 지수 진단 게임")
13 | root.resizable(False, False)
14 | canvas = tkinter.Canvas(root, width=800, height=600)
15 | canvas.pack()
16 | gazou = tkinter.PhotoImage(file="mina.png")
17 | canvas.create_image(400, 300, image=gazou)
18 | button = tkinter.Button(text="진단하기", font=("Times New Roman", 32), bg="lightgreen", command=click_btn)
19 | button.place(x=400, y=480)
20 | text = tkinter.Text(width=40, height=5, font=("Times New Roman", 16))
21 | text.place(x=320, y=30)
22 |
23 | bvar = [None] * 7
24 | cbtn = [None] * 7
25 | ITEM = [
26 | "높은 곳이 좋다",
27 | "공을 보면 굴리고 싶어진다",
28 | "깜짝 놀라면 털이 곤두선다",
29 | "쥐구멍이 마음에 든다",
30 | "개에게 적대감을 느낀다",
31 | "생선 뼈를 발라 먹고 싶다",
32 | "밤, 기운이 난다"
33 | ]
34 | for i in range(7):
35 | bvar[i] = tkinter.BooleanVar()
36 | bvar[i].set(False)
37 | cbtn[i] = tkinter.Checkbutton(text=ITEM[i], font=("Times New Roman", 12), variable=bvar[i], bg="#dfe")
38 | cbtn[i].place(x=400, y=160 + 40 * i)
39 | root.mainloop()
40 |
--------------------------------------------------------------------------------
/Chapter7/list0705_4.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | RESULT = [
4 | "전생에 고양이었을 가능성은 매우 낮습니다.",
5 | "보통 사람입니다.",
6 | "특별히 이상한 곳은 없습니다.",
7 | "꽤 고양이 다운 구석이 있습니다.",
8 | "고양이와 비슷한 성격 같습니다.",
9 | "고양이와 근접한 성격입니다.",
10 | "전생에 고양이었을지도 모릅니다.",
11 | "겉모습은 사람이지만, 속은 고양이일 가능성이 있습니다."
12 | ]
13 | def click_btn():
14 | pts = 0
15 | for i in range(7):
16 | if bvar[i].get() == True:
17 | pts = pts + 1
18 | nekodo = int(100 * pts / 7)
19 | text.delete("1.0", tkinter.END)
20 | text.insert("1.0", "<진단결과>\n당신의 고양이 지수는 " + str(nekodo) + "%입니다。\n" + RESULT[pts])
21 |
22 | root = tkinter.Tk()
23 | root.title("고양이 지수 판정 게임")
24 | root.resizable(False, False)
25 | canvas = tkinter.Canvas(root, width=800, height=600)
26 | canvas.pack()
27 | gazou = tkinter.PhotoImage(file="mina.png")
28 | canvas.create_image(400, 300, image=gazou)
29 | button = tkinter.Button(text="진단하기", font=("Times New Roman", 32), bg="lightgreen", command=click_btn)
30 | button.place(x=400, y=480)
31 | text = tkinter.Text(width=40, height=5, font=("Times New Roman", 16))
32 | text.place(x=320, y=30)
33 |
34 | bvar = [None] * 7
35 | cbtn = [None] * 7
36 | ITEM = [
37 | "높은 곳이 좋다",
38 | "공을 보면 굴리고 싶어진다",
39 | "깜짝 놀라면 털이 곤두선다",
40 | "쥐구멍이 마음에 든다",
41 | "개에게 적대감을 느낀다",
42 | "생선 뼈를 발라 먹고 싶다",
43 | "밤, 기운이 난다"
44 | ]
45 | for i in range(7):
46 | bvar[i] = tkinter.BooleanVar()
47 | bvar[i].set(False)
48 | cbtn[i] = tkinter.Checkbutton(text=ITEM[i], font=("Times New Roman", 12), variable=bvar[i], bg="#dfe")
49 | cbtn[i].place(x=400, y=160 + 40 * i)
50 | root.mainloop()
51 |
--------------------------------------------------------------------------------
/Chapter7/mina.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter7/mina.png
--------------------------------------------------------------------------------
/Chapter8/cat00.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter8/cat00.png
--------------------------------------------------------------------------------
/Chapter8/cat01.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter8/cat01.png
--------------------------------------------------------------------------------
/Chapter8/cat02.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter8/cat02.png
--------------------------------------------------------------------------------
/Chapter8/cat03.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter8/cat03.png
--------------------------------------------------------------------------------
/Chapter8/column08.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | pnum = 0
4 | def photograph():
5 | global pnum
6 | canvas.delete("PH")
7 | canvas.create_image(400, 300, image=photo[pnum], tag="PH")
8 | pnum = pnum + 1
9 | if pnum >= len(photo):
10 | pnum = 0
11 | root.after(7000, photograph)
12 |
13 | root = tkinter.Tk()
14 | root.title("디지털 액자")
15 | canvas = tkinter.Canvas(width=800, height=600)
16 | canvas.pack()
17 | photo = [
18 | tkinter.PhotoImage(file="cat00.png"),
19 | tkinter.PhotoImage(file="cat01.png"),
20 | tkinter.PhotoImage(file="cat02.png"),
21 | tkinter.PhotoImage(file="cat03.png")
22 | ]
23 | photograph()
24 | root.mainloop()
25 |
--------------------------------------------------------------------------------
/Chapter8/list0801_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | tmr = 0 # type: int
3 | def count_up():
4 | global tmr
5 | tmr = tmr + 1
6 | label["text"] = tmr
7 | root.after(1000, count_up)
8 |
9 | root = tkinter.Tk()
10 | label = tkinter.Label(font=("Times New Roman", 80))
11 | label.pack()
12 | root.after(1000, count_up)
13 | root.mainloop()
14 |
--------------------------------------------------------------------------------
/Chapter8/list0802_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | key = 0
3 | def key_down(e):
4 | global key
5 | key = e.keycode
6 | print("KEY:" + str(key))
7 |
8 | root = tkinter.Tk()
9 | root.title("키 코드 얻기")
10 | root.bind("", key_down)
11 | root.mainloop()
12 |
--------------------------------------------------------------------------------
/Chapter8/list0803_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | key = 0
4 | def key_down(e):
5 | global key
6 | key = e.keycode
7 |
8 | def main_proc():
9 | label["text"] = key
10 | root.after(100, main_proc)
11 |
12 | root = tkinter.Tk()
13 | root.title("실시간 키입력")
14 | root.bind("", key_down)
15 | label = tkinter.Label(font=("Times New Roman", 80))
16 | label.pack()
17 | main_proc()
18 | root.mainloop()
19 |
--------------------------------------------------------------------------------
/Chapter8/list0803_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | key = ""
4 | def key_down(e):
5 | global key
6 | key = e.keysym
7 |
8 | def main_proc():
9 | label["text"] = key
10 | root.after(100, main_proc)
11 |
12 | root = tkinter.Tk()
13 | root.title("실시간 키입력")
14 | root.bind("", key_down)
15 | label = tkinter.Label(font=("Times New Roman", 80))
16 | label.pack()
17 | main_proc()
18 | root.mainloop()
19 |
--------------------------------------------------------------------------------
/Chapter8/list0803_3.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | key = ""
4 | def key_down(e):
5 | global key
6 | key = e.keysym
7 | def key_up(e):
8 | global key
9 | key = ""
10 |
11 | cx = 400
12 | cy = 300
13 | def main_proc():
14 | global cx, cy
15 | if key == "Up":
16 | cy = cy - 20
17 | if key == "Down":
18 | cy = cy + 20
19 | if key == "Left":
20 | cx = cx - 20
21 | if key == "Right":
22 | cx = cx + 20
23 | canvas.coords("MYCHR", cx, cy)
24 | root.after(100, main_proc)
25 |
26 | root = tkinter.Tk()
27 | root.title("캐릭터 이동")
28 | root.bind("", key_down)
29 | root.bind("", key_up)
30 | canvas = tkinter.Canvas(width=800, height=600, bg="lightgreen")
31 | canvas.pack()
32 | img = tkinter.PhotoImage(file="mimi.png")
33 | canvas.create_image(cx, cy, image=img, tag="MYCHR")
34 | main_proc()
35 | root.mainloop()
36 |
--------------------------------------------------------------------------------
/Chapter8/list0804_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | root = tkinter.Tk()
3 | root.title("미로 표시")
4 | canvas = tkinter.Canvas(width=800, height=560, bg="white")
5 | canvas.pack()
6 | maze = [
7 | [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
8 | [1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
9 | [1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
10 | [1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
11 | [1, 0, 0, 1, 1, 1, 1, 1, 0, 1],
12 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
13 | [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
14 | ]
15 | for y in range(7):
16 | for x in range(10):
17 | if maze[y][x] == 1:
18 | canvas.create_rectangle(x * 80, y * 80, x * 80 + 80, y * 80 + 80, fill="gray")
19 | root.mainloop()
20 |
--------------------------------------------------------------------------------
/Chapter8/list0805_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | key = ""
4 | def key_down(e):
5 | global key
6 | key = e.keysym
7 | def key_up(e):
8 | global key
9 | key = ""
10 |
11 | mx = 1
12 | my = 1
13 | def main_proc():
14 | global mx, my
15 | if key == "Up" and maze[my - 1][mx] == 0:
16 | my = my - 1
17 | if key == "Down" and maze[my + 1][mx] == 0:
18 | my = my + 1
19 | if key == "Left" and maze[my][mx - 1] == 0:
20 | mx = mx - 1
21 | if key == "Right" and maze[my][mx + 1] == 0:
22 | mx = mx + 1
23 | canvas.coords("MYCHR", mx * 80 + 40, my * 80 + 40)
24 | root.after(300, main_proc)
25 |
26 | root = tkinter.Tk()
27 | root.title("미로 안 이동하기")
28 | root.bind("", key_down)
29 | root.bind("", key_up)
30 | canvas = tkinter.Canvas(width=800, height=560, bg="white")
31 | canvas.pack()
32 |
33 | maze = [
34 | [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
35 | [1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
36 | [1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
37 | [1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
38 | [1, 0, 0, 1, 1, 1, 1, 1, 0, 1],
39 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
40 | [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
41 | ]
42 | for y in range(7):
43 | for x in range(10):
44 | if maze[y][x] == 1:
45 | canvas.create_rectangle(x * 80, y * 80, x * 80 + 79, y * 80 + 79, fill="skyblue", width=0)
46 |
47 | img = tkinter.PhotoImage(file="mimi_s.png")
48 | canvas.create_image(mx * 80 + 40, my * 80 + 40, image=img, tag="MYCHR")
49 | main_proc()
50 | root.mainloop()
51 |
--------------------------------------------------------------------------------
/Chapter8/list0806_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | key = ""
4 | def key_down(e):
5 | global key
6 | key = e.keysym
7 | def key_up(e):
8 | global key
9 | key = ""
10 |
11 | mx = 1
12 | my = 1
13 | def main_proc():
14 | global mx, my
15 | if key == "Up" and maze[my - 1][mx] == 0:
16 | my = my - 1
17 | if key == "Down" and maze[my + 1][mx] == 0:
18 | my = my + 1
19 | if key == "Left" and maze[my][mx - 1] == 0:
20 | mx = mx - 1
21 | if key == "Right" and maze[my][mx + 1] == 0:
22 | mx = mx + 1
23 | if maze[my][mx] == 0:
24 | maze[my][mx] = 2
25 | canvas.create_rectangle(mx * 80, my * 80, mx * 80 + 79, my * 80 + 79, fill="pink", width=0)
26 | canvas.delete("MYCHR")
27 | canvas.create_image(mx * 80 + 40, my * 80 + 40, image=img, tag="MYCHR")
28 | root.after(300, main_proc)
29 |
30 | root = tkinter.Tk()
31 | root.title("미로를 칠한다냥")
32 | root.bind("", key_down)
33 | root.bind("", key_up)
34 | canvas = tkinter.Canvas(width=800, height=560, bg="white")
35 | canvas.pack()
36 |
37 | maze = [
38 | [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
39 | [1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
40 | [1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
41 | [1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
42 | [1, 0, 0, 1, 1, 1, 1, 1, 0, 1],
43 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
44 | [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
45 | ]
46 | for y in range(7):
47 | for x in range(10):
48 | if maze[y][x] == 1:
49 | canvas.create_rectangle(x * 80, y * 80, x * 80 + 79, y * 80 + 79, fill="skyblue", width=0)
50 |
51 | img = tkinter.PhotoImage(file="mimi_s.png")
52 | canvas.create_image(mx * 80 + 40, my * 80 + 40, image=img, tag="MYCHR")
53 | main_proc()
54 | root.mainloop()
55 |
--------------------------------------------------------------------------------
/Chapter8/list0806_2.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import tkinter.messagebox
3 |
4 | key = ""
5 | def key_down(e):
6 | global key
7 | key = e.keysym
8 | def key_up(e):
9 | global key
10 | key = ""
11 |
12 | mx = 1
13 | my = 1
14 | yuka = 0
15 | def main_proc():
16 | global mx, my, yuka
17 | if key == "Up" and maze[my-1][mx] == 0:
18 | my = my - 1
19 | if key == "Down" and maze[my+1][mx] == 0:
20 | my = my + 1
21 | if key == "Left" and maze[my][mx-1] == 0:
22 | mx = mx - 1
23 | if key == "Right" and maze[my][mx+1] == 0:
24 | mx = mx + 1
25 | if maze[my][mx] == 0:
26 | maze[my][mx] = 2
27 | yuka = yuka + 1
28 | canvas.create_rectangle(mx*80, my*80, mx*80+79, my*80+79, fill="pink", width=0)
29 | canvas.delete("MYCHR")
30 | canvas.create_image(mx*80+40, my*80+40, image=img, tag="MYCHR")
31 | if yuka == 30:
32 | canvas.update()
33 | tkinter.messagebox.showinfo("축하합니다!", "모든 바닥을 칠했습니다!")
34 | else:
35 | root.after(300, main_proc)
36 |
37 | root = tkinter.Tk()
38 | root.title("미로를 칠한다냥")
39 | root.bind("", key_down)
40 | root.bind("", key_up)
41 | canvas = tkinter.Canvas(width=800, height=560, bg="white")
42 | canvas.pack()
43 |
44 | maze = [
45 | [1,1,1,1,1,1,1,1,1,1],
46 | [1,0,0,0,0,0,1,0,0,1],
47 | [1,0,1,1,0,0,1,0,0,1],
48 | [1,0,0,1,0,0,0,0,0,1],
49 | [1,0,0,1,1,1,1,1,0,1],
50 | [1,0,0,0,0,0,0,0,0,1],
51 | [1,1,1,1,1,1,1,1,1,1]
52 | ]
53 | for y in range(7):
54 | for x in range(10):
55 | if maze[y][x] == 1:
56 | canvas.create_rectangle(x*80, y*80, x*80+79, y*80+79, fill="skyblue", width=0)
57 |
58 | img = tkinter.PhotoImage(file="mimi_s.png")
59 | canvas.create_image(mx*80+40, my*80+40, image=img, tag="MYCHR")
60 | main_proc()
61 | root.mainloop()
62 |
--------------------------------------------------------------------------------
/Chapter8/list0806_3.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import tkinter.messagebox
3 |
4 | key = ""
5 | def key_down(e):
6 | global key
7 | key = e.keysym
8 | def key_up(e):
9 | global key
10 | key = ""
11 |
12 | mx = 1
13 | my = 1
14 | yuka = 0
15 | def main_proc():
16 | global mx, my, yuka
17 | if key == "Shift_L" and yuka > 1:
18 | canvas.delete("PAINT")
19 | mx = 1
20 | my = 1
21 | yuka = 0
22 | for y in range(7):
23 | for x in range(10):
24 | if maze[y][x] == 2:
25 | maze[y][x] = 0
26 | if key == "Up" and maze[my - 1][mx] == 0:
27 | my = my - 1
28 | if key == "Down" and maze[my + 1][mx] == 0:
29 | my = my + 1
30 | if key == "Left" and maze[my][mx - 1] == 0:
31 | mx = mx - 1
32 | if key == "Right" and maze[my][mx + 1] == 0:
33 | mx = mx + 1
34 | if maze[my][mx] == 0:
35 | maze[my][mx] = 2
36 | yuka = yuka + 1
37 | canvas.create_rectangle(mx * 80, my * 80, mx * 80 + 79, my * 80 + 79, fill="pink", width=0, tag="PAINT")
38 | canvas.delete("MYCHR")
39 | canvas.create_image(mx * 80 + 40, my * 80 + 40, image=img, tag="MYCHR")
40 | if yuka == 30:
41 | canvas.update()
42 | tkinter.messagebox.showinfo("축하합니다!", "모든 바닥을 칠했습니다!")
43 | else:
44 | root.after(300, main_proc)
45 |
46 | root = tkinter.Tk()
47 | root.title("바닥을 칠한다냥")
48 | root.bind("", key_down)
49 | root.bind("", key_up)
50 | canvas = tkinter.Canvas(width=800, height=560, bg="white")
51 | canvas.pack()
52 |
53 | maze = [
54 | [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
55 | [1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
56 | [1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
57 | [1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
58 | [1, 0, 0, 1, 1, 1, 1, 1, 0, 1],
59 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
60 | [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
61 | ]
62 | for y in range(7):
63 | for x in range(10):
64 | if maze[y][x] == 1:
65 | canvas.create_rectangle(x * 80, y * 80, x * 80 + 79, y * 80 + 79, fill="skyblue", width=0)
66 |
67 | img = tkinter.PhotoImage(file="mimi_s.png")
68 | canvas.create_image(mx * 80 + 40, my * 80 + 40, image=img, tag="MYCHR")
69 | main_proc()
70 | root.mainloop()
71 |
--------------------------------------------------------------------------------
/Chapter8/mimi.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter8/mimi.png
--------------------------------------------------------------------------------
/Chapter8/mimi_s.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter8/mimi_s.png
--------------------------------------------------------------------------------
/Chapter9/column09.py:
--------------------------------------------------------------------------------
1 | import winsound
2 | print("사운드 시작")
3 | winsound.Beep(261,1000)
4 | winsound.Beep(293,1000)
5 | winsound.Beep(329,1000)
6 | winsound.Beep(349,1000)
7 | winsound.Beep(392,1000)
8 | winsound.Beep(440,1000)
9 | winsound.Beep(493,1000)
10 | winsound.Beep(523,1000)
11 | print("사운드 종료")
12 |
--------------------------------------------------------------------------------
/Chapter9/list0902_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | mouse_x = 0
4 | mouse_y = 0
5 | mouse_c = 0
6 |
7 | def mouse_move(e):
8 | global mouse_x, mouse_y
9 | mouse_x = e.x
10 | mouse_y = e.y
11 |
12 | def mouse_press(e):
13 | global mouse_c
14 | mouse_c = 1
15 |
16 | def mouse_release(e):
17 | global mouse_c
18 | mouse_c = 0
19 |
20 | def game_main():
21 | fnt = ("Times New Roman", 30)
22 | txt = "mouse({},{}){}".format(mouse_x, mouse_y, mouse_c)
23 | cvs.delete("TEST")
24 | cvs.create_text(456, 384, text=txt, fill="black", font=fnt, tag="TEST")
25 | root.after(100, game_main)
26 |
27 | root = tkinter.Tk()
28 | root.title("마우스 입력")
29 | root.resizable(False, False)
30 | root.bind("", mouse_move)
31 | root.bind("", mouse_press)
32 | root.bind("", mouse_release)
33 | cvs = tkinter.Canvas(root, width=912, height=768)
34 | cvs.pack()
35 | game_main()
36 | root.mainloop()
37 |
--------------------------------------------------------------------------------
/Chapter9/list0903_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | cursor_x = 0
4 | cursor_y = 0
5 | mouse_x = 0
6 | mouse_y = 0
7 |
8 | def mouse_move(e):
9 | global mouse_x, mouse_y
10 | mouse_x = e.x
11 | mouse_y = e.y
12 |
13 | def game_main():
14 | global cursor_x, cursor_y
15 | if 24 <= mouse_x and mouse_x < 24 + 72 * 8 and 24 <= mouse_y and mouse_y < 24 + 72 * 10:
16 | cursor_x = int((mouse_x - 24) / 72)
17 | cursor_y = int((mouse_y - 24) / 72)
18 | cvs.delete("CURSOR")
19 | cvs.create_image(cursor_x * 72 + 60, cursor_y * 72 + 60, image=cursor, tag="CURSOR")
20 | root.after(100, game_main)
21 |
22 | root = tkinter.Tk()
23 | root.title("커서 표시")
24 | root.resizable(False, False)
25 | root.bind("", mouse_move)
26 | cvs = tkinter.Canvas(root, width=912, height=768)
27 | cvs.pack()
28 |
29 | bg = tkinter.PhotoImage(file="kitty_bg.png")
30 | cursor = tkinter.PhotoImage(file="kity_cursor.png")
31 | cvs.create_image(456, 384, image=bg)
32 | game_main()
33 | root.mainloop()
34 |
--------------------------------------------------------------------------------
/Chapter9/list0904_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | neko = [
4 | [1, 0, 0, 0, 0, 0, 7, 7],
5 | [0, 2, 0, 0, 0, 0, 7, 7],
6 | [0, 0, 3, 0, 0, 0, 0, 0],
7 | [0, 0, 0, 4, 0, 0, 0, 0],
8 | [0, 0, 0, 0, 5, 0, 0, 0],
9 | [0, 0, 0, 0, 0, 6, 0, 0],
10 | [0, 0, 0, 0, 0, 0, 0, 0],
11 | [0, 0, 0, 0, 0, 0, 0, 0],
12 | [0, 0, 0, 0, 0, 0, 0, 0],
13 | [0, 0, 1, 2, 3, 4, 5, 6]
14 | ]
15 |
16 | def draw_neko():
17 | for y in range(10):
18 | for x in range(8):
19 | if neko[y][x] > 0:
20 | cvs.create_image(x * 72 + 60, y * 72 + 60, image=img_neko[neko[y][x]])
21 |
22 | root = tkinter.Tk()
23 | root.title("2차원 리스트로 위치 관리")
24 | root.resizable(False, False)
25 | cvs = tkinter.Canvas(root, width=912, height=768)
26 | cvs.pack()
27 |
28 | bg = tkinter.PhotoImage(file="kitty_bg.png")
29 | img_neko = [
30 | None,
31 | tkinter.PhotoImage(file="kitty1.png"),
32 | tkinter.PhotoImage(file="kitty2.png"),
33 | tkinter.PhotoImage(file="kitty3.png"),
34 | tkinter.PhotoImage(file="kitty4.png"),
35 | tkinter.PhotoImage(file="kitty5.png"),
36 | tkinter.PhotoImage(file="kitty6.png"),
37 | tkinter.PhotoImage(file="kitty_niku.png")
38 | ]
39 |
40 | cvs.create_image(456, 384, image=bg)
41 | draw_neko()
42 | root.mainloop()
43 |
--------------------------------------------------------------------------------
/Chapter9/list0905_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 |
3 | neko = [
4 | [1, 0, 0, 0, 0, 0, 1, 2],
5 | [0, 2, 0, 0, 0, 0, 3, 4],
6 | [0, 0, 3, 0, 0, 0, 0, 0],
7 | [0, 0, 0, 4, 0, 0, 0, 0],
8 | [0, 0, 0, 0, 5, 0, 0, 0],
9 | [0, 0, 0, 0, 0, 6, 0, 0],
10 | [0, 0, 0, 0, 0, 0, 0, 0],
11 | [0, 0, 0, 0, 0, 0, 0, 0],
12 | [0, 0, 0, 0, 0, 0, 0, 0],
13 | [0, 0, 1, 2, 3, 4, 0, 0]
14 | ]
15 |
16 | def draw_neko():
17 | for y in range(10):
18 | for x in range(8):
19 | if neko[y][x] > 0:
20 | cvs.create_image(x * 72 + 60, y * 72 + 60, image=img_neko[neko[y][x]], tag="NEKO")
21 |
22 | def drop_neko():
23 | for y in range(8, -1, -1):
24 | for x in range(8):
25 | if neko[y][x] != 0 and neko[y + 1][x] == 0:
26 | neko[y + 1][x] = neko[y][x]
27 | neko[y][x] = 0
28 |
29 | def game_main():
30 | drop_neko()
31 | cvs.delete("NEKO")
32 | draw_neko()
33 | root.after(100, game_main)
34 |
35 | root = tkinter.Tk()
36 | root.title("고양이 낙하시키기")
37 | root.resizable(False, False)
38 | cvs = tkinter.Canvas(root, width=912, height=768)
39 | cvs.pack()
40 |
41 | bg = tkinter.PhotoImage(file="kitty_bg.png")
42 | img_neko = [
43 | None,
44 | tkinter.PhotoImage(file="kitty1.png"),
45 | tkinter.PhotoImage(file="kitty2.png"),
46 | tkinter.PhotoImage(file="kitty3.png"),
47 | tkinter.PhotoImage(file="kitty4.png"),
48 | tkinter.PhotoImage(file="kitty5.png"),
49 | tkinter.PhotoImage(file="kitty6.png"),
50 | tkinter.PhotoImage(file="kitty_niku.png")
51 | ]
52 |
53 | cvs.create_image(456, 384, image=bg)
54 | game_main()
55 | root.mainloop()
56 |
--------------------------------------------------------------------------------
/Chapter9/list0906_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import random
3 |
4 | cursor_x = 0
5 | cursor_y = 0
6 | mouse_x = 0
7 | mouse_y = 0
8 | mouse_c = 0
9 |
10 | def mouse_move(e):
11 | global mouse_x, mouse_y
12 | mouse_x = e.x
13 | mouse_y = e.y
14 |
15 | def mouse_press(e):
16 | global mouse_c
17 | mouse_c = 1
18 |
19 | neko = [
20 | [0, 0, 0, 0, 0, 0, 0, 0],
21 | [0, 0, 0, 0, 0, 0, 0, 0],
22 | [0, 0, 0, 0, 0, 0, 0, 0],
23 | [0, 0, 0, 0, 0, 0, 0, 0],
24 | [0, 0, 0, 0, 0, 0, 0, 0],
25 | [0, 0, 0, 0, 0, 0, 0, 0],
26 | [0, 0, 0, 0, 0, 0, 0, 0],
27 | [0, 0, 0, 0, 0, 0, 0, 0],
28 | [0, 0, 0, 0, 0, 0, 0, 0],
29 | [0, 0, 0, 0, 0, 0, 0, 0]
30 | ]
31 |
32 | def draw_neko():
33 | for y in range(10):
34 | for x in range(8):
35 | if neko[y][x] > 0:
36 | cvs.create_image(x * 72 + 60, y * 72 + 60, image=img_neko[neko[y][x]], tag="NEKO")
37 |
38 | def drop_neko():
39 | for y in range(8, -1, -1):
40 | for x in range(8):
41 | if neko[y][x] != 0 and neko[y + 1][x] == 0:
42 | neko[y + 1][x] = neko[y][x]
43 | neko[y][x] = 0
44 |
45 | def game_main():
46 | global cursor_x, cursor_y, mouse_c
47 | drop_neko()
48 | if 24 <= mouse_x and mouse_x < 24 + 72 * 8 and 24 <= mouse_y and mouse_y < 24 + 72 * 10:
49 | cursor_x = int((mouse_x - 24) / 72)
50 | cursor_y = int((mouse_y - 24) / 72)
51 | if mouse_c == 1:
52 | # mouse_c = 0
53 | neko[cursor_y][cursor_x] = random.randint(1, 6)
54 | cvs.delete("CURSOR")
55 | cvs.create_image(cursor_x * 72 + 60, cursor_y * 72 + 60, image=cursor, tag="CURSOR")
56 | cvs.delete("NEKO")
57 | draw_neko()
58 | root.after(100, game_main)
59 |
60 | root = tkinter.Tk()
61 | root.title("클릭해서 고양이 놓기")
62 | root.resizable(False, False)
63 | root.bind("", mouse_move)
64 | root.bind("", mouse_press)
65 | cvs = tkinter.Canvas(root, width=912, height=768)
66 | cvs.pack()
67 |
68 | bg = tkinter.PhotoImage(file="kitty_bg.png")
69 | cursor = tkinter.PhotoImage(file="kity_cursor.png")
70 | img_neko = [
71 | None,
72 | tkinter.PhotoImage(file="kitty1.png"),
73 | tkinter.PhotoImage(file="kitty2.png"),
74 | tkinter.PhotoImage(file="kitty3.png"),
75 | tkinter.PhotoImage(file="kitty4.png"),
76 | tkinter.PhotoImage(file="kitty5.png"),
77 | tkinter.PhotoImage(file="kitty6.png"),
78 | tkinter.PhotoImage(file="kitty_niku.png")
79 | ]
80 |
81 | cvs.create_image(456, 384, image=bg)
82 | game_main()
83 | root.mainloop()
84 |
--------------------------------------------------------------------------------
/Chapter9/list0907_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import random
3 |
4 | cursor_x = 0
5 | cursor_y = 0
6 | mouse_x = 0
7 | mouse_y = 0
8 | mouse_c = 0
9 |
10 | def mouse_move(e):
11 | global mouse_x, mouse_y
12 | mouse_x = e.x
13 | mouse_y = e.y
14 |
15 | def mouse_press(e):
16 | global mouse_c
17 | mouse_c = 1
18 |
19 | neko = []
20 | for i in range(10):
21 | neko.append([0, 0, 0, 0, 0, 0, 0, 0])
22 |
23 | def draw_neko():
24 | for y in range(10):
25 | for x in range(8):
26 | if neko[y][x] > 0:
27 | cvs.create_image(x * 72 + 60, y * 72 + 60, image=img_neko[neko[y][x]], tag="NEKO")
28 |
29 | def yoko_neko():
30 | for y in range(10):
31 | for x in range(1, 7):
32 | if neko[y][x] > 0:
33 | if neko[y][x - 1] == neko[y][x] and neko[y][x + 1] == neko[y][x]:
34 | neko[y][x - 1] = 7
35 | neko[y][x] = 7
36 | neko[y][x + 1] = 7
37 | for y in range(1, 9):
38 | for x in range(8):
39 | if neko[y][x] > 0:
40 | if neko[y - 1][x] == neko[y][x] and neko[y + 1][x] == neko[y][x]:
41 | neko[y - 1][x] = 7
42 | neko[y][x] = 7
43 | neko[y + 1][x] = 7
44 |
45 | def game_main():
46 | global cursor_x, cursor_y, mouse_c
47 | if 660 <= mouse_x and mouse_x < 840 and 100 <= mouse_y and mouse_y < 160 and mouse_c == 1:
48 | mouse_c = 0
49 | yoko_neko()
50 | if 24 <= mouse_x and mouse_x < 24 + 72 * 8 and 24 <= mouse_y and mouse_y < 24 + 72 * 10:
51 | cursor_x = int((mouse_x - 24) / 72)
52 | cursor_y = int((mouse_y - 24) / 72)
53 | if mouse_c == 1:
54 | mouse_c = 0
55 | neko[cursor_y][cursor_x] = random.randint(1, 2)
56 | cvs.delete("CURSOR")
57 | cvs.create_image(cursor_x * 72 + 60, cursor_y * 72 + 60, image=cursor, tag="CURSOR")
58 | cvs.delete("NEKO")
59 | draw_neko()
60 | root.after(100, game_main)
61 |
62 | root = tkinter.Tk()
63 | root.title("가로로 3개 나란히 놓였는가")
64 | root.resizable(False, False)
65 | root.bind("", mouse_move)
66 | root.bind("", mouse_press)
67 | cvs = tkinter.Canvas(root, width=912, height=768)
68 | cvs.pack()
69 |
70 | bg = tkinter.PhotoImage(file="kitty_bg.png")
71 | cursor = tkinter.PhotoImage(file="kity_cursor.png")
72 | img_neko = [
73 | None,
74 | tkinter.PhotoImage(file="kitty1.png"),
75 | tkinter.PhotoImage(file="kitty2.png"),
76 | tkinter.PhotoImage(file="kitty3.png"),
77 | tkinter.PhotoImage(file="kitty4.png"),
78 | tkinter.PhotoImage(file="kitty5.png"),
79 | tkinter.PhotoImage(file="kitty6.png"),
80 | tkinter.PhotoImage(file="kitty_niku.png")
81 | ]
82 |
83 | cvs.create_image(456, 384, image=bg)
84 | cvs.create_rectangle(660, 100, 840, 160, fill="white")
85 | cvs.create_text(750, 130, text="테스트", fill="red", font=("Times New Roman", 30))
86 | game_main()
87 | root.mainloop()
88 |
--------------------------------------------------------------------------------
/Chapter9/list0908_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import random
3 |
4 | cursor_x = 0
5 | cursor_y = 0
6 | mouse_x = 0
7 | mouse_y = 0
8 | mouse_c = 0
9 |
10 | def mouse_move(e):
11 | global mouse_x, mouse_y
12 | mouse_x = e.x
13 | mouse_y = e.y
14 |
15 | def mouse_press(e):
16 | global mouse_c
17 | mouse_c = 1
18 |
19 | neko = []
20 | check = []
21 | for i in range(10):
22 | neko.append([0, 0, 0, 0, 0, 0, 0, 0])
23 | check.append([0, 0, 0, 0, 0, 0, 0, 0])
24 |
25 | def draw_neko():
26 | for y in range(10):
27 | for x in range(8):
28 | if neko[y][x] > 0:
29 | cvs.create_image(x*72+60, y*72+60, image=img_neko[neko[y][x]], tag="NEKO")
30 |
31 | def check_neko():
32 | for y in range(10):
33 | for x in range(8):
34 | check[y][x] = neko[y][x]
35 |
36 | for y in range(1, 9):
37 | for x in range(8):
38 | if check[y][x] > 0:
39 | if check[y-1][x] == check[y][x] and check[y+1][x] == check[y][x]:
40 | neko[y-1][x] = 7
41 | neko[y][x] = 7
42 | neko[y+1][x] = 7
43 |
44 | for y in range(10):
45 | for x in range(1, 7):
46 | if check[y][x] > 0:
47 | if check[y][x-1] == check[y][x] and check[y][x+1] == check[y][x]:
48 | neko[y][x-1] = 7
49 | neko[y][x] = 7
50 | neko[y][x+1] = 7
51 |
52 | for y in range(1, 9):
53 | for x in range(1, 7):
54 | if check[y][x] > 0:
55 | if check[y-1][x-1] == check[y][x] and check[y+1][x+1] == check[y][x]:
56 | neko[y-1][x-1] = 7
57 | neko[y][x] = 7
58 | neko[y+1][x+1] = 7
59 | if check[y+1][x-1] == check[y][x] and check[y-1][x+1] == check[y][x]:
60 | neko[y+1][x-1] = 7
61 | neko[y][x] = 7
62 | neko[y-1][x+1] = 7
63 |
64 | def game_main():
65 | global cursor_x, cursor_y, mouse_c
66 | if 660 <= mouse_x and mouse_x < 840 and 100 <= mouse_y and mouse_y < 160 and mouse_c == 1:
67 | mouse_c = 0
68 | check_neko()
69 | if 24 <= mouse_x and mouse_x < 24+72*8 and 24 <= mouse_y and mouse_y < 24+72*10:
70 | cursor_x = int((mouse_x-24)/72)
71 | cursor_y = int((mouse_y-24)/72)
72 | if mouse_c == 1:
73 | mouse_c = 0
74 | neko[cursor_y][cursor_x] = random.randint(1, 2)
75 | cvs.delete("CURSOR")
76 | cvs.create_image(cursor_x*72+60, cursor_y*72+60, image=cursor, tag="CURSOR")
77 | cvs.delete("NEKO")
78 | draw_neko()
79 | root.after(100, game_main)
80 |
81 | root = tkinter.Tk()
82 | root.title("세로, 가로, 대각선으로 3개 나란히 놓였는가")
83 | root.resizable(False, False)
84 | root.bind("", mouse_move)
85 | root.bind("", mouse_press)
86 | cvs = tkinter.Canvas(root, width=912, height=768)
87 | cvs.pack()
88 |
89 | bg = tkinter.PhotoImage(file="kitty_bg.png")
90 | cursor = tkinter.PhotoImage(file="kity_cursor.png")
91 | img_neko = [
92 | None,
93 | tkinter.PhotoImage(file="kitty1.png"),
94 | tkinter.PhotoImage(file="kitty2.png"),
95 | tkinter.PhotoImage(file="kitty3.png"),
96 | tkinter.PhotoImage(file="kitty4.png"),
97 | tkinter.PhotoImage(file="kitty5.png"),
98 | tkinter.PhotoImage(file="kitty6.png"),
99 | tkinter.PhotoImage(file="kitty_niku.png")
100 | ]
101 |
102 | cvs.create_image(456, 384, image=bg)
103 | cvs.create_rectangle(660, 100, 840, 160, fill="white")
104 | cvs.create_text(750, 130, text="테스트", fill="red", font=("Times New Roman", 30))
105 | game_main()
106 | root.mainloop()
107 |
--------------------------------------------------------------------------------
/Chapter9/list0909_1.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import random
3 |
4 | index = 0
5 | timer = 0
6 | score = 0
7 | next_kitty = 0
8 |
9 | cursor_x = 0
10 | cursor_y = 0
11 | mouse_x = 0
12 | mouse_y = 0
13 | mouse_c = 0
14 |
15 | def mouse_move(e):
16 | global mouse_x, mouse_y
17 | mouse_x = e.x
18 | mouse_y = e.y
19 |
20 | def mouse_press(e):
21 | global mouse_c
22 | mouse_c = 1
23 |
24 | neko = []
25 | check = []
26 | for i in range(10):
27 | neko.append([0, 0, 0, 0, 0, 0, 0, 0])
28 | check.append([0, 0, 0, 0, 0, 0, 0, 0])
29 |
30 | def draw_neko():
31 | cvs.delete("NEKO")
32 | for y in range(10):
33 | for x in range(8):
34 | if neko[y][x] > 0:
35 | cvs.create_image(x * 72 + 60, y * 72 + 60, image=img_neko[neko[y][x]], tag="NEKO")
36 |
37 | def check_neko():
38 | for y in range(10):
39 | for x in range(8):
40 | check[y][x] = neko[y][x]
41 |
42 | for y in range(1, 9):
43 | for x in range(8):
44 | if check[y][x] > 0:
45 | if check[y - 1][x] == check[y][x] and check[y + 1][x] == check[y][x]:
46 | neko[y - 1][x] = 7
47 | neko[y][x] = 7
48 | neko[y + 1][x] = 7
49 |
50 | for y in range(10):
51 | for x in range(1, 7):
52 | if check[y][x] > 0:
53 | if check[y][x - 1] == check[y][x] and check[y][x + 1] == check[y][x]:
54 | neko[y][x - 1] = 7
55 | neko[y][x] = 7
56 | neko[y][x + 1] = 7
57 |
58 | for y in range(1, 9):
59 | for x in range(1, 7):
60 | if check[y][x] > 0:
61 | if check[y - 1][x - 1] == check[y][x] and check[y + 1][x + 1] == check[y][x]:
62 | neko[y - 1][x - 1] = 7
63 | neko[y][x] = 7
64 | neko[y + 1][x + 1] = 7
65 | if check[y + 1][x - 1] == check[y][x] and check[y - 1][x + 1] == check[y][x]:
66 | neko[y + 1][x - 1] = 7
67 | neko[y][x] = 7
68 | neko[y - 1][x + 1] = 7
69 |
70 | def sweep_neko():
71 | num = 0
72 | for y in range(10):
73 | for x in range(8):
74 | if neko[y][x] == 7:
75 | neko[y][x] = 0
76 | num = num + 1
77 | return num
78 |
79 | def drop_neko():
80 | flg = False
81 | for y in range(8, -1, -1):
82 | for x in range(8):
83 | if neko[y][x] != 0 and neko[y + 1][x] == 0:
84 | neko[y + 1][x] = neko[y][x]
85 | neko[y][x] = 0
86 | flg = True
87 | return flg
88 |
89 | def over_neko():
90 | for x in range(8):
91 | if neko[0][x] > 0:
92 | return True
93 | return False
94 |
95 | def set_neko():
96 | for x in range(8):
97 | neko[0][x] = random.randint(0, 6)
98 |
99 | def draw_txt(txt, x, y, siz, col, tg):
100 | fnt = ("Times New Roman", siz, "bold")
101 | cvs.create_text(x + 2, y + 2, text=txt, fill="black", font=fnt, tag=tg)
102 | cvs.create_text(x, y, text=txt, fill=col, font=fnt, tag=tg)
103 |
104 | def game_main():
105 | global index, timer, score, next_kitty
106 | global cursor_x, cursor_y, mouse_c
107 | if index == 0: # 타이틀 로고
108 | draw_txt("야옹야옹", 312, 240, 100, "violet", "TITLE")
109 | draw_txt("Click to start.", 312, 560, 50, "orange", "TITLE")
110 | index = 1
111 | mouse_c = 0
112 | elif index == 1: # 타이틀 화면 / 시작 대기
113 | if mouse_c == 1:
114 | for y in range(10):
115 | for x in range(8):
116 | neko[y][x] = 0
117 | mouse_c = 0
118 | score = 0
119 | tsugi = 0
120 | cursor_x = 0
121 | cursor_y = 0
122 | set_neko()
123 | draw_neko()
124 | cvs.delete("TITLE")
125 | index = 2
126 | elif index == 2: # 낙하
127 | if drop_neko() == False:
128 | index = 3
129 | draw_neko()
130 | elif index == 3: # 나란히 놓였는가
131 | check_neko()
132 | draw_neko()
133 | index = 4
134 | elif index == 4: # 나란히 놓인 고양이가 있다면 삭제
135 | sc = sweep_neko()
136 | score = score + sc * 10
137 | if sc > 0:
138 | index = 2
139 | else:
140 | if over_neko() == False:
141 | tsugi = random.randint(1, 6)
142 | index = 5
143 | else:
144 | index = 6
145 | timer = 0
146 | draw_neko()
147 | elif index == 5: # 마우스 입력 대기
148 | if 24 <= mouse_x and mouse_x < 24 + 72 * 8 and 24 <= mouse_y and mouse_y < 24 + 72 * 10:
149 | cursor_x = int((mouse_x - 24) / 72)
150 | cursor_y = int((mouse_y - 24) / 72)
151 | if mouse_c == 1:
152 | mouse_c = 0
153 | set_neko()
154 | neko[cursor_y][cursor_x] = tsugi
155 | tsugi = 0
156 | index = 2
157 | cvs.delete("CURSOR")
158 | cvs.create_image(cursor_x * 72 + 60, cursor_y * 72 + 60, image=cursor, tag="CURSOR")
159 | draw_neko()
160 | elif index == 6: # 게임 오버
161 | timer = timer + 1
162 | if timer == 1:
163 | draw_txt("GAME OVER", 312, 348, 60, "red", "OVER")
164 | if timer == 50:
165 | cvs.delete("OVER")
166 | index = 0
167 | cvs.delete("INFO")
168 | draw_txt("SCORE " + str(score), 160, 60, 32, "blue", "INFO")
169 | if tsugi > 0:
170 | cvs.create_image(752, 128, image=img_neko[tsugi], tag="INFO")
171 | root.after(100, game_main)
172 |
173 | root = tkinter.Tk()
174 | root.title("낙하 퍼즐 '야옹야옹'")
175 | root.resizable(False, False)
176 | root.bind("", mouse_move)
177 | root.bind("", mouse_press)
178 | cvs = tkinter.Canvas(root, width=912, height=768)
179 | cvs.pack()
180 |
181 | bg = tkinter.PhotoImage(file="kitty_bg.png")
182 | cursor = tkinter.PhotoImage(file="kity_cursor.png")
183 | img_neko = [
184 | None,
185 | tkinter.PhotoImage(file="kitty1.png"),
186 | tkinter.PhotoImage(file="kitty2.png"),
187 | tkinter.PhotoImage(file="kitty3.png"),
188 | tkinter.PhotoImage(file="kitty4.png"),
189 | tkinter.PhotoImage(file="kitty5.png"),
190 | tkinter.PhotoImage(file="kitty6.png"),
191 | tkinter.PhotoImage(file="kitty_niku.png")
192 | ]
193 |
194 | cvs.create_image(456, 384, image=bg)
195 | game_main()
196 | root.mainloop()
197 |
--------------------------------------------------------------------------------
/Chapter9/neko1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter9/neko1.png
--------------------------------------------------------------------------------
/Chapter9/neko2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter9/neko2.png
--------------------------------------------------------------------------------
/Chapter9/neko3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter9/neko3.png
--------------------------------------------------------------------------------
/Chapter9/neko4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter9/neko4.png
--------------------------------------------------------------------------------
/Chapter9/neko5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter9/neko5.png
--------------------------------------------------------------------------------
/Chapter9/neko6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter9/neko6.png
--------------------------------------------------------------------------------
/Chapter9/neko_bg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter9/neko_bg.png
--------------------------------------------------------------------------------
/Chapter9/neko_cursor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter9/neko_cursor.png
--------------------------------------------------------------------------------
/Chapter9/neko_niku.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jpub/PythonGame_1/af32318bf1e6ea73aa00fc4c72d07e1a5d7c5300/Chapter9/neko_niku.png
--------------------------------------------------------------------------------
/Chapter9/neko_pzl.py:
--------------------------------------------------------------------------------
1 | import tkinter
2 | import random
3 |
4 | index = 0
5 | timer = 0
6 | score = 0
7 | hisc = 1000
8 | difficulty = 0
9 | tsugi = 0
10 |
11 | cursor_x = 0
12 | cursor_y = 0
13 | mouse_x = 0
14 | mouse_y = 0
15 | mouse_c = 0
16 |
17 | def mouse_move(e):
18 | global mouse_x, mouse_y
19 | mouse_x = e.x
20 | mouse_y = e.y
21 |
22 | def mouse_press(e):
23 | global mouse_c
24 | mouse_c = 1
25 |
26 | neko = []
27 | check = []
28 | for i in range(10):
29 | neko.append([0, 0, 0, 0, 0, 0, 0, 0])
30 | check.append([0, 0, 0, 0, 0, 0, 0, 0])
31 |
32 | def draw_neko():
33 | cvs.delete("NEKO")
34 | for y in range(10):
35 | for x in range(8):
36 | if neko[y][x] > 0:
37 | cvs.create_image(x * 72 + 60, y * 72 + 60, image=img_neko[neko[y][x]], tag="NEKO")
38 |
39 | def check_neko():
40 | for y in range(10):
41 | for x in range(8):
42 | check[y][x] = neko[y][x]
43 |
44 | for y in range(1, 9):
45 | for x in range(8):
46 | if check[y][x] > 0:
47 | if check[y - 1][x] == check[y][x] and check[y + 1][x] == check[y][x]:
48 | neko[y - 1][x] = 7
49 | neko[y][x] = 7
50 | neko[y + 1][x] = 7
51 |
52 | for y in range(10):
53 | for x in range(1, 7):
54 | if check[y][x] > 0:
55 | if check[y][x - 1] == check[y][x] and check[y][x + 1] == check[y][x]:
56 | neko[y][x - 1] = 7
57 | neko[y][x] = 7
58 | neko[y][x + 1] = 7
59 |
60 | for y in range(1, 9):
61 | for x in range(1, 7):
62 | if check[y][x] > 0:
63 | if check[y - 1][x - 1] == check[y][x] and check[y + 1][x + 1] == check[y][x]:
64 | neko[y - 1][x - 1] = 7
65 | neko[y][x] = 7
66 | neko[y + 1][x + 1] = 7
67 | if check[y + 1][x - 1] == check[y][x] and check[y - 1][x + 1] == check[y][x]:
68 | neko[y + 1][x - 1] = 7
69 | neko[y][x] = 7
70 | neko[y - 1][x + 1] = 7
71 |
72 | def sweep_neko():
73 | num = 0
74 | for y in range(10):
75 | for x in range(8):
76 | if neko[y][x] == 7:
77 | neko[y][x] = 0
78 | num = num + 1
79 | return num
80 |
81 | def drop_neko():
82 | flg = False
83 | for y in range(8, -1, -1):
84 | for x in range(8):
85 | if neko[y][x] != 0 and neko[y + 1][x] == 0:
86 | neko[y + 1][x] = neko[y][x]
87 | neko[y][x] = 0
88 | flg = True
89 | return flg
90 |
91 | def over_neko():
92 | for x in range(8):
93 | if neko[0][x] > 0:
94 | return True
95 | return False
96 |
97 | def set_neko():
98 | for x in range(8):
99 | neko[0][x] = random.randint(0, difficulty)
100 |
101 | def draw_txt(txt, x, y, siz, col, tg):
102 | fnt = ("Times New Roman", siz, "bold")
103 | cvs.create_text(x + 2, y + 2, text=txt, fill="black", font=fnt, tag=tg)
104 | cvs.create_text(x, y, text=txt, fill=col, font=fnt, tag=tg)
105 |
106 | def game_main():
107 | global index, timer, score, hisc, difficulty, tsugi
108 | global cursor_x, cursor_y, mouse_c
109 | if index == 0: # 타이틀 로고
110 | draw_txt("야옹야옹", 312, 240, 100, "violet", "TITLE")
111 | cvs.create_rectangle(168, 384, 456, 456, fill="skyblue", width=0, tag="TITLE")
112 | draw_txt("Easy", 312, 420, 40, "white", "TITLE")
113 | cvs.create_rectangle(168, 528, 456, 600, fill="lightgreen", width=0, tag="TITLE")
114 | draw_txt("Normal", 312, 564, 40, "white", "TITLE")
115 | cvs.create_rectangle(168, 672, 456, 744, fill="orange", width=0, tag="TITLE")
116 | draw_txt("Hard", 312, 708, 40, "white", "TITLE")
117 | index = 1
118 | mouse_c = 0
119 | elif index == 1: # 타이틀 화면, 시작 대기
120 | difficulty = 0
121 | if mouse_c == 1:
122 | if 168 < mouse_x and mouse_x < 456 and 384 < mouse_y and mouse_y < 456:
123 | difficulty = 4
124 | if 168 < mouse_x and mouse_x < 456 and 528 < mouse_y and mouse_y < 600:
125 | difficulty = 5
126 | if 168 < mouse_x and mouse_x < 456 and 672 < mouse_y and mouse_y < 744:
127 | difficulty = 6
128 | if difficulty > 0:
129 | for y in range(10):
130 | for x in range(8):
131 | neko[y][x] = 0
132 | mouse_c = 0
133 | score = 0
134 | tsugi = 0
135 | cursor_x = 0
136 | cursor_y = 0
137 | set_neko()
138 | draw_neko()
139 | cvs.delete("TITLE")
140 | index = 2
141 | elif index == 2: # 블록 낙하
142 | if drop_neko() == False:
143 | index = 3
144 | draw_neko()
145 | elif index == 3: # 나란히 놓인 블록 확인
146 | check_neko()
147 | draw_neko()
148 | index = 4
149 | elif index == 4: # 나란히 놓인 고양이 블록이 있다면
150 | sc = sweep_neko()
151 | score = score + sc * difficulty * 2
152 | if score > hisc:
153 | hisc = score
154 | if sc > 0:
155 | index = 2
156 | else:
157 | if over_neko() == False:
158 | tsugi = random.randint(1, difficulty)
159 | index = 5
160 | else:
161 | index = 6
162 | timer = 0
163 | draw_neko()
164 | elif index == 5: # 마우스 입력 대기
165 | if 24 <= mouse_x and mouse_x < 24 + 72 * 8 and 24 <= mouse_y and mouse_y < 24 + 72 * 10:
166 | cursor_x = int((mouse_x - 24) / 72)
167 | cursor_y = int((mouse_y - 24) / 72)
168 | if mouse_c == 1:
169 | mouse_c = 0
170 | set_neko()
171 | neko[cursor_y][cursor_x] = tsugi
172 | tsugi = 0
173 | index = 2
174 | cvs.delete("CURSOR")
175 | cvs.create_image(cursor_x * 72 + 60, cursor_y * 72 + 60, image=cursor, tag="CURSOR")
176 | draw_neko()
177 | elif index == 6: # 게임 오버
178 | timer = timer + 1
179 | if timer == 1:
180 | draw_txt("GAME OVER", 312, 348, 60, "red", "OVER")
181 | if timer == 50:
182 | cvs.delete("OVER")
183 | index = 0
184 | cvs.delete("INFO")
185 | draw_txt("SCORE " + str(score), 160, 60, 32, "blue", "INFO")
186 | draw_txt("HISC " + str(hisc), 450, 60, 32, "yellow", "INFO")
187 | if tsugi > 0:
188 | cvs.create_image(752, 128, image=img_neko[tsugi], tag="INFO")
189 | root.after(100, game_main)
190 |
191 | root = tkinter.Tk()
192 | root.title("블록 낙하 퍼즐 '야옹야옹'")
193 | root.resizable(False, False)
194 | root.bind("", mouse_move)
195 | root.bind("", mouse_press)
196 | cvs = tkinter.Canvas(root, width=912, height=768)
197 | cvs.pack()
198 |
199 | bg = tkinter.PhotoImage(file="neko_bg.png")
200 | cursor = tkinter.PhotoImage(file="neko_cursor.png")
201 | img_neko = [
202 | None,
203 | tkinter.PhotoImage(file="neko1.png"),
204 | tkinter.PhotoImage(file="neko2.png"),
205 | tkinter.PhotoImage(file="neko3.png"),
206 | tkinter.PhotoImage(file="neko4.png"),
207 | tkinter.PhotoImage(file="neko5.png"),
208 | tkinter.PhotoImage(file="neko6.png"),
209 | tkinter.PhotoImage(file="neko_niku.png")
210 | ]
211 |
212 | cvs.create_image(456, 384, image=bg)
213 | game_main()
214 | root.mainloop()
215 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 파이썬으로 배우는 게임 개발 입문편
2 | 
3 |
4 | **출판사** 제이펍
5 | **원서명** Pythonでつくる ゲーム開発 入門講座(원서 ISBN: 9784800712394)
6 | **저자명** 히로세 츠요시
7 | **역자명** 김연수
8 | **출판일** 2020년 10월 26일
9 | **페이지** 424쪽
10 | **ISBN** 979-11-90665-44-5 (93000)
11 |
12 | [### 도서 소개 페이지 바로 가기 ###](https://jpub.tistory.com/1095)
--------------------------------------------------------------------------------