나는 파이썬과 Tkinter를 사용하여 소행성의 버전을 만들려고 노력 중이다. 왼쪽 또는 오른쪽 화살표 키를 누르면 우주선이 회전해야합니다. 배는 Tkinter 캔버스에있는 삼각형입니다. 삼각형의 좌표를 조정하는 수식 문제가 있습니다. 나는 그것이 확실하지 않지만 죄와 사면과 관련이 있다고 믿습니다. 지금까지 나는 배를위한 것과 배틀을위한 두 개의 클래스를 가지고 있습니다. ship 클래스에는 키 누르기를위한 콜백 메소드가 있습니다. 어떤 도움이라도 대단히 감사하겠습니다. 감사.Tkinter Canvas에서 파이썬으로 폴리곤을 회전하는 방법은 무엇입니까?
선박 클래스
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)
도움을 주셔서 대단히 감사합니다. 당신은 학자이고 신사입니다. 이것은 내가 찾고 있었던 바로 그 것이다. – Sam
대단히 반갑습니다. – JAB