国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python寫的貪吃蛇游戲例子

2020-02-23 05:28:46
字體:
來源:轉載
供稿:網友

第一次用Python寫這種比較實用且好玩的東西,權當練手吧

游戲說明:

* P鍵控制“暫停/開始”
* 方向鍵控制貪吃蛇的方向

源代碼如下:
代碼如下:
from Tkinter import *
import tkMessageBox,sys
from random import randint

class Grid(object):
    def __init__(self,master=None,window_width=800,window_height=600,grid_width=50,offset=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 = "#EBEBEB"
        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_width,fill=color,outline=self.bg)
    def grid_list(self):
        grid_list = []
        for y in range(0,self.grid_y):
            for x in range(0,self.grid_x):
                grid_list.append((x,y))
        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(0,self.grid.grid_x - 1)
        y = randint(0,self.grid.grid_y - 1)
        self.pos =  (x, y)   
    def display(self):
        self.grid.draw(self.pos,self.color)

class Snake(object):
    def __init__(self, Grid):
        self.grid = Grid
        self.body = [(10,6),(10,7),(10,8)]
        self.direction = "Up"

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五大连池市| 杂多县| 平顺县| 贵阳市| 洪雅县| 宁德市| 青浦区| 开远市| 长阳| 清镇市| 定远县| 田阳县| 肇庆市| 宁海县| 峨山| 砚山县| 邛崃市| 盘山县| 抚顺县| 乐清市| 罗甸县| 聂拉木县| 景洪市| 莎车县| 克山县| 崇阳县| 西和县| 德格县| 冕宁县| 天等县| 遂溪县| 新邵县| 澜沧| 理塘县| 庆元县| 青铜峡市| 泰顺县| 台南市| 齐河县| 定日县| 宁晋县|