鱼C论坛

 找回密码
 立即注册
查看: 753|回复: 7

[已解决]求分享一下贪吃蛇的图片资源

[复制链接]
发表于 2020-5-15 16:48:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

蟹蟹
18188223639@163.com
最佳答案
2020-5-15 17:05:09
  1. from tkinter import *
  2. import threading
  3. import queue
  4. import time
  5. import random


  6. class GUI(Tk):
  7.     '''class GUI use to create the gui'''

  8.     def __init__(self, queue):
  9.         Tk.__init__(self)
  10.         self.queue = queue
  11.         self.is_game_over = False
  12.         self.canvas = Canvas(self, width=495, height=305, bg='#000000')
  13.         self.canvas.pack()
  14.         self.snake = self.canvas.create_line((0,0),(0,0), fill='#FFFF00', width=10)
  15.         self.food = self.canvas.create_rectangle(0,0,0,0, fill='#00FF00', outline='#00FF00')
  16.         self.point_score = self.canvas.create_text(455, 15, fill='white', text='score:0')
  17.         self.queue_handler()

  18.     def restart(self):
  19.         self.destroy()
  20.         main()

  21.     def queue_handler(self):
  22.         try:
  23.             while True:
  24.                 task = self.queue.get(block=False)
  25.                 if task.get('game_over'):
  26.                     self.game_over()
  27.                 elif task.get('move'):
  28.                     points = [x for point in task['move'] for x in point]
  29.                     self.canvas.coords(self.snake, *points)
  30.                 elif task.get('food'):
  31.                     self.canvas.coords(self.food, *task['food'])
  32.                 elif task.get('points_score'):
  33.                     self.canvas.itemconfigure(self.point_score,
  34.                                               text='score:{}'.format(task['points_score']))
  35.                     self.queue.task_done()
  36.         except queue.Empty:
  37.             if not self.is_game_over:
  38.                 self.canvas.after(100, self.queue_handler)

  39.     def game_over(self):
  40.         self.is_game_over = True
  41.         self.canvas.create_text(220, 150, fill='white',text='Game Over!')
  42.         quitbtn = Button(self, text='退出', command=self.destroy)
  43.         retbtn = Button(self, text='重来', command=self.restart)
  44.         self.canvas.create_window(230, 180, anchor=W, window=quitbtn)
  45.         self.canvas.create_window(200, 180, anchor=E, window=retbtn)


  46. class Food():
  47.     '''class Food use to make food'''

  48.     def __init__(self, queue):
  49.         self.queue = queue
  50.         self.make_food()

  51.     def make_food(self):
  52.         x = random.randrange(5, 480, 10)
  53.         y = random.randrange(5, 295, 10)
  54.         self.position = x,y
  55.         self.exppos = x-5,y-5,x+5,y+5
  56.         self.queue.put({'food':self.exppos})

  57. class Snake(threading.Thread):
  58.     '''class Snake use to create snake and response action'''

  59.     def __init__(self, gui, queue):
  60.         threading.Thread.__init__(self)
  61.         self.gui = gui
  62.         self.queue = queue
  63.         self.daemon = True
  64.         self.points_score = 0
  65.         self.snake_points = [(495,55),(485,55),(475,55),(465,55),(455,55)]
  66.         self.food = Food(queue)
  67.         self.direction = 'Left'
  68.         self.start()

  69.     def run(self):
  70.         if self.gui.is_game_over:
  71.             self._delete()
  72.         while not self.gui.is_game_over:
  73.             self.queue.put({'move':self.snake_points})
  74.             time.sleep(0.2)
  75.             self.move()

  76.     def key_pressed(self,e):
  77.         self.direction = e.keysym

  78.     def move(self):
  79.         new_snake_point = self.calculate_new_coordinates()
  80.         if self.food.position == new_snake_point:
  81.             add_snake_point = self.calculate_new_coordinates()
  82.             self.snake_points.append(add_snake_point)
  83.             self.points_score += 1
  84.             self.queue.put({'points_score':self.points_score})
  85.             self.food.make_food()
  86.         else:
  87.             self.snake_points.pop(0)
  88.             self.check_game_over(new_snake_point)
  89.             self.snake_points.append(new_snake_point)

  90.     def calculate_new_coordinates(self):
  91.         last_x,last_y = self.snake_points[-1]
  92.         if self.direction == 'Up':
  93.             new_snake_point = last_x,last_y-10
  94.         elif self.direction == 'Down':
  95.             new_snake_point = last_x,last_y+10
  96.         elif self.direction == 'Left':
  97.             new_snake_point = last_x-10,last_y
  98.         elif self.direction == 'Right':
  99.             new_snake_point = last_x+10,last_y
  100.         return new_snake_point

  101.     def check_game_over(self, snake_point):
  102.         x,y = snake_point[0],snake_point[1]
  103.         if not -5 < x < 505 or not -5 < y < 315 or snake_point in self.snake_points:
  104.             self.queue.put({'game_over':True})


  105. def main():
  106.     q = queue.Queue()
  107.     gui = GUI(q)
  108.     gui.title("贪吃蛇")
  109.     snake = Snake(gui, q)
  110.     gui.bind('<Key-Left>', snake.key_pressed)
  111.     gui.bind('<Key-Right>', snake.key_pressed)
  112.     gui.bind('<Key-Up>', snake.key_pressed)
  113.     gui.bind('<Key-Down>', snake.key_pressed)
  114.     gui.mainloop()


  115. if __name__ == '__main__':
  116.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-15 16:53:04 | 显示全部楼层
有的不用图片就行。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-15 17:00:36 | 显示全部楼层
我记得
小甲鱼没有讲过贪吃蛇
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-15 17:05:09 | 显示全部楼层    本楼为最佳答案   
  1. from tkinter import *
  2. import threading
  3. import queue
  4. import time
  5. import random


  6. class GUI(Tk):
  7.     '''class GUI use to create the gui'''

  8.     def __init__(self, queue):
  9.         Tk.__init__(self)
  10.         self.queue = queue
  11.         self.is_game_over = False
  12.         self.canvas = Canvas(self, width=495, height=305, bg='#000000')
  13.         self.canvas.pack()
  14.         self.snake = self.canvas.create_line((0,0),(0,0), fill='#FFFF00', width=10)
  15.         self.food = self.canvas.create_rectangle(0,0,0,0, fill='#00FF00', outline='#00FF00')
  16.         self.point_score = self.canvas.create_text(455, 15, fill='white', text='score:0')
  17.         self.queue_handler()

  18.     def restart(self):
  19.         self.destroy()
  20.         main()

  21.     def queue_handler(self):
  22.         try:
  23.             while True:
  24.                 task = self.queue.get(block=False)
  25.                 if task.get('game_over'):
  26.                     self.game_over()
  27.                 elif task.get('move'):
  28.                     points = [x for point in task['move'] for x in point]
  29.                     self.canvas.coords(self.snake, *points)
  30.                 elif task.get('food'):
  31.                     self.canvas.coords(self.food, *task['food'])
  32.                 elif task.get('points_score'):
  33.                     self.canvas.itemconfigure(self.point_score,
  34.                                               text='score:{}'.format(task['points_score']))
  35.                     self.queue.task_done()
  36.         except queue.Empty:
  37.             if not self.is_game_over:
  38.                 self.canvas.after(100, self.queue_handler)

  39.     def game_over(self):
  40.         self.is_game_over = True
  41.         self.canvas.create_text(220, 150, fill='white',text='Game Over!')
  42.         quitbtn = Button(self, text='退出', command=self.destroy)
  43.         retbtn = Button(self, text='重来', command=self.restart)
  44.         self.canvas.create_window(230, 180, anchor=W, window=quitbtn)
  45.         self.canvas.create_window(200, 180, anchor=E, window=retbtn)


  46. class Food():
  47.     '''class Food use to make food'''

  48.     def __init__(self, queue):
  49.         self.queue = queue
  50.         self.make_food()

  51.     def make_food(self):
  52.         x = random.randrange(5, 480, 10)
  53.         y = random.randrange(5, 295, 10)
  54.         self.position = x,y
  55.         self.exppos = x-5,y-5,x+5,y+5
  56.         self.queue.put({'food':self.exppos})

  57. class Snake(threading.Thread):
  58.     '''class Snake use to create snake and response action'''

  59.     def __init__(self, gui, queue):
  60.         threading.Thread.__init__(self)
  61.         self.gui = gui
  62.         self.queue = queue
  63.         self.daemon = True
  64.         self.points_score = 0
  65.         self.snake_points = [(495,55),(485,55),(475,55),(465,55),(455,55)]
  66.         self.food = Food(queue)
  67.         self.direction = 'Left'
  68.         self.start()

  69.     def run(self):
  70.         if self.gui.is_game_over:
  71.             self._delete()
  72.         while not self.gui.is_game_over:
  73.             self.queue.put({'move':self.snake_points})
  74.             time.sleep(0.2)
  75.             self.move()

  76.     def key_pressed(self,e):
  77.         self.direction = e.keysym

  78.     def move(self):
  79.         new_snake_point = self.calculate_new_coordinates()
  80.         if self.food.position == new_snake_point:
  81.             add_snake_point = self.calculate_new_coordinates()
  82.             self.snake_points.append(add_snake_point)
  83.             self.points_score += 1
  84.             self.queue.put({'points_score':self.points_score})
  85.             self.food.make_food()
  86.         else:
  87.             self.snake_points.pop(0)
  88.             self.check_game_over(new_snake_point)
  89.             self.snake_points.append(new_snake_point)

  90.     def calculate_new_coordinates(self):
  91.         last_x,last_y = self.snake_points[-1]
  92.         if self.direction == 'Up':
  93.             new_snake_point = last_x,last_y-10
  94.         elif self.direction == 'Down':
  95.             new_snake_point = last_x,last_y+10
  96.         elif self.direction == 'Left':
  97.             new_snake_point = last_x-10,last_y
  98.         elif self.direction == 'Right':
  99.             new_snake_point = last_x+10,last_y
  100.         return new_snake_point

  101.     def check_game_over(self, snake_point):
  102.         x,y = snake_point[0],snake_point[1]
  103.         if not -5 < x < 505 or not -5 < y < 315 or snake_point in self.snake_points:
  104.             self.queue.put({'game_over':True})


  105. def main():
  106.     q = queue.Queue()
  107.     gui = GUI(q)
  108.     gui.title("贪吃蛇")
  109.     snake = Snake(gui, q)
  110.     gui.bind('<Key-Left>', snake.key_pressed)
  111.     gui.bind('<Key-Right>', snake.key_pressed)
  112.     gui.bind('<Key-Up>', snake.key_pressed)
  113.     gui.bind('<Key-Down>', snake.key_pressed)
  114.     gui.mainloop()


  115. if __name__ == '__main__':
  116.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-15 17:07:07 | 显示全部楼层
wuqramy 发表于 2020-5-15 17:00
我记得
小甲鱼没有讲过贪吃蛇

他没讲,我看论坛有人发这个游戏,也许有人有资源
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-15 17:07:48 | 显示全部楼层
猪猪虾 发表于 2020-5-15 17:07
他没讲,我看论坛有人发这个游戏,也许有人有资源

做这个不需要资源
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-15 18:19:02 | 显示全部楼层
猪猪虾 发表于 2020-5-15 17:07
他没讲,我看论坛有人发这个游戏,也许有人有资源

贪吃蛇不需要素材的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-5-17 09:48:42 | 显示全部楼层
qiuyouzhi 发表于 2020-5-15 18:19
贪吃蛇不需要素材的

ao,好吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 13:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表