gpt4 book ai didi

python - 如何在 Tkinter Canvas 上旋转多边形?

转载 作者:太空狗 更新时间:2023-10-29 20:55:03 29 4
gpt4 key购买 nike

我正在努力使用 Python 和 Tkinter 创建一个小行星版本。当按下左或右箭头键时,船需要旋转。这艘船是 Tkinter Canvas 上的三角形。我无法想出调整三角形坐标的公式。我相信它与 sin 和 cos 有关,尽管我不确定。到目前为止,我有两个类,一个用于船,另一个用于游戏。在 ship 类中,我有按键的回调方法。任何帮助将不胜感激。谢谢。

船级

import math
class Ship:
def __init__(self,canvas,x,y,width,height):
self.canvas = canvas
self.x = x - width/2
self.y = y + height/2
self.width = width
self.height = height

self.x0 = self.x
self.y0 = self.y

self.x1 = self.x0 + self.width/2
self.y1 = self.y0-self.height

self.x2 = self.x0 + self.width
self.y2 = self.y0

self.ship = self.canvas.create_polygon((self.x0, self.y0, self.x1, self.y1, self.x2, self.y2), outline="white", width=3)
def changeCoords(self):
self.canvas.coords(self.ship,self.x0, self.y0, self.x1, self.y1, self.x2, self.y2)
def rotateLeft(self, event=None):
# Should rotate one degree left.
pass
def rotateRight(self, event=None):
# Should rotate one degree right.
self.x0 = self.x0 -1
self.y0 = self.y0 - 1

self.x1 = self.x1 + 1
self.y1 = self.y1 + 1

self.x2 = self.x2 - 1
self.y2 = self.y2 + 1
self.changeCoords()

游戏类

from Tkinter import *
from ship import *


class Game:
def __init__(self, gameWidth, gameHeight):
self.root = Tk()
self.gameWidth = gameWidth
self.gameHeight = gameHeight
self.gameWindow()

self.ship = Ship(self.canvas, x=self.gameWidth/2,y=self.gameHeight/2, width=50, height=50)
self.root.bind('<Left>', self.ship.rotateLeft)
self.root.bind('<Right>', self.ship.rotateRight)

self.root.mainloop()

def gameWindow(self):
self.frame = Frame(self.root)
self.frame.pack(fill=BOTH, expand=YES)

self.canvas = Canvas(self.frame,width=self.gameWidth, height=self.gameHeight, bg="black", takefocus=1)
self.canvas.pack(fill=BOTH, expand=YES)

asteroids = Game(600,600)

最佳答案

首先,您需要围绕三角形的中心旋转。质心可能最适合这种情况。要找到它,您可以使用公式 C = (1/3*(x0 + x1 + x2), 1/3*(y0 + y1 + y2)),因为它是所有的平均值三角形中的点。然后你必须应用以该点为中心的旋转。所以它会是这样的......

import math

class Ship:
def centroid(self):
return 1 / 3 * (self.x0 + self.x1 + self.x2), 1 / 3 * (self.y0 + self.y1 + self.y2)

def __init__(self, canvas, x, y, width, height, turnspeed, acceleration=1):
self._d = {'Up':1, 'Down':-1, 'Left':1, 'Right':-1}

self.canvas = canvas
self.width = width
self.height = height
self.speed = 0
self.turnspeed = turnspeed
self.acceleration = acceleration

self.x0, self.y0 = x, y

self.bearing = -math.pi / 2

self.x1 = self.x0 + self.width / 2
self.y1 = self.y0 - self.height

self.x2 = self.x0 + self.width
self.y2 = self.y0

self.x, self.y = self.centroid()

self.ship = self.canvas.create_polygon((self.x0, self.y0, self.x1, self.y1, self.x2, self.y2), outline="white", width=3)

def changeCoords(self):
self.canvas.coords(self.ship,self.x0, self.y0, self.x1, self.y1, self.x2, self.y2)

def rotate(self, event=None):
t = self._d[event.keysym] * self.turnspeed * math.pi / 180 # the trig functions generally take radians as their arguments rather than degrees; pi/180 radians is equal to 1 degree

self.bearing -= t

def _rot(x, y):
#note: the rotation is done in the opposite fashion from for a right-handed coordinate system due to the left-handedness of computer coordinates
x -= self.x
y -= self.y
_x = x * math.cos(t) + y * math.sin(t)
_y = -x * math.sin(t) + y * math.cos(t)
return _x + self.x, _y + self.y

self.x0, self.y0 = _rot(self.x0, self.y0)
self.x1, self.y1 = _rot(self.x1, self.y1)
self.x2, self.y2 = _rot(self.x2, self.y2)
self.x, self.y = self.centroid()

self.changeCoords()

def accel(self, event=None):
mh = int(self.canvas['height'])
mw = int(self.canvas['width'])
self.speed += self.acceleration * self._d[event.keysym]

self.x0 += self.speed * math.cos(self.bearing)
self.x1 += self.speed * math.cos(self.bearing)
self.x2 += self.speed * math.cos(self.bearing)

self.y0 += self.speed * math.sin(self.bearing)
self.y1 += self.speed * math.sin(self.bearing)
self.y2 += self.speed * math.sin(self.bearing)

self.x, self.y = self.centroid()

if self.y < - self.height / 2:
self.y0 += mh
self.y1 += mh
self.y2 += mh
elif self.y > mh + self.height / 2:
self.y0 += mh
self.y1 += mh
self.y2 += mh

if self.x < -self.width / 2:
self.x0 += mw
self.x1 += mw
self.x2 += mw
elif self.x > mw + self.width / 2:
self.x0 -= mw
self.x1 -= mw
self.x2 -= mw

self.x, self.y = self.centroid()

self.changeCoords()

顺便说一句,我对控件做了一些更改,使游戏更像小行星。 (虽然没有实现开火。我可能比我预期的更了解这个,但我不会做所有事情。另外,当你尝试一次使用多个移动键时会有一点问题,但是这是由于 Tk 处理事件的方式。它不是为游戏设计的,所以你必须花很多时间才能让它在 Tk/Tkinter 中正常工作。)

from tkinter import *
from ship import *

class Game:
def __init__(self, gameWidth, gameHeight):
self.root = Tk()
self.gameWidth = gameWidth
self.gameHeight = gameHeight
self.gameWindow()

self.ship = Ship(self.canvas, x=self.gameWidth / 2,y=self.gameHeight / 2, width=50, height=50, turnspeed=10, acceleration=5)
self.root.bind('<Left>', self.ship.rotate)
self.root.bind('<Right>', self.ship.rotate)
self.root.bind('<Up>', self.ship.accel)
self.root.bind('<Down>', self.ship.accel)

self.root.mainloop()

def gameWindow(self):
self.frame = Frame(self.root)
self.frame.pack(fill=BOTH, expand=YES)

self.canvas = Canvas(self.frame,width=self.gameWidth, height=self.gameHeight, bg="black", takefocus=1)
self.canvas.pack(fill=BOTH, expand=YES)

asteroids = Game(600,600)

顺便说一句,您可能希望使用属性来更轻松地处理点等。

关于python - 如何在 Tkinter Canvas 上旋转多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3408779/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com