December 03, 2020, 07:56:05 PM

### Author Topic: [bmx] asteroids movement by bradford6 [ 1+ years ago ]  (Read 608 times)

#### BlitzBot

• Jr. Member
•  • Posts: 1 ##### [bmx] asteroids movement by bradford6 [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : asteroids movement
Posted : 1+ years ago

Description : This is an example of turning and moving a ship like the classic game Asteroids

Code :
Code: BlitzMax
1. Strict
2. Graphics 1024,768
3. AutoMidHandle(True)
4.
5.
6. Local GW = GraphicsWidth()
7. Local GH = GraphicsHeight()
8. Local midw=GW/2
9. Local midh=GH/2
10. Local angle:Float
11. Local Thrust:Float
12. Local ThrustX:Float
13. Local ThrustY:Float
14. Local Angular_Velocity:Float
15. Local Friction:Float = 0.99
16. Local Xpos:Float = midw
17. Local Ypos:Float = midh
18. Local Velocity:Float
19. Local Xvelocity:Float
20. Local Yvelocity:Float
21.
22. Local ship:timage = CreateImage(64,64)
23.         ' draw the ship...
24.         SetColor 0,255,0
25.         DrawLine 0,0,64,32
26.         DrawLine 64,32,0,64
27.         SetColor 50,50,50
28.         DrawRect 0,16,32,32
29.         SetColor 255,255,0
30.         DrawOval 36,24,22,16
31.         GrabImage(ship,0,0)
32. Cls
33.
34.
35. Repeat
36.         ' Calculate the Heading of the ship (the Angle)
37.                 Angular_Velocity:*Friction      ' slow the spin
38.                 angle:+Angular_Velocity         ' apply the angular velocity to the current Angle
39.
40.         ' Calculate the Speed
41.
42.                 XVelocity:*Friction                     ' slow the speed down (I know there is no friction in space but It looks better :)
43.                 Yvelocity:*Friction
44.                 Thrust:*0.95                            ' this reduces the thrust amount each frame
45.
46.         ' Here we are dividing the Thrust into its X and Y components
47.
48.                 ThrustX = Thrust*Cos(angle)
49.                 ThrustY = Thrust*Sin(angle)
50.         ' now we add the resulting Thrust to the Velocities
51.
52.                 Xvelocity:+ Thrustx
53.                 Yvelocity:+ Thrusty
54.         ' AND update the ships current X,Y Cartesian Coordinates
55.
56.                 Xpos:+XVelocity
57.                 Ypos:+YVelocity
58.
59.
60.         ' Get Key Input
61.         If KeyDown(KEY_UP) Then Thrust:+0.01
62.
63.
64.
65.         If KeyDown(KEY_LEFT) Then Angular_Velocity:-0.1
66.         If KeyDown(KEY_RIGHT) Then Angular_Velocity:+0.1
67.
68.
69.         ' WRAP to SCREEN
70.         If xpos > GW Then Xpos = 0
71.         If ypos > GH Then Ypos = 0
72.         If Xpos < 0 Then Xpos = GW
73.         If Ypos < 0 Then Ypos = GH
74.
75.         ' Draw the stuff to the screen
76.                 SetRotation angle
77.                 DrawImage ship, Xpos , Ypos
78.                 SetRotation 0
79.                 SetColor 255,0,0
80.                 DrawRect midw-20,midh-20,40,40
81.                 SetColor 255,255,255
82.
83.         Flip
84.         Cls
85. Until KeyDown(KEY_ESCAPE)