资源简介

基于python2的源码,python3无法使用 贪吃蛇小游戏源码

资源截图

代码片段和文件信息

from Tkinter import *
import tkMessageBoxsys
from random import randint
 
class Grid(object):
    def __init__(selfmaster=Nonewindow_width=800window_height=600grid_width=50offset=10):
        self.height = window_height
        self.width = window_width
        self.grid_width = grid_width
        self.offset = offset
        self.grid_x = self.width/self.grid_width
        self.grid_y = self.height/self.grid_width
        self.bg = “#EBEBEE“
        self.canvas = Canvas(master width=self.width+2*self.offset height=self.height+2*self.offset bg=self.bg)
        self.canvas.pack()
        self.grid_list()
    def draw(self pos color):
        x = pos[0]*self.grid_width + self.offset
        y = pos[1]*self.grid_width + self.offset
        self.canvas.create_rectangle(x y x+self.grid_width y+self.grid_widthfill=coloroutline=self.bg)
    def grid_list(self):
        grid_list = []
        for y in range(0self.grid_y):
            for x in range(0self.grid_x):
                grid_list.append((xy))
        self.grid_list = grid_list
 
class Food(object):
    def __init__(self Grid):
        self.grid = Grid
        self.color = “#23D978“        
        self.set_pos()
    def set_pos(self):
        x = randint(0self.grid.grid_x - 1)
        y = randint(0self.grid.grid_y - 1)
        self.pos =  (x y)    
    def display(self):
        self.grid.draw(self.posself.color)
 
class Snake(object):
    def __init__(self Grid):
        self.grid = Grid
        self.body = [(106)(107)(108)]
        self.direction = “Up“
        self.status = [‘run‘‘stop‘]
        self.speed = 300
        self.color = “#5FA8D9“        
        self.food = Food(self.grid)
        self.display_food()
        self.gameover = False
        self.score = 0
    def available_grid(self):
        return [i for i in self.grid.grid_list if i not in self.body[2:]]
    def change_direction(self direction):
        self.direction = direction
    def display(self):
        for (xy) in self.body:
            self.grid.dr

评论

共有 条评论