forked from Pakz001/Monkey2examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Beginners - Vec2f (ship movement).monkey2
74 lines (55 loc) · 1.48 KB
/
Beginners - Vec2f (ship movement).monkey2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
Global instance:AppInstance
Class MyWindow Extends Window
' Here we create the vectors for the ship
' and the velocity
Field ship:Vec2f
Field velocity:Vec2f
Method New()
ship.X = 100
ship.Y = 100
End Method
Method OnRender( canvas:Canvas ) Override
' if key escape then quit
If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate()
App.RequestRender() ' Loop with sync
' Input
If Keyboard.KeyDown(Key.Right)
velocity.X += 0.1
End If
If Keyboard.KeyDown(Key.Left)
velocity.X -= 0.1
End If
If Keyboard.KeyDown(Key.Up)
velocity.Y -= 0.1
End If
If Keyboard.KeyDown(Key.Down)
velocity.Y += 0.1
End If
'Limit speed
If velocity.X < -5 Then velocity.X = -5
If velocity.X > 5 Then velocity.X = 5
If velocity.Y < -5 Then velocity.Y = -5
If velocity.Y > 5 Then velocity.Y = 5
' keep the ship inside the viewport
If ship.X > canvas.Viewport.Width Then ship.X = 0
If ship.X < 0 Then ship.X = canvas.Viewport.Width
If ship.Y > canvas.Viewport.Height Then ship.Y = 0
If ship.Y < 0 Then ship.Y = canvas.Viewport.Height
' Update the ship position
ship+=velocity
canvas.Clear(Color.Black)
canvas.Color = Color.White
canvas.DrawText("Use cursor keys to move rectangle around the screen.",0,0)
' Draw the ship
canvas.DrawRect(ship.X,ship.Y,20,20)
End Method
End Class
Function Main()
instance = New AppInstance
New MyWindow
App.Run()
End Function