用python贪吃蛇源代码_python贪吃蛇源码

__author__ = 'ChenYan'

from tkinter import *

import threading

import queue

import time

import random

class GUI(Tk):

'''class GUI use to create the gui'''

def __init__(self queue):

Tk.__init__(self)

self.queue = queue

self.is_game_over = False

self.canvas = Canvas(self width=495 height=305 bg='#FFFFFF')

self.canvas.pack()

self.snake = self.canvas.create_line((00)(00) fill='#00FF00' width=10)

self.snakeh= self.canvas.create_line((00)(00) fill='#FF0000' width=10)

self.food = self.canvas.create_rectangle(0000 fill='#0000FF' outline='#FF0000')

self.point_score = self.canvas.create_text(455 15 fill='Black' text='score:0')

self.queue_handler()

def restart(self):

self.destroy()

main()

def queue_handler(self):

try:

while True:

task = self.queue.get(block=False)

if task.get('game_over'):

self.game_over()

elif task.get('move'):

points = [x for point in task['move'] for x in point]

l=len(points);

self.canvas.coords(self.snake *points)

self.canvas.coords(self.snakehpoints[l-2]points[l-1]points[l-4]points[l-3]);

elif task.get('food'):

self.canvas.coords(self.food *task['food'])

elif task.get('points_score'):

self.canvas.itemconfigure(self.point_score

text='score:{}'.format(task['points_score']))

self.queue.task_done()

except queue.Empty:

if not self.is_game_over:

self.canvas.after(100 self.queue_handler)

def game_over(self):

self.is_game_over = True

self.canvas.create_text(220 150 fill='white'text='Game Over!')

quitbtn = Button(self text='Quit' command=self.destroy)

retbtn = Button(self text='Resume' command=self.restart)

self.canvas.create_window(230 180 anchor=W window=quitbtn)

self.canvas.create_window(200 180 anchor=E window=retbtn)

class Food():

'''class Food use to make food'''

def __init__(self queue):

self.queue = queue

self.make_food()

def make_food(self):

x = random.randrange(5 480 10)

y = random.randrange(5 295 10)

self.position = xy

self.exppos = x-5y-5x+5y+5

self.queue.put({'food':self.exppos})

class Snake(threading.Thread):

'''class Snake use to create snake and response action'''

def __init__(self gui queue round=True speed=10 speed2=30):

threading.Thread.__init__(self)

self.__speedup=False;

self.__sp=speed;

self.__sp2=speed2;

self.__round=round;

self.gui = gui

self.queue = queue

self.daemon = True

self.points_score = 0

self.snake_points = [(39555)(38555)(37555)(36555)(35555)]

self.food = Food(queue)

self.direction = 'Left'

self.start()

def run(self):

if self.gui.is_game_over:

self._delete()

while not self.gui.is_game_over:

self.queue.put({'move':self.snake_points})

time.sleep(0.2)

self.move()

def key_pressed(selfe):

if (e.keysym == "Up" and self.direction == "Down") \

or (e.keysym == "Down" and self.direction == "Up") \

or (e.keysym == "Left" and self.direction == "Right") \

or (e.keysym == "Right" and self.direction == "Left") :

return;

self.direction = e.keysym

def move(self):

new_snake_point = self.calculate_new_coordinates()

if self.food.position == new_snake_point:

add_snake_point = self.calculate_new_coordinates()

self.snake_points.append(add_snake_point)

self.points_score += 1

self.queue.put({'points_score':self.points_score})

self.food.make_food()

else:

self.snake_points.pop(0)

self.check_game_over(new_snake_point)

self.snake_points.append(new_snake_point)

def calculate_new_coordinates(self):

last_xlast_y = self.snake_points[-1]

if self.__speedup:

v=self.__sp2;

else:

v=self.__sp;

if self.direction == 'Up':

new_snake_point = last_xlast_y-v;

elif self.direction == 'Down':

new_snake_point = last_xlast_y+v;

elif self.direction == 'Left':

new_snake_point = last_x-vlast_y;

elif self.direction == 'Right':

new_snake_point = last_x+vlast_y;

if (self.__round):

x = (new_snake_point[0] % 500 + 500) % 500;

y = (new_snake_point[1] % 310 + 310) % 310;

new_snake_point=xy;

return new_snake_point

def check_game_over(self snake_point):

xy = snake_point[0]snake_point[1]

if not 0 <= x <= 500 or not 0 <= y <= 310 or snake_point in self.snake_points:

self.queue.put({'game_over':True})

def speedup(selfe):

self.__speedup=True;

def speeddown(selfe):

self.__speedup=False;

def hah(event):

print("hah");

return 0;

def haha(event):

print("hahh");

return 1;

def main():

q = queue.Queue()

gui = GUI(q)

gui.title("我的贪吃蛇")

snake = Snake(gui q)

gui.bind('' snake.key_pressed)

gui.bind('' snake.key_pressed)

gui.bind('' snake.key_pressed)

gui.bind('' snake.key_pressed)

gui.bind(''snake.speedup);

gui.bind(''snake.speeddown);

gui.mainloop()

if __name__ == '__main__':

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值