问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

100行Python代码,轻松完成贪吃蛇小游戏?

发布网友 发布时间:2022-04-22 13:22

我来回答

1个回答

热心网友 时间:2023-06-25 08:17

以前在远标写过: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"
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 (x,y) in self.body:
self.grid.draw((x,y),self.color)
def display_food(self):
while(self.food.pos in self.body):
self.food.set_pos()
self.food.display()
def move(self):
head = self.body[0]
if self.direction == 'Up':
new = (head[0], head[1]-1)
elif self.direction == 'Down':
new = (head[0], head[1]+1)
elif self.direction == 'Left':
new = (head[0]-1,head[1])
else:
new = (head[0]+1,head[1])
if not self.food.pos == head:
pop = self.body.pop()
self.grid.draw(pop,self.grid.bg)
else:
self.display_food()
self.score += 1
self.body.insert(0,new)
if not new in self.available_grid():
self.status.reverse()
self.gameover = True
else:
self.grid.draw(new,color=self.color)

class SnakeGame(Frame):
def __init__(self,master=None, *args, **kwargs):
Frame.__init__(self, master)
self.master = master
self.grid = Grid(master=master,*args, **kwargs)
self.snake = Snake(self.grid)
self.bind_all("", self.key_release)
self.snake.display()
def run(self):
if not self.snake.status[0] == 'stop':
self.snake.move()
if self.snake.gameover == True:
message = tkMessageBox.showinfo("Game Over", "your score: %d" % self.snake.score)
if message == 'ok':
sys.exit()
self.after(self.snake.speed,self.run)
def key_release(self, event):
key = event.keysym
key_dict = {"Up":"Down","Down":"Up","Left":"Right","Right":"Left"}
if key_dict.has_key(key) and not key == key_dict[self.snake.direction]:
self.snake.change_direction(key)
self.snake.move()
elif key == 'p':
self.snake.status.reverse()

if __name__ == '__main__':
root = Tk()
snakegame = SnakeGame(root)
snakegame.run()
snakegame.mainloop()
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
自离后能补办离职证明吗? 我是家里自离的,可以补办辞职手续吗 急救!!!狗狗受凉吃什么就吐什么,该吃什么药 狗狗受凉呕吐吃什么药好得快 公告栏标题里的字如何设定字体及大小 公告栏怎移动啊 我的公告栏 如何设置 怎么让QQ拍拍公告栏文字不滚动 在滚动的公告栏中鼠标放上去时就停止,这种效果怎么做?谢谢了 王者荣耀铂金1和黄金3双排排到的是什么段位的 海参的营养价值和功效如何做海参? Python编程编写的贪吃蛇小游戏效果演示 如何用Python写一个贪吃蛇AI – 码农网 海参的营养价值和功效百度 怎样从零开始用 Python 写一个贪吃蛇 如何用Python写一个贪吃蛇AI python怎样做贪吃蛇 4寸照片尺寸多少?像素多少? 裁4寸照片的分辨率是254和300,没有其他的吗? 怎么把照片压缩成4寸 4寸相片怎么剪切 4寸照片有的电脑打不了 ps怎么设计图片大小ps怎么设计图片大小一样 如何用PS把照片改成4寸大小的? 四寸电子照片怎么修改啊 尺寸大小 像素啥的 怎么用adobe photoshop做4寸照片 影楼裁4寸照片的尺寸是多少 怎样用PS把图片改成4寸照片? 送女朋友生日礼物送什么好 生日礼物的礼盒怎么折 海参的营养价值及功效作用 python贪吃蛇 怎样实现"蛇"的动画爬行 海参的营养价值及功效与作用 404 Not Found 海参营养成分和功效 python怎么不用pygame就能写出贪吃蛇 海参的功效和营养是什么 如何用Python写一个贪吃蛇AI-CSDN论坛 python贪吃蛇游戏的_init_(self)中 self没定义 海参斑鱼的营养功效 海参和鲍鱼的营养价值和功效 如何用Python寫一個貪吃蛇AI 鲍鱼和海参营养功效与作用 求用vbs编写的贪吃蛇源代码 C语言的贪吃蛇源代码 海参和鲍鱼的营养成分有那些 海参有什么营养价值 processing怎么编游戏贪吃蛇 乾山兼戌屋场风水怎样 亥兼壬屋场风水好坏之分有哪些