2010-08-04 13 views
7

मैं पायथन और टिंकर का उपयोग कर क्षुद्रग्रहों का एक संस्करण बनाने के लिए काम कर रहा हूं। जब बाएं या दायां तीर कुंजी दबाया जाता है तो जहाज को घूमने की आवश्यकता होती है। जहाज टिंकर कैनवास पर एक त्रिकोण है। मुझे त्रिकोण के लिए निर्देशांक समायोजित करने के लिए फॉर्मूला के साथ आने में परेशानी हो रही है। मेरा मानना ​​है कि इसमें पाप और कॉस के साथ कुछ करना है, हालांकि मुझे बिल्कुल यकीन नहीं है। अब तक मेरे पास जहाज के लिए दो कक्षाएं हैं और दूसरा खेल के लिए है। जहाज वर्ग में मेरे पास मुख्य प्रेस के लिए कॉलबैक विधियां हैं I किसी भी तरह की सहायता का स्वागत किया जाएगा। धन्यवाद।मैं टिंकर कैनवास पर पाइथन में बहुभुज कैसे घुमा सकता हूं?

जहाज कक्षा

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) 

उत्तर

9

सबसे पहले, आप त्रिकोण के केंद्र के चारों ओर बारी बारी से करने की जरूरत है। सेंट्रॉइड शायद इसके लिए सबसे अच्छा काम करेगा। इसे खोजने के लिए, आप सूत्र 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() 

मैं, नियंत्रण है कि खेल क्षुद्रग्रहों की तरह थोड़ा अधिक बनाने के लिए कुछ परिवर्तन किए माध्यम से। (हालांकि, फायरिंग को लागू नहीं किया गया था। हालांकि, मुझे उम्मीद से ज्यादा फायदा हो सकता है, लेकिन मैं सबकुछ करने वाला नहीं हूं। साथ ही, जब आप एक साथ कई आंदोलन कुंजी का उपयोग करने का प्रयास करते हैं तो कुछ समस्या होती है, लेकिन कि, जिस तरह से टी घटना हैंडलिंग। यह जुआ खेलने के लिए नहीं बनाया गया था करता है की वजह से है, तो आपको लगता है कि टी/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) 

एक अलग रूप में के रूप में , हो सकता है कि आप गुणों का उपयोग करना चाहें ताकि अंक और आसानी से आसानी से निपटने की अनुमति मिल सके।

+2

सहायता के लिए बहुत बहुत धन्यवाद। आप एक विद्वान और सज्जन हैं। यही वह है जिसकी तलाश में मैं हूं। – Sam

+1

आपका स्वागत है। – JAB

संबंधित मुद्दे