用python自带的tkinter做游戏(一)—— 贪吃蛇 篇



  本人新手,刚自学python满半年,现分享下心得,希望各位老手能指点一二,也希望和我一样的新手能共勉,谢谢~


  大家都知道用python做游戏用的比较多的是pygame。其实做游戏最重要的是思路,用什么库并不重要,特别是些小游戏,用自带的tkinter完全能胜任。虽然tkinter存在着各种的不便,但毕竟是自带的,用起来也简单,非常适合新手入门。


  


  废话少说,开始正题。选贪吃蛇做入门练手游戏最适合不过了,没玩过的也应该见过吧?先随我一起看看游戏的设计思路~


  贪吃蛇游戏画面很简单,若干个方格组成一个矩阵,颜色最低三色即可,空地,食物和蛇身。我这里用了五种颜色,增加了四周的墙和蛇头(蛇头和蛇身颜色分开,更直观些)。


  其实贪吃蛇这个游戏控制的就是蛇头的走向,蛇身其实就是蛇头的运动轨迹,只要记录下蛇头的每一步坐标,然后根据所需要的长度,就可以拼凑出蛇身来。有个这个思路,这个游戏制作起来就简单了。


  


  


好了,开始了, 先加载需要的库。


import tkinter as tk
from tkinter.messagebox import showinfo
import random

showinfo是个弹窗,暂停游戏时用。
random都知道,生成随机数。

然后设置下游戏参数。

class Snake():
    """ 贪吃蛇游戏  """
    def __init__(self):
        """ 游戏参数设置 """
        global body_len, FPS
        self.len        = 3                   # 蛇身初始长度(最小设定值为1,不包括蛇头)
        body_len        = self.len            # 蛇身当前长度
        FPS             = 120                 # 蛇的移动速度(单位毫秒)
        self.row_cells  = 22                  # 一行多少个单元格(含边框)
        self.col_cells  = 22                  # 一共多少行单元格(含边框)
        self.canvas_bg  = 'white'             # 游戏背景色
        self.cell_size  = 25                  # 方格单元格大小
        self.cell_gap   = 1                   # 方格间距
        self.frame_x    = 15                  # 左右边距
        self.frame_y    = 15                  # 上下边距
        self.win_w_plus = 220                 # 窗口右边额外多出的宽度
        self.color_dict = {0:  '#d7d7d7',     # 0表示空白
                           1:   'yellow',     # 1代表蛇头
                           2:  '#009700',     # 2代表蛇身
                           3:      'red',     # 3代表食物
                           4:  '#808080'}     # 4代表墙

body_len不要设置为0,不然连蛇头都没有了。
  
之前提到的五种颜色,放置在color_dict这个字典中,方便调用。
然后游戏窗口设置成居中。

def window_center(self,window,w_size,h_size):
        """ 窗口居中 """
        screenWidth  =  window.winfo_screenwidth()  # 获取显示区域的宽度
        screenHeight = window.winfo_screenheight()  # 获取显示区域的高度
        left =  (screenWidth - w_size) // 2
        top  = (screenHeight - h_size) // 2
        window.geometry("%dx%d+%d+%d" % (w_size, h_size, left, top))

没学过tkinter的同学也可以简单了解一下tk的语法,之后就显示窗体了。

def run_game(self):
        """ 开启游戏 """
        global window
        
        window = tk.Tk()
        window.focus_force() # 主窗口焦点
        window.title('Snake')
        
        win_w_size = self.row_cells * self.cell_size + self.frame_x*2 + self.win_w_plus 
        win_h_size = self.col_cells * self.cell_size + self.frame_y*2
        self.window_center(window,win_w_size,win_h_size)
        
        txt_lable = tk.Label(window, text=
                              "方向键移动,或者"
                             +"\n字母键WSAD移动"
                             +"\n(大小写均可)"
                             +"\n"
                             +"\n空格键暂停"
                             +"\n作者:Juni Zhu"
                             +"\n微信:znix1116",
                             font=('Yahei', 15),anchor="ne", justify="left")
        
        txt_lable.place(x = self.cell_size * self.col_cells + self.cell_size*2, 
                        y = self.cell_size*6)
        
        self.create_canvas()
        self.game_start()

窗体架构完了,开始游戏内容了。先创建个游戏地图,既然是若干个方格所组成的矩阵,那每个方格就对应一个坐标吧。

def create_map(self):
        """ 创建地图列表 """
        global game_map
        
        game_map = [] 
        for i in range(0,self.col_cells):
            game_map.append([])
        for i in range(0,self.col_cells):
           for j in range(0,self.row_cells):
              game_map[i].append(j)   
              game_map[i][j] = 0  # 生成一个全是0的空数列

有了这张game_map就可以开始作画了,先画上四周的边框。

def create_wall(self):
        """ 绘制边框 """
        for i in range(0,self.row_cells-1):
            game_map[0][i] = 4
            game_map[self.col_cells-1][i] = 4
        
        for i in range(0,self.col_cells-1):
            game_map[i][0] = 4
            game_map[i][self.row_cells-1] = 4
        game_map[-1][-1] = 4

边框有了,接着把蛇头画出来。

def create_snake(self):
        """ 创建蛇头和蛇身 """
        global snake_body
        snake_body = [[self.col_cells // 2 , self.row_cells // 2]] # 蛇头出生地在地图的中央
        game_map[snake_body[0][0]][snake_body[0][1]] = 1  # 蛇头上色,颜色为定义的1

snake_body是个列表,初始只有蛇头的坐标,之后的蛇身因为蛇头还没开始走动,所以还没有生成。

蛇头有了,接下来就是食物了,这回随机数派上用处了。

def create_food(self):
        """ 创建食物 """
        global food_xy
        
        food_xy = [0,0]
        food_xy[1] = random.randint(1, self.row_cells-2)
        food_xy[0] = random.randint(1, self.col_cells-2)
        
        while game_map[food_xy[0]][food_xy[1]] != 0:
            food_xy[0] = random.randint(1,self.row_cells-2)
            food_xy[1] = random.randint(1,self.col_cells-2)

        game_map[food_xy[0]][food_xy[1]] = 3

食物必须得出现在空地上,不然嵌在墙里也就算了,直接在蛇身上冒出来总不合适吧。。。

现在,边框,蛇头和食物都定义好了,我们现在加载看看效果。

def create_canvas(self):
        """ 创建画布 """
        global canvas
        canvas_h = self.cell_size * self.col_cells + self.frame_y*2
        canvas_w = self.cell_size * self.row_cells + self.frame_x*2
        
        canvas = tk.Canvas(window,  bg = self.canvas_bg, 
                                height = canvas_h,
                                 width = canvas_w,
                           highlightthickness = 0)
        canvas.place(x=0,y=0)
        
        
    def create_cells(self):
        """ 创建单元格 """
        for y in range(0,self.col_cells):
            for x in range(0,self.row_cells):
                a = self.frame_x + self.cell_size*x
                b = self.frame_y + self.cell_size*y
                c = self.frame_x + self.cell_size*(x+1)
                d = self.frame_y + self.cell_size*(y+1)
                e = self.canvas_bg
                f = self.cell_gap
                g = self.color_dict[game_map[y][x]]
                canvas.itemconfig(canvas.create_rectangle(a,b,c,d, outline=e, width=f, fill=g),fill=g)

用canvas创建方格,颜色填充根据颜色字典来。
如果现在把以上这些函数连起来运行的话,就可以看到画面中央有个黄色的蛇头静静的望着红色的食物一动不动。那怎么让蛇动起来呢?
蛇头一旦动起来,蛇身就会根据蛇头的运行轨迹而出现。所以在蛇运动起来之前,我们先要想办法知道蛇头的每一步坐标。

python运行游戏需要什么扩展库 python自带的游戏_python

def snake_xy(self):
        """ 获取蛇头坐标 """
        global head_x, head_y
        xy = []
        for i in range(0,self.col_cells):
                try: # 查找数值为1的坐标,没有就返回0。为防止在0列,先加上1,最后再减去。
                    x = game_map[i].index(1) + 1 
                except:
                    x = 0
                xy.append(x)
        head_x = max(xy)
        head_y = xy.index(head_x)
        head_x = head_x - 1 # 之前加1,现在减回

这样就得到了蛇头的XY轴的坐标,那就让它动起来!

def move_snake(self,event):
        """ 蛇体移动 """
        def move_key(a,b,c,d):  # 记录按键的方向,1上2下3左4右
            direction = event.keysym
                    
            if   head_x != snake_body[-1][1]:
                if(direction == a):
                    dd[0] = 1
                if(direction == b):
                    dd[0] = 2
            else:
                if(direction == c):
                    dd[0] = 3
                if(direction == d):
                    dd[0] = 4
                  
            if   head_y != snake_body[-1][0]:
                if(direction == c):
                    dd[0] = 3
                if(direction == d):
                    dd[0] = 4
            else:
                if(direction == a):
                    dd[0] = 1
                if(direction == b):
                    dd[0] = 2

        def pause_key(key):
            """ 暂停键 """
            global loop
            direction = event.keysym
            if(direction == key):
                loop = 0
                showinfo('暂停','按确定键继续')
                loop = 1
                window.after(FPS, self.game_loop)
                    
        move_key('w','s','a','d')
        move_key('W','S','A','D')
        move_key('Up','Down','Left','Right')
        pause_key('space')

四个方向键只是记录方位,修改dd[0]的值,1~4分别为上下左右。顺便把暂停键空格也定义上,一按空格键,loop值就等于0,停止游戏界面刷新。

之前说过,蛇身就是蛇头的运动轨迹,既然蛇头动起来了,蛇身的数据也就得到了。

def snake_record(self):
        """ 蛇身 """ # 记录蛇头运行轨迹,生成蛇身
        global body_len,snake_body
        
        temp = []
        temp.append(head_y)
        temp.append(head_x)
        snake_body.append(temp) 
        
        if snake_body[-1] == snake_body[-2]: 
            del snake_body[-1]
        
        if [head_y,head_x] == food_xy: # 碰到食物身体加长,并再随机生成一个食物
            body_len = body_len + 1
            self.create_food()
        elif len(snake_body) > body_len:  # 限制蛇身长度,不超过设定值
            game_map[snake_body[0][0]][snake_body[0][1]] = 0
            del snake_body[0]

这样所有的素材都齐全了,大功快要告成了!

def game_over(self):
        def over():
            global body_len
            showinfo('Game Over','再来一局')
            body_len = self.len
            self.game_start()
        
        if [head_y,head_x] in snake_body[0:-2]:
            over()
        if head_x == self.row_cells - 1 or head_x == 0:
            over()
        if head_y == self.col_cells - 1 or head_y == 0:
            over()

    def auto_move(self):
        """ 自动前进 """
        def move(d,x,y):
            if dd[0] == d:  # 根据方向值来决定走向
                game_map[head_y + x][head_x + y] = 1
                game_map[head_y + 0][head_x + 0] = 2
            
        move( 1, -1,  0 )
        move( 2,  1,  0 )
        move( 3,  0, -1 )
        move( 4,  0,  1 )

根据游戏设定,蛇头撞墙或者撞到自身了就Game Over。位置的移动根据蛇头的坐标X轴或Y轴加一或减一就行。蛇头前方的格子变成蛇头(1),然后原本蛇头的位置变为蛇身(2)。

现在让窗体自动刷新起来,这样蛇头才会自动前进。
并把所有的函数串起来。

def game_loop(self):
        """ 游戏循环刷新 """
        global loop_id
        self.snake_record()
        self.auto_move()
        self.snake_xy()
        canvas.delete('all') # 清除canvas
        self.create_cells()
        self.game_over()
        if loop == 1:
            loop_id  = window.after(FPS, self.game_loop)
        
        
    def game_start(self):
        """  """
        global window, backup_map, dd, loop
        loop = 1 # 暂停标记,1为开启,0为暂停
        dd = [0] # 记录按键方向
        self.create_map()
        self.create_wall()
        self.create_snake()
        self.create_food()
        window.bind('<Key>', self.move_snake)
        self.snake_xy()
        self.game_loop()
        
        def close_w():
            global loop
            loop = 0
            window.after_cancel(loop_id)
            window.destroy()
            
        window.protocol('WM_DELETE_WINDOW', close_w)
        window.mainloop()

loop值为1时,window.after起作用,让游戏界面根据FPS值不断刷新。

至此,这个贪吃蛇的游戏算是全部完成了,该有的功能都有,就缺个计分功能。
这个难度也不大,我就懒得添加了。

python运行游戏需要什么扩展库 python自带的游戏_tkinter_02

下期预告:
用python自带的tkinter做游戏(二)—— 俄罗斯方块 篇
(本人微信:znix1116,欢迎来交流)

最后附上完整代码:

2022.03.25 更新下代码,修复些小BUG(退出游戏后再次运行游戏会有提示报错)。

2022.05.31 修复一个BUG(游戏失败后,再次游戏后蛇身没有恢复成初始长度)。

2022.06.03 修复一处小BUG(游戏失败后再次游戏,退出游戏的时候会提示报错)。

2023.04.03 无聊小更新一下,把global的定义全部取消了。

# -*- coding: utf-8 -*-
"""
Created on Sat Mar 27 17:45:21 2021

@author: Juni Zhu
"""

import tkinter as tk
from tkinter.messagebox import showinfo
import random


class Snake():
    """ 贪吃蛇游戏  """
    def __init__(self):
        """ 游戏参数设置 """
        
        self.window     = None                # 实例化的窗体
        self.canvas     = None                # 实例化的画布
        self.loop       = 0                   # 暂停标记,1为开启,0为暂停
        self.loop_id    = None                # 实例化loop,用来取消循环
        
        self.game_map   = []                  # 整个游戏的地图
        self.snake_body = []                  # 蛇身的坐标集
        self.food_xy    = []                  # 食物的坐标
        self.head_x     = 0                   # 蛇头的X坐标
        self.head_y     = 0                   # 蛇头的Y坐标
        self.dd         = [0]                 # 记录按键方向
        
        self.len        = 3                   # 蛇身初始长度(最小设定值为1,不包括蛇头)
        self.body_len   = self.len            # 蛇身当前长度
        self.FPS        = 120                 # 蛇的移动速度(单位毫秒)
        self.row_cells  = 22                  # 一行多少个单元格(含边框)
        self.col_cells  = 22                  # 一共多少行单元格(含边框)
        self.canvas_bg  = 'white'             # 游戏背景色
        self.cell_size  = 25                  # 方格单元格大小
        self.cell_gap   = 1                   # 方格间距
        self.frame_x    = 15                  # 左右边距
        self.frame_y    = 15                  # 上下边距
        self.win_w_plus = 220                 # 窗口右边额外多出的宽度
        
        self.color_dict = {0:  '#d7d7d7',     # 0表示空白
                           1:   'yellow',     # 1代表蛇头
                           2:  '#009700',     # 2代表蛇身
                           3:      'red',     # 3代表食物
                           4:  '#808080'}     # 4代表墙
        
        self.run_game()

    
    def window_center(self,window,w_size,h_size):
        """ 窗口居中 """
        screenWidth  =  window.winfo_screenwidth()  # 获取显示区域的宽度
        screenHeight = window.winfo_screenheight()  # 获取显示区域的高度
        left =  (screenWidth - w_size) // 2
        top  = (screenHeight - h_size) // 2
        window.geometry("%dx%d+%d+%d" % (w_size, h_size, left, top))
        
        
    def create_map(self):
        """ 创建地图列表 """
        self.game_map = [] 
        for i in range(0,self.col_cells):
            self.game_map.append([])
        for i in range(0,self.col_cells):
           for j in range(0,self.row_cells):
              self.game_map[i].append(j)   
              self.game_map[i][j] = 0  # 生成一个全是0的空数列


    def create_wall(self):
        """ 绘制边框 """
        for i in range(0,self.row_cells-1):
            self.game_map[0][i] = 4
            self.game_map[self.col_cells-1][i] = 4
        
        for i in range(0,self.col_cells-1):
            self.game_map[i][0] = 4
            self.game_map[i][self.row_cells-1] = 4
        self.game_map[-1][-1] = 4
        

    def create_canvas(self):
        """ 创建画布 """
        canvas_h = self.cell_size * self.col_cells + self.frame_y*2
        canvas_w = self.cell_size * self.row_cells + self.frame_x*2
        
        self.canvas = tk.Canvas(self.window,  bg = self.canvas_bg, 
                                height = canvas_h,
                                 width = canvas_w,
                           highlightthickness = 0)
        self.canvas.place(x=0,y=0)
        
        
    def create_cells(self):
        """ 创建单元格 """
        for y in range(0,self.col_cells):
            for x in range(0,self.row_cells):
                a = self.frame_x + self.cell_size*x
                b = self.frame_y + self.cell_size*y
                c = self.frame_x + self.cell_size*(x+1)
                d = self.frame_y + self.cell_size*(y+1)
                e = self.canvas_bg
                f = self.cell_gap
                g = self.color_dict[self.game_map[y][x]]
                self.canvas.itemconfig(self.canvas.create_rectangle(a,b,c,d, outline=e, width=f, fill=g),fill=g)
        

    def create_snake(self):
        """ 创建蛇头和蛇身 """
        self.snake_body = [[self.col_cells // 2 , self.row_cells // 2]] # 蛇头出生地在地图的中央
        self.game_map[self.snake_body[0][0]][self.snake_body[0][1]] = 1  # 蛇头上色,颜色为定义的1


    def create_food(self):
        """ 创建食物 """
        
        self.food_xy = [0,0]
        self.food_xy[1] = random.randint(1, self.row_cells-2)
        self.food_xy[0] = random.randint(1, self.col_cells-2)
        
        while self.game_map[self.food_xy[0]][self.food_xy[1]] != 0:
            self.food_xy[0] = random.randint(1,self.row_cells-2)
            self.food_xy[1] = random.randint(1,self.col_cells-2)

        self.game_map[self.food_xy[0]][self.food_xy[1]] = 3


    def snake_xy(self):
        """ 获取蛇头坐标 """
        xy = []
        for i in range(0,self.col_cells):
                try: # 查找数值为1的坐标,没有就返回0。为防止在0列,先加上1,最后再减去。
                    x = self.game_map[i].index(1) + 1 
                except:
                    x = 0
                xy.append(x)
        self.head_x = max(xy)
        self.head_y = xy.index(self.head_x)
        self.head_x = self.head_x - 1 # 之前加1,现在减回
        
        
    def move_snake(self,event):
        """ 蛇体移动 """
        def move_key(a,b,c,d):  # 记录按键的方向,1上2下3左4右
            direction = event.keysym
                    
            if   self.head_x != self.snake_body[-1][1]:
                if(direction == a):
                    self.dd[0] = 1
                if(direction == b):
                    self.dd[0] = 2
            else:
                if(direction == c):
                    self.dd[0] = 3
                if(direction == d):
                    self.dd[0] = 4
                  
            if   self.head_y != self.snake_body[-1][0]:
                if(direction == c):
                    self.dd[0] = 3
                if(direction == d):
                    self.dd[0] = 4
            else:
                if(direction == a):
                    self.dd[0] = 1
                if(direction == b):
                    self.dd[0] = 2

        def pause_key(key):
            """ 暂停键 """
            direction = event.keysym
            if(direction == key):
                self.loop = 0
                showinfo('暂停','按确定键继续')
                self.loop = 1
                self.window.after(self.FPS, self.game_loop)
                    
        move_key('w','s','a','d')
        move_key('W','S','A','D')
        move_key('Up','Down','Left','Right')
        pause_key('space')
        
        
    def game_over(self):
        
        def over():
            showinfo('Game Over','再来一局')
            self.body_len = self.len
            self.game_start()
        
        if [self.head_y,self.head_x] in self.snake_body[0:-2]:
            over()
        if self.head_x == self.row_cells - 1 or self.head_x == 0:
            over()
        if self.head_y == self.col_cells - 1 or self.head_y == 0:
            over()
        
        
    def snake_record(self):
        """ 蛇身 """ # 记录蛇头运行轨迹,生成蛇身
        
        temp = []
        temp.append(self.head_y)
        temp.append(self.head_x)
        self.snake_body.append(temp) 
        
        if self.snake_body[-1] == self.snake_body[-2]: 
            del self.snake_body[-1]
        
        if [self.head_y,self.head_x] == self.food_xy: # 碰到食物身体加长,并再随机生成一个食物
            self.body_len += 1
            self.create_food()
        elif len(self.snake_body) > self.body_len:  # 限制蛇身长度,不超过设定值
            self.game_map[self.snake_body[0][0]][self.snake_body[0][1]] = 0
            del self.snake_body[0]
            

    def auto_move(self):
        """ 自动前进 """
        def move(d,x,y):
            if self.dd[0] == d:  # 根据方向值来决定走向
                self.game_map[self.head_y + x][self.head_x + y] = 1
                self.game_map[self.head_y + 0][self.head_x + 0] = 2
            
        move( 1, -1,  0 )
        move( 2,  1,  0 )
        move( 3,  0, -1 )
        move( 4,  0,  1 )


    def game_loop(self):
        """ 游戏循环刷新 """
        self.snake_record()
        self.auto_move()
        self.snake_xy()
        self.canvas.delete('all') # 清除canvas
        self.create_cells()
        self.game_over()
        if self.loop == 1:
            self.loop_id  = self.window.after(self.FPS, self.game_loop)
        
        
    def game_start(self):
        """  """
        self.loop = 1 # 暂停标记,1为开启,0为暂停
        self.dd = [0] # 记录按键方向
        self.create_map()
        self.create_wall()
        self.create_snake()
        self.create_food()
        self.window.bind('<Key>', self.move_snake)
        self.snake_xy()
        self.game_loop()
        
        def close_w():
            self.loop = 0
            self.window.after_cancel(self.loop_id)
            self.window.destroy()
            
        self.window.protocol('WM_DELETE_WINDOW', close_w)
        self.window.mainloop()
        
        
    def run_game(self):
        """ 开启游戏 """
        
        self.window = tk.Tk()
        self.window.focus_force() # 主窗口焦点
        self.window.title('Snake')
        
        win_w_size = self.row_cells * self.cell_size + self.frame_x*2 + self.win_w_plus 
        win_h_size = self.col_cells * self.cell_size + self.frame_y*2
        self.window_center(self.window,win_w_size,win_h_size)
        
        txt_lable = tk.Label(self.window, text=
                              "方向键移动,或者"
                             +"\n字母键WSAD移动"
                             +"\n(大小写均可)"
                             +"\n"
                             +"\n空格键暂停"
                             +"\n作者:Juni Zhu"
                             +"\n微信:znix1116",
                             font=('Yahei', 15),anchor="ne", justify="left")
        
        txt_lable.place(x = self.cell_size * self.col_cells + self.cell_size*2, 
                        y = self.cell_size*6)
        
        self.create_canvas()
        self.game_start()
        
        
if __name__ == '__main__':
    
    Snake()