鱼C论坛

 找回密码
 立即注册
查看: 1960|回复: 6

[已解决]代码报错,求助

[复制链接]
发表于 2020-3-21 18:37:30 | 显示全部楼层 |阅读模式
6鱼币
本帖最后由 乘号 于 2020-3-21 20:04 编辑
  1. __author__ = 'ChengHao'

  2. from tkinter import *
  3. import threading
  4. import pueue
  5. import time
  6. import random


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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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


  116. if __name__ == '__main__':
  117.     main()

复制代码




报错信息:Traceback (most recent call last):
  File "C:\Users\cilu\Desktop\小游戏.py", line 5, in <module>
    import pueue
ModuleNotFoundError: No module named 'pueue'
最佳答案
2020-3-21 18:37:31
打错了  第五行是queue

最佳答案

查看完整内容

打错了 第五行是queue
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-21 18:37:31 | 显示全部楼层    本楼为最佳答案   
打错了  第五行是queue
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-21 18:51:18 | 显示全部楼层
谁能帮帮我………………
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-21 19:37:04 | 显示全部楼层
你在论坛也不是一天两天了,不知道提问要贴错误报告吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-21 20:04:40 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-3-21 19:37
你在论坛也不是一天两天了,不知道提问要贴错误报告吗?

Traceback (most recent call last):
  File "C:\Users\cilu\Desktop\小游戏.py", line 5, in <module>
    import pueue
ModuleNotFoundError: No module named 'pueue'
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-21 20:17:46 | 显示全部楼层
乘号 发表于 2020-3-21 20:04
Traceback (most recent call last):
  File "C:%users\cilu\Desktop\小游戏.py", line 5, in
    imp ...

真好,我也没有这个模块

cmd运行
  1. pip install pueue
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-21 20:18:58 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-3-21 20:17
真好,我也没有这个模块

cmd运行

emmm……还是没好
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 00:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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