Python Forum
[Pyglet] Space ships movements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pyglet] Space ships movements
#1
Hi, I want to do a spaceship that I can control with the arrows on the keyboard. For now I'm able to make it rotate on itself with the left and the right arrow, but I want to make it go in the direction that the ship is facing. For now, all I can do is make it go up and down, but on one axis.

My code look like this:
import pyglet

fenetre = pyglet.window.Window(700, 700, 'Lost')

fusee_img = pyglet.image.load('player1-ConvertImage.jpg')
fusee_img.anchor_x = fusee_img.width
fusee_img.anchor_y = fusee_img.height
fusee = pyglet.sprite.Sprite(fusee_img, x=100, y=100)

keys = pyglet.window.key.KeyStateHandler()


def update(dt):
    fenetre.push_handlers(keys)
    left = keys[pyglet.window.key.LEFT]
    right = keys[pyglet.window.key.RIGHT]
    up = keys[pyglet.window.key.UP]
    down = keys[pyglet.window.key.DOWN]
    if up and down:
        None
    elif up:
        fusee.y += (dt * 100)
    elif down:
        fusee.y -= dt * 100
    if right and left:
        None
    elif right:
        fusee.rotation += dt * 300
    elif left:
        fusee.rotation -= dt * 300


pyglet.clock.schedule_interval(update, 1 / 120.0)


@fenetre.event
def on_draw():
    fenetre.clear()
    fusee.draw()


pyglet.app.run()
Reply
#2
The math is pretty simple
import math
def get_direction(angle):
    rad = math.radians(angle)
    return math.cos(rad), math.sin(rad)
You might have to adjust angle for math to work as expected. Like 90, 180, -90.
No more thinking up is up. It is now forward. You have to add to x and y for up and down.
Up.
direction = get_direction(angle)
x += direction[0] * dt * 100
y += direction[1] * dt * 100
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Battleships Program must display how many ships are left after each turn FnaticPutin 1 3,101 Oct-24-2017, 03:21 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020