Python游戏:控制广场。如果square在随机衍生的方块的10个像素内,它会增加1个点。不行。为什么?

时间:2016-12-29 01:17:26

标签: python tkinter tkinter-canvas

我做了一个游戏,你控制一个正方形,随机的其他正方形在地图上随机生成。如果第一个方块在另一个方块的10个像素内,则会增加一个点,但程序不起作用。谁能告诉我为什么?

这是我的代码:

from tkinter import *
from random import uniform, randrange
tk = Tk()
canvas = Canvas(tk, width=400, height=400,bg='black')
canvas.pack()
pointcount = -1
LENGTH = 15
WIDTH = 15
LENGTH2 = randrange(1,390)
WIDTH2 = randrange(1,390)
LENGTH3 = LENGTH2 + 15
WIDTH3 = WIDTH2 + 15
X = randrange(1,400)
Y = randrange(1,400)
Snake = canvas.create_rectangle(0,0,WIDTH,LENGTH,fill="green")
Food = canvas.create_rectangle(WIDTH2,LENGTH2,WIDTH3,LENGTH3,fill="yellow")
pos = canvas.coords(Snake)
pos2 = canvas.coords(Food)
def movement_right(event):
    canvas.move(Snake,15,0)
def movement_left(event):
    canvas.move(Snake,-15,0)
def movement_down(event):
    canvas.move(Snake,0,15)
def movement_up(event):
    canvas.move(Snake,0,-15)
for i in range(1,20):
    if pos[0] - pos2[2] == 10:
        pointcount = pointcount + 1
        print("Total points : ", pointcount)
        tk.update()


tk.bind('<Left>', movement_left)
tk.bind('<Right>', movement_right)
tk.bind('<Down>', movement_down)
tk.bind('<Up>', movement_up)



tk.mainloop()

1 个答案:

答案 0 :(得分:0)

你必须在函数内部检查它,因为其他代码只执行一次。

您可以创建函数来测试位置并在所有movement_函数中执行。

我使用&#34;细胞&#34;设定职位

import tkinter as tk
import random

# --- constants --- (UPPER_CASE names)

SCREEN_WIDTH = 400
SCREEN_HEIGHT = 400

CELL_WIDTH = 20
CELL_HEIGHT = 20

COLS = SCREEN_WIDTH/CELL_WIDTH
ROWS = SCREEN_HEIGHT/CELL_HEIGHT

# --- functions ---

def test():
    global pointcount

    snake_pos = canvas.coords(snake)
    food_pos = canvas.coords(food)

    # calculate distance 
    diff_x = abs(snake_pos[0] - food_pos[0]) 
    diff_y = abs(snake_pos[1] - food_pos[1]) 

    print(diff_x, diff_y)

    # if snake eat food
    if diff_x == 0 and diff_y == 0:
        pointcount += 1
        print("Total points : ", pointcount)

        # move food to new place
        food_x1 = CELL_WIDTH * random.randrange(0, COLS)
        food_y1 = CELL_HEIGHT * random.randrange(0, ROWS)

        food_x2 = food_x1 + CELL_WIDTH
        food_y2 = food_y1 + CELL_HEIGHT

        canvas.coords(food, (food_x1, food_y1, food_x2, food_y2))

def movement_right(event):
    canvas.move(snake, CELL_WIDTH, 0)
    test()

def movement_left(event):
    canvas.move(snake, -CELL_WIDTH, 0)
    test()

def movement_down(event):
    canvas.move(snake, 0, CELL_HEIGHT)
    test()

def movement_up(event):
    canvas.move(snake, 0, -CELL_HEIGHT)
    test()

# --- other --- (lower_case names)

pointcount = 0

# --- main ---

root = tk.Tk()

canvas = tk.Canvas(root, width=SCREEN_WIDTH, height=SCREEN_HEIGHT, bg='black')
canvas.pack()

root.bind('<Left>', movement_left)
root.bind('<Right>', movement_right)
root.bind('<Down>', movement_down)
root.bind('<Up>', movement_up)

snake = canvas.create_rectangle(0, 0, CELL_WIDTH, CELL_HEIGHT, fill="green")

# create food in first random place
food_x1 = CELL_WIDTH * random.randrange(0, COLS)
food_y1 = CELL_HEIGHT * random.randrange(0, ROWS)

food_x2 = food_x1 + CELL_WIDTH
food_y2 = food_y1 + CELL_HEIGHT

food = canvas.create_rectangle(food_x1, food_y1, food_x2, food_y2, fill="yellow")

root.mainloop()