乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      如何旋轉(zhuǎn)一個多邊形在python的Tkinter的畫布?

       imelee 2016-12-18
        

      如何旋轉(zhuǎn)一個多邊形在python的Tkinter的畫布?



      python 向量vector tkinter
      我的工作來創(chuàng)建一個版本的Python和Tkinter的的。當(dāng)左或右箭頭鍵按下時的船舶必須旋轉(zhuǎn)。這艘船是Tkinter的畫布上的三角形。我遇到了公式,為三角形的坐標(biāo)。我相信這是與sin和cos,雖然我并不很清楚。到目前為止,我有兩個類一個用于船舶和其他的在船級我對按鍵。任何幫助將不勝感激。謝謝。 船級
      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)
      

      本文地址 :CodeGo.net/182737/
      -------------------------------------------------------------------------------------------------------------------------
      1. 首先,你需要圍成的三角形的中心旋轉(zhuǎn)。重心很可能最適合的。地發(fā)現(xiàn),你的公式C = (1/3*(x0 + x1 + x2), 1/3*(y0 + y1 + y2))的,因為它是所有點的平均值中的三角形。然后,你必須應(yīng)用旋轉(zhuǎn)以該點為中心。所以它會是這樣的...
      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()
      
      我做了修改,以使更多的有點像小行星的控件,順便說一句。 (沒有射擊 CodeGo.net,雖然,我可能得到更進這個比我的預(yù)期,但我不打算做的一切。此外,當(dāng)您嘗試多個鍵一次有一個有點問題,但是這是由于這樣就Tk的事件處理,它不是設(shè)計的游戲,所以你必須反復(fù)折騰公平一點,以獲得與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)
      
      順便說一句,你可能想的屬性,以便更容易處理點,這樣的。
      本文標(biāo)題 :如何旋轉(zhuǎn)一個多邊形在python的Tkinter的畫布?
      本文地址 :CodeGo.net/182737/  

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多