如何用Python制作贪吃蛇游戏窗口
今天搞定,手把手教你制作贪吃啥游戏,让游戏设计不再神秘。
今天搞了一个游戏玩玩,不能只是玩代码经常感觉太鼓噪了,为了让我们的学习、生活增加一点乐趣,今天搞搞游戏。
记得以前有那种游戏机,很小的时候,经常玩。也不知道是怎么弄成了。特感的好奇、贪玩,也经常game over,从头再来,你还记得么。
今天我们也尝试自己弄弄,主要是锻炼代码,熟悉代码,不要贪玩哦。
大概设计如下较为简单游戏界面
游戏开始
贪吃蛇开始
玩几下,碰墙结束了(要考虑结束的条件)
团吃蛇游戏接受了
改的玩
换个背景看看
很感谢那些设计编辑代码软件的人,我们学会使用就可以了。
代码在下面,自己拿去玩吧。代码段有些注释初学自己看着做,高手指点,此处谢过。
import tkinter as tk
import random
class tcs_Game:
def __init__(self, tcs):#初始化tcs
self.tcs = tcs
self.tcs.title("简单的贪吃蛇游戏")
self.tcs.resizable(0, 0)#控制最大最小化的,可以不设置,你看着玩
# self.tcs.resizable(True, True)#这样就可以最大化
# 游戏参数 速度、计算方块的大小
self.width = 600
self.height = 400
self.cell_size = 20
self.speed = 150#小就快,难度就增加了,适度点就可以了
self.score = 0
# 弄一个游戏区域
#self.canvas = tk.Canvas(tcs, width=self.width, height=self.height, bg="black")#确定自己喜欢的颜色,游戏界面的大小
self.canvas = tk.Canvas(tcs, width=self.width, height=self.height, bg="blue")
self.canvas.pack()
# 给蛇一个位置,初始下
self.snake = [(10, 10), (9, 10), (8, 10)]#蛇开始的样子,然后你找东西吃
self.direction = "Right"#自动会跑的哦
# 弄点食物吧,我都快饿死了
self.food = self.create_food()
# 统计下得分,显示下给玩家看
self.score_label = tk.Label(tcs, text=f"得分: {self.score}", font=("Arial", 14), fg="white", bg="black")
self.score_label.pack()
# 弄个按钮,游戏结束了可以重新开始
self.btn_frame = tk.Frame(tcs, bg="black")
self.btn_frame.pack(pady=10)
tk.Button(self.btn_frame, text="重新开始", command=self.reset_game, bg="#4CAF50", fg="white").pack(side="left", padx=10)
# 绑定键盘事件
self.tcs.bind("", self.change_direction)
# 开始游戏
self.update()
def create_food(self):
#搞点让tcs吃
while True:
x = random.randint(0, (self.width//self.cell_size)-1)#600//20 横的区间
y = random.randint(0, (self.height//self.cell_size)-1)#横竖就一个坐标了
if (x, y) not in self.snake:#不能蛇出现在蛇的身上,那样导致无先天失败的
return self.canvas.create_rectangle(
x*self.cell_size, y*self.cell_size,
(x+1)*self.cell_size, (y+1)*self.cell_size,#(x1,y1 x2,y2)
fill="red", tags="food"#让贪心的蛇,识别食物还是身体
)
def change_direction(self, event):
#我们用键盘控制移动方向
key = event.keysym
if key in ["Up", "Down", "Left", "Right"]:
if (key == "Up" and self.direction != "Down" or
key == "Down" and self.direction != "Up" or
key == "Left" and self.direction != "Right" or
key == "Right" and self.direction != "Left"):
self.direction = key
def update(self):
#游戏更新
# 计算新头部位置
head_x, head_y = self.snake[0]
dirs = {
"Up": (0, -1),
"Down": (0, 1),
"Left": (-1, 0),
"Right": (1, 0)
}
dx, dy = dirs[self.direction]
new_head = (head_x + dx, head_y + dy)
# 碰撞检测
if (new_head in self.snake or
new_head[0] < 0 or new_head0>= self.width//self.cell_size or
new_head[1] < 0 or new_head1>= self.height//self.cell_size):
self.game_over()
return
self.snake.insert(0, new_head)
# 吃食物检测
food_coords = self.canvas.coords(self.food)
head_coords = [
new_head[0]*self.cell_size,
new_head[1]*self.cell_size,
(new_head[0]+1)*self.cell_size,
(new_head[1]+1)*self.cell_size
]
if head_coords == food_coords:
self.score += 10
self.score_label.config(text=f"得分: {self.score}")
self.canvas.delete("food")
self.food = self.create_food()
else:
self.snake.pop()
# 重新绘制蛇
self.canvas.delete("snake")
for x, y in self.snake:
color = "#4CAF50" if (x, y) == new_head else "#8BC34A"
self.canvas.create_rectangle(
x*self.cell_size, y*self.cell_size,
(x+1)*self.cell_size, (y+1)*self.cell_size,
fill=color, tags="snake"
)
self.tcs.after(self.speed, self.update)
def game_over(self):
# 游戏结束及画面
self.canvas.create_text(
self.width/2, self.height/2,
text="游戏结束!",
fill="white",
font=("Arial", 30, "bold")
)
self.tcs.unbind("")
def reset_game(self):
#重置游戏,方便重玩
self.canvas.delete("all")
self.snake = [(10, 10), (9, 10), (8, 10)]
self.direction = "Right"
self.score = 0
self.score_label.config(text="得分: 0")
self.food = self.create_food()
self.tcs.bind("", self.change_direction)
self.update()
if __name__ == "__main__":
root = tk.Tk() #
root.configure(bg="black")
game = tcs_Game(root)#实例
root.mainloop()
练习代码为主,熟悉、弄懂每个、每行代码的意思才是最主要的。