Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Python tkinter蛇游戏绘图滞后

Python tkinter蛇游戏绘图滞后
EN

Stack Overflow用户
提问于 2016-02-09 02:01:52
回答 1查看 964关注 0票数 1
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#example snake snake = [[1, 2], [1, 3], [2, 3]]
def draw():
canvas.delete('all')
for segment in snake:
    y = segment[0] * 10
    x = segment[1] * 10
    canvas.create_rectangle(x, y, x + 10, y + 10, fill="red")
    canvas.update()

我用tkinter在python中创建了一个简单的蛇游戏,但是当蛇数组包含30+矩形时,动作会迅速减慢,所以我想知道是否有更好的方法来绘制对象,使其工作得更快,而不是不断调用这个绘制函数?

另一个可能的问题是调动功能效率低下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def move_right(event):

global left, right, up, down
if right != True:
    left, right, up, down = False, True, False, False
    while right == True:
        if snake[0][1] >= snake[1][1]:
            for x in range(len(snake) - 1, 0, -1):
                snake[x] = snake[x - 1]
            snake[0] = [snake[0][0], snake[0][1] + 1]
            draw()
        time.sleep(0.05)

这是我的第一个真正的游戏,所以不要杀了我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-09 03:51:36

您不必删除画布中的所有矩形。您必须只删除最后一个矩形,并添加新的头部矩形。

要在蛇列表中添加新的x,y,不需要移动所有元素--只需要snake.insert(0, [new_x, new_y])

您可以使用root.after代替while循环和sleep

示例-不检查碰撞

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tkinter as tk

# === constants ===

BLOCK_SIZE = 10
TIME = 50

# === functions ===

# create all rectangles on canvas
def create_snake(canvas, snake):
    snake_rect = []

    for x, y in snake:
        x1 = x * BLOCK_SIZE
        y1 = y * BLOCK_SIZE
        x2 = x1 + BLOCK_SIZE
        y2 = y1 + BLOCK_SIZE
        rect = canvas.create_rectangle(x1,y1,x2,y2, fill='red')
        snake_rect.append(rect)

    return snake_rect

# move snake - add first rectangle and remove last one
def move(canvas, snake, snake_rect, remove_last=True):

    # get head
    x, y = snake[0]

    # new head position
    if direction == 'up':
        y = y-1
    elif direction == 'down':
        y = y+1
    elif direction == 'left':
        x = x-1
    elif direction == 'right':
        x = x+1

    # add first - new head
    snake.insert(0, [x, y])

    x1 = x * BLOCK_SIZE
    y1 = y * BLOCK_SIZE
    x2 = x1 + BLOCK_SIZE
    y2 = y1 + BLOCK_SIZE
    rect = canvas.create_rectangle(x1,y1,x2,y2, fill='red')

    snake_rect.insert(0, rect)

    # remove last - tail (if snake doesn't eat 'apple')
    if remove_last:
        del snake[-1]

        canvas.delete(snake_rect[-1])
        del snake_rect[-1]

    # call `move` function again after TIME miliseconds
    root.after(TIME, move, canvas, snake, snake_rect)

# change direction                
def change_direction(new_direction):
    global direction

    #print(new_direction)

    if new_direction == 'left':
        if direction != 'right':
            direction = new_direction
    elif new_direction == 'right':
        if direction != 'left':
            direction = new_direction
    elif new_direction == 'up':
        if direction != 'down':
            direction = new_direction
    elif new_direction == 'down':
        if direction != 'up':
            direction = new_direction

# === main ===

direction = 'up'

# ---

root = tk.Tk()

canvas = tk.Canvas(root)
canvas.pack()

# create long (curved) snake
snake = [[x,25] for x in range(10,35)] + [[35, y] for y in range(25, 1, -1)] + [[x, 1] for x in range(35, 1, -1)]
snake_rect = create_snake(canvas, snake)

# call `move` function after TIME miliseconds
root.after(TIME, move, canvas, snake, snake_rect)

# bind arrows to change snake direction
root.bind('<Left>', lambda event:change_direction('left'))
root.bind('<Right>', lambda event:change_direction('right'))
root.bind('<Up>', lambda event:change_direction('up'))
root.bind('<Down>', lambda event:change_direction('down'))

# start program
root.mainloop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35289012

复制
相关文章

相似问题

添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文