-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathそうこばん.py
132 lines (108 loc) · 4.23 KB
/
そうこばん.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import tkinter as tk
from tkinter import messagebox
import copy
class Sokoban:
def __init__(self):
self.root = tk.Tk()
self.root.title("倉庫番 (Sokoban)")
# Create a frame to hold the game board
self.game_frame = tk.Frame(self.root)
self.game_frame.pack(expand=True)
# Game state
self.level = [
["#", "#", "#", "#", "#"],
["#", " ", " ", " ", "#"],
["#", " ", "@", " ", "#"],
["#", " ", "$", ".", "#"],
["#", "#", "#", "#", "#"]
]
self.player_pos = [2, 2]
# Create game board
self.cells = []
for i in range(len(self.level)):
row = []
for j in range(len(self.level[0])):
cell = tk.Label(self.game_frame, width=2, height=1, relief="solid")
cell.grid(row=i, column=j)
row.append(cell)
self.cells.append(row)
# Bind keys
self.root.bind("<Up>", lambda e: self.move("up"))
self.root.bind("<Down>", lambda e: self.move("down"))
self.root.bind("<Left>", lambda e: self.move("left"))
self.root.bind("<Right>", lambda e: self.move("right"))
# Center the window after creating all elements
self.center_window()
self.update_display()
def move(self, direction):
dx, dy = 0, 0
if direction == "up": dy = -1
elif direction == "down": dy = 1
elif direction == "left": dx = -1
elif direction == "right": dx = 1
x, y = self.player_pos
new_x, new_y = x + dx, y + dy
# Check if move is valid
if self.level[new_y][new_x] == "#":
return
# Handle box pushing
if self.level[new_y][new_x] in ["$", "*"]:
box_x, box_y = new_x + dx, new_y + dy
if self.level[box_y][box_x] in ["#", "$", "*"]:
return
# Move box
if self.level[new_y][new_x] == "$":
self.level[new_y][new_x] = " "
else:
self.level[new_y][new_x] = "."
if self.level[box_y][box_x] == ".":
self.level[box_y][box_x] = "*"
else:
self.level[box_y][box_x] = "$"
# Move player
self.level[y][x] = " " if self.level[y][x] == "@" else "."
self.level[new_y][new_x] = "@"
self.player_pos = [new_x, new_y]
self.update_display()
self.check_win()
def update_display(self):
for i in range(len(self.level)):
for j in range(len(self.level[0])):
cell = self.cells[i][j]
char = self.level[i][j]
if char == "#":
cell.config(bg="gray", text="")
elif char == " ":
cell.config(bg="white", text="")
elif char == "@":
cell.config(bg="yellow", text="@")
elif char == "$":
cell.config(bg="brown", text="$")
elif char == ".":
cell.config(bg="green", text=".")
elif char == "*":
cell.config(bg="blue", text="*")
def check_win(self):
for row in self.level:
for cell in row:
if cell == "$":
return
messagebox.showinfo("Congratulations!", "You won!")
def run(self):
self.root.mainloop()
def center_window(self):
# Wait for window to be rendered
self.root.update_idletasks()
# Get screen and window dimensions
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
window_width = self.root.winfo_width()
window_height = self.root.winfo_height()
# Calculate position
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
# Set the position
self.root.geometry(f'+{x}+{y}')
if __name__ == "__main__":
game = Sokoban()
game.run()