December 04, 2020, 10:44:07 AM

### Author Topic: [bmx] breakout by Matt McFarland [ 1+ years ago ]  (Read 555 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bmx] breakout by Matt McFarland [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : breakout
Author : Matt McFarland
Posted : 1+ years ago

Description : I haven't touched blitzmax in 2 years and felt like getting my feet wet again...

Borrowed some code from <a href="../Community/posts4ca3.html?topic=86957#985715" target="_blank">here[/url] and <a href="http://www.blitzmax.com/codearcs/codearcs.php?code=1321" target="_blank"> here [/url]
and made a simple breakout game with decent physics.  Still a bit quirky though.

Code :
Code: BlitzMax
1. SuperStrict
2. Graphics 640, 480
3. Global ObjectList:TList = CreateList()
4. Global BrickList:TList = CreateList()
5. Const COLOR_BLUE:Byte = 0
6. Const COLOR_GREEN:Byte = 1
7. Const COLOR_RED:Byte = 2
8.
9.
10. Function DrawGeom(x:Float, y:Float, sides:Float, length:Float, angle:Float, r:Double = 255, g:Double = 255, b:Double = 255, glow:Byte = True)
11.
12.         Local aStep# = 360 / sides
13.         Length:Float = (length / 2) / Sin(aStep:Float / 2) 'Calculate the correct length of a side
14.         For Local a:Byte = 0 To sides - 1
15.                 Local x1:Float = x:Float - (Sin(angle + (aStep * a)) * length)
16.                 Local y1:Float = y:Float - (Cos(angle + (aStep * a)) * length)
17.                 Local x2:Float = x:Float - (Sin(angle + (aStep * (a + 1))) * length)
18.                 Local y2:Float = y:Float - (Cos(angle + (aStep * (a + 1))) * length)
19.                 If glow = True
20.                         SetColor r / 4, g / 4, b / 4
21.                         DrawOval x1 - 5, y1 - 5, 11, 11                                                 ' Draw Circle at Vertex
22.                 End If
23.                 SetColor r, g, b
24.                 DrawLine x1, y1, x2, y2                                                                 ' Draw Connecting Lines
25.         Next
26.
27. End Function
28.
29.
30.
31. Type TgameObject
32.         Field x:Double, y:Double, Size:Float
33.         Field xSpeed:Float, ySpeed:Float
34.
36. End Type
37.
38. Function Spawn(Obj:TgameObject, xstart:Double, ystart:Double, size:Float)
39.         obj.x = xstart
40.         obj.y = ystart
41.         obj.Size = size
43. End Function
44.
45. Type tBall Extends TgameObject
46.         Field rot:Float
47.         Function Create:tBall(xstart:Double, ystart:Double)
48.                 Local newBall:tBall = New tBall
49.                 Spawn (newball, xstart, ystart, 6)
50.                 newball.ySpeed = 2.6
51.                 newball.xSpeed = 2.6
52.         End Function
53.
55.                 SetBlend ALPHABLEND
56.                 DrawGeom(x, y, 6, size, rot, 255, 255, 255, False)
57.
58.                 x:+xspeed
59.                 y:+yspeed
60.                 rot:-xspeed * 6
61.                 If x > 640 Or x < 0 Then xspeed = -(xspeed)
62.                 If y > 480 Or y < 0 Then yspeed = -(yspeed)
63.                 SetBlend LIGHTBLEND
64.                 For Local brick:tBrick = EachIn BrickList
65.                         If brick.dead = False collide(brick:tBrick)
66.                 Next
67.
68.         End Method
69.
70.         Method collide(Obj:TBrick)
71.                 Local Distance:Float = Sqr((Obj.X - X) * (Obj.X - X) + (Obj.Y - Y) * (Obj.Y - Y))
72.                 Local TouchDistance:Float = size + Obj.size
73.                 If Distance < TouchDistance
75.                         'Detect angle of collision
76.                         Local CollisionAngle:Float = ATan2(Obj.Y - Y, Obj.X - X) + 180
77.                         'Move out of collision
78.                         X :+ (TouchDistance - Distance) * Cos(CollisionAngle)
79.                         Y :+ (TouchDistance - Distance) * Sin(CollisionAngle)
80.                         'Sets new Speed
81.                         Local Nx:Float = Cos(CollisionAngle)
82.                         Local Ny:Float = Sin(CollisionAngle)
83.                         'This section adjusted with code by Christian "Warpy" Perfect
84.                         'p is the projection of V onto the normal
85.                         Local Px:Float, Py:Float
86.                         Local Dotproduct:Float = xspeed * Nx + yspeed * Ny
87.                         Px = Dotproduct*Nx
88.                         Py = Dotproduct*Ny
89.                         'the velocity after hitting the wall is V - 2p, so just subtract 2*p from V
90.                         xSpeed = xSpeed - 2 * Px
91.                         ySpeed = ySpeed - 2 * Py +.01
92.                 End If
93.         End Method
94.
95.
96. End Type
97.
99.         Field inertia:Float
102.                 MoveMouse(320, 200)
103.
104.                 Spawn (newPaddle, xstart, ystart, 64)
105.
106.
107.         End Function
108.
109.         Method collide(obj:tgameobject)
110.                 Local Distance:Float = Sqr((Obj.X - X) * (Obj.X - X) + (Obj.Y - Y) * (Obj.Y - Y))
111.                 Local TouchDistance:Float = size + Obj.size
112.                 If Distance < TouchDistance
113.                         'Detect angle of collision
114.                         Local CollisionAngle:Float = ATan2(Obj.Y - Y, Obj.X - X) + 180
115.                         'Move out of collision
116.                         X :+ (TouchDistance - Distance) * Cos(CollisionAngle)
117.                         Y :+ (TouchDistance - Distance) * Sin(CollisionAngle)
118.                         'Sets new Speed
119.                         Local Nx:Float = Cos(CollisionAngle)
120.                         Local Ny:Float = Sin(CollisionAngle)
121.                         'This section adjusted with code by Christian "Warpy" Perfect
122.                         'p is the projection of V onto the normal
123.                         Local Px:Float, Py:Float
124.                         Local Dotproduct:Float = obj.xspeed * Nx + obj.yspeed * Ny
125.                         Px = Dotproduct*Nx
126.                         Py = Dotproduct*Ny
127.                         'the velocity after hitting the wall is V - 2p, so just subtract 2*p from V
128.                         obj.xSpeed = obj.xSpeed - 2 * Px
129.                         obj.ySpeed = obj.ySpeed - 2 * Py +.01
130.                         If obj.ySpeed > 0 obj.ySpeed = 1.6
131.                 End If
132.         End Method
133.
135.                 DrawGeom(x, y, 6, size, 90, 100, 255, 255)
136.                 inertia = Abs(x - MouseX())
137.                 If x > MouseX() Then x:-inertia
138.                 If x < MouseX() Then x:+inertia
139.                 For Local o:TgameObject = EachIn ObjectList
140.                         If o.Size < 40 collide(o:TgameObject)
141.                 Next
142.
143.         End Method
144. End Type
145.
146. Type tBrick Extends TgameObject
147.         Field color:Byte
148.         Field r:Double, g:Double, b:Double
149.         Field ang:Float
150.         Field z:Float
151.         Field spinspeed:Float
153.         Method Die()
154.                 ListRemove(BrickList, Self)
155.                 ListRemove(ObjectList, Self)
156.         End Method
157.         Function Create:tBrick(xstart:Double, ystart:Double, color:Byte, spinspeed:Float)
158.                 Local NewBrick:tBrick = New tBrick
159.                 Spawn (newbrick, xstart, ystart, 25)
160.                 newbrick.spinspeed = spinspeed
161.                 Select color
162.                         Case COLOR_BLUE
163.                                 NewBrick.r = 100
164.                                 NewBrick.g = 100
165.                                 NewBrick.b = 255
166.                         Case COLOR_GREEN
167.                                 NewBrick.r = 100
168.                                 NewBrick.g = 255
169.                                 NewBrick.b = 100
170.                         Case COLOR_RED
171.                                 NewBrick.r = 255
172.                                 NewBrick.g = 100
173.                                 NewBrick.b = 100
174.                 End Select
176.                 Return newbrick
177.
178.         End Function
181.                         size:+16
182.                         spinspeed:+1
183.                 End If
184.                 If size > 900 die()
185.                 ang:+spinspeed
186.                 DrawGeom(x, y, 6, size, ang, r, g, b)
187.         End Method
188. End Type
189. HideMouse()
190. For Local n:Int = 1 To 10
191.         tBrick.Create(n * 58, 100, COLOR_BLUE, -.5)
192. Next
193.
194. For Local n:Int = 1 To 10
195.         tBrick.Create(n * 58, 160, COLOR_GREEN, 6)
196. Next
197.
198. For Local n:Int = 1 To 10
199.         tBrick.Create(n * 58, 220, COLOR_RED, .5)
200. Next
201.
203. tBall.Create(320, 330)
204. SetBlend LIGHTBLEND
205.
206. Repeat
207.         Cls
208.         For Local o:TgameObject = EachIn ObjectList