Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 990859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:00:38+00:00 2026-05-16T06:00:38+00:00

I am working to create a version of asteroids using Python and Tkinter. When

  • 0

I am working to create a version of asteroids using Python and Tkinter. When the left or right arrow key is pressed the ship needs to rotate. The ship is a triangle on the Tkinter canvas. I am having trouble coming up with formula to adjust the coordinates for the triangle. I believe it has something to do with sin and cos, though I am not exactly sure. So far I have two classes one for the ship and the other for the game. In the ship class I have callback methods for the key presses. Any help would be greatly appreciated. Thanks.

Ship Class

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()

Game Class

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:00:39+00:00Added an answer on May 16, 2026 at 6:00 am

    First of all, you need to rotate around a center of the triangle. The centroid would probably work best for that. To find that, you can use the formula C = (1/3*(x0 + x1 + x2), 1/3*(y0 + y1 + y2)), as it’s the average of all points in the triangle. Then you have to apply the rotation with that point as the center. So it’d be something like this…

    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()
    

    I made some changes to the controls that make the game a bit more like Asteroids, by the way. (Didn’t implement firing, though. I may have gotten more into this than I expected, but I’m not going to do everything. Also, there’s a bit of a problem when you try to use multiple movement keys at once, but that’s due to the way Tk does event handling. It wasn’t designed for gaming, so you’d have to fiddle around a fair bit to get that working properly with 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)
    

    As an aside, you might want to use properties to allow for easier handling of the points and such.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This has been identified as a bug in MSAF and… May 17, 2026 at 2:28 am
  • Editorial Team
    Editorial Team added an answer If you want to use tab for indentation, you might… May 17, 2026 at 2:28 am
  • Editorial Team
    Editorial Team added an answer It's not guaranteed to be in order, in a sense… May 17, 2026 at 2:28 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am using a very stripped down version of Linux, I want to create
I am creating a process for working with version control and my web based
Im trying to create a custom version of the RequiredAttribute to replace the built
I am working on a project that uses conflict.dll version 6.2, but the project
I am trying to create a magento shopping cart module but things arent working
I'm working on a C++ and Objective C iPhone Project. I'm using git as
I need a version control system that works like Subversion but is able to
I'm working on creating a new base class for an asp.net app in order
Short version: assuming I don't want to keep the data for long, how do
I have started working on what I expect to become, by far, the largest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.