Skip to content

Instantly share code, notes, and snippets.

@Torxed
Last active July 29, 2020 11:48
Show Gist options
  • Save Torxed/073f127a3f28c5285de759ef61875a7f to your computer and use it in GitHub Desktop.
Save Torxed/073f127a3f28c5285de759ef61875a7f to your computer and use it in GitHub Desktop.
import math
import pyglet
class Coordinate:
x = 0
y = 0
def __repr__(self):
return f'Coordinate(x: {self.x}, y: {self.y})'
window = pyglet.window.Window()
main_batch = pyglet.graphics.Batch()
player_image = pyglet.image.load('player.png')
player_image.anchor_x = player_image.width // 2
player_image.anchor_y = player_image.height // 2
player = pyglet.sprite.Sprite(player_image, x=100, y=200, batch=main_batch)
mouse = Coordinate()
label_delta = pyglet.text.Label('', x=10, y=window.height-20, batch=main_batch)
label_tangent = pyglet.text.Label('', x=10, y=window.height-40, batch=main_batch)
label_degrees = pyglet.text.Label('', x=10, y=window.height-60, batch=main_batch)
keys = {}
angle = 0
def delta(source, destination):
# Step 2
result = Coordinate()
result.x = destination.x - source.x
result.y = destination.y - source.y
return result
def angle_in_radiance(delta_y, delta_x):
# Step 3
return math.atan2(delta_y, delta_x)
def radiance_to_degrees(radiance):
# Step 4
return (radiance * (180/math.pi) + 360) % 360
def draw_line_between(source, destination):
pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
("v2f", (source.x, source.y, destination.x, destination.y))
)
def move(object, angle, distance):
# Convert the angle to a cosine and sine
# https://setosa.io/ev/sine-and-cosine/
# (Essentially a multiplier in a diction/space)
x_multiplier = math.cos(((angle)/180)*math.pi)
y_multiplier = math.sin(((angle)/180)*math.pi)
# Multiply the x and y multipliers with the distance you want to travel
object.x += x_multiplier * distance
object.y += y_multiplier * distance
@window.event
def on_mouse_motion(x, y, dx, dy):
# Step 1, gether the data
mouse.x = x
mouse.y = y
@window.event
def on_mouse_press(*args, **kwargs):
global angle # Put it here to emphasis that it's a global variable
move(player, angle, distance=5) # 50 pixels
@window.event
def on_draw():
global angle
window.clear()
# Step 2
delta_result = delta(player, mouse)
# Step 3
tangent = angle_in_radiance(delta_result.y, delta_result.x)
# Step 4
angle = radiance_to_degrees(tangent)
# Debug output
label_delta.text = 'Delta: ' + str(delta_result)
label_tangent.text = 'Tangent: ' + str(tangent)
label_degrees.text = 'Degrees: ' + str(int(angle))
draw_line_between(player, mouse)
# Use the result
player.rotation = 0-angle # Degrees are counted counter clockwise, so we have to adjust for it.
# Draw it
main_batch.draw()
pyglet.app.run()
@Torxed
Copy link
Author

Torxed commented Jul 29, 2020

angles

@Torxed
Copy link
Author

Torxed commented Jul 29, 2020

player.png

player

@Torxed
Copy link
Author

Torxed commented Jul 29, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment