[英]I'm making a snake game and keep getting this error _tkinter.TclError: bad geometry specifier "704x772+288.0+14.0" what should I do?
I'm coding a snake game in python and I'm at the point where the food should show up on the screen but I am getting this error:我正在 python 中编写一个蛇游戏,我正处于食物应该出现在屏幕上的位置,但我收到了这个错误:
return self.tk.call('wm', 'geometry', self._w, newGeometry) _tkinter.TclError: bad geometry specifier "704x772+288.0+14.0" return self.tk.call('wm', 'geometry', self._w, newGeometry) _tkinter.TclError: bad geometry specifier "704x772+288.0+14.0"
import random
GAME_WIDTH = 700
GAME_HEIGHT = 700
SPEED = 50
SPACE_SIZE = 50
BODY_PARTS = 3
SNAKE_COLOUR = "#00FF00"
FOOD_COLOUR = "#FF0000"
BACKGROUND_COLOUR = "#000000"
class snake:
pass
class food:
def __init__(self):
x = random.randint(0, (GAME_WIDTH /SPACE_SIZE-1)) * SPACE_SIZE
y = random.randint(0, (GAME_HEIGHT / SPACE_SIZE - 1)) * SPACE_SIZE
self.coordinates = [x,y]
canvas.create_oval(x,y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOUR, tag="food")
def next_turn():
pass
def change_direction(new_direction):
pass
def check_collisions():
pass
def game_over():
pass
from tkinter import *
window = Tk()
window.title("Snake Game")
score = 0
direction = 'down'
label = Label(window, text="Score:{}".format(score), font=('consolas', 40))
label.pack()
canvas = Canvas(window, bg=BACKGROUND_COLOUR, height=GAME_HEIGHT, width=GAME_WIDTH)
canvas.pack()
window.update()
window_width: int = window.winfo_width()
window_height = window.winfo_height()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = int(screen_width/2) - (window_width/2)
y = int(screen_height/2) - (window_height/2)
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
snake = Snake()
food = Food()
window.mainloop()
Replace window_width/2
with something that produces an int, eg window_width//2
.将
window_width/2
替换为产生 int 的东西,例如window_width//2
。 Do it likewise for window_height/2
.对
window_height/2
做同样的事情。
This will remove the offending .0
from the geometry.这将从几何图形中删除有问题的
.0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.