### Author Topic: Flappy Bird Clone by Terry Ritchie  (Read 844 times)

0 Members and 1 Guest are viewing this topic.

#### Qwerkey

• Moderator
• Forum Resident
• Posts: 736
##### Flappy Bird Clone by Terry Ritchie
« on: April 30, 2020, 04:24:10 AM »
Flappy Bird

Author: @TerryRitchie
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=437.0
Version: 1
Tags: [Graphics], [2D], [Game], [Side-scroller]

Description:
Here's an oldie, my QB64 version of FlappyBird. I used this program as an introduction to game programming in the course I used to teach.

Source Code:
Note the source code is given here for reference.  You will need all the files from the .zip
Code: QB64: [Select]
1.     ' -----------------------------------------------
2.     ' QB64 FlappyBird Clone by Terry Ritchie 02/28/14
3.     '
4.     ' This program was created to accompany the QB64
5.     ' Game Programming course located at:
6.     ' http://www.qb64sourcecode.com
7.     '
8.     ' You may not sell or distribute this game! It
9.     ' was made for instructional purposes only.
10.     '
11.     ' Update: 04/29/20
12.     '
13.     ' Added EXE icon support in lines 18 and 19
14.     ' Any key press now starts game in line 274
15.     ' Any key press now flaps bird in line 196
16.     '
17.     ' -----------------------------------------------
18.
19.     \$EXEICON:'.\fbird.ico'
20.
21.     '--------------------------------
22.     '- Variable declaration section -
23.     '--------------------------------
24.
25.     CONST FALSE = 0 '            boolean: truth  0
26.     CONST TRUE = NOT FALSE '     boolean: truth -1
27.     CONST LARGE = 0 '            large numbers
28.     CONST SMALL = 1 '            small numbers (not used in current version of game)
29.     CONST GOLD = 0 '             gold medal
30.     CONST SILVER = 1 '           silver medal
31.     CONST LIGHT = 0 '            light colored gold/silver medal
32.     CONST DARK = 1 '             dark colored gold/silver medal
33.
34.     TYPE PARALLAX '              parallax scenery settings
35.         image AS LONG '          scene image
36.         x AS INTEGER '           scene image x location
37.         y AS INTEGER '           scene image y location
38.         frame AS INTEGER '       current parallax frame
39.         fmax AS INTEGER '        maximum parallax frames allowed
40.
41.     TYPE INFLIGHT '              flappy bird inflight characterisitcs
42.         y AS SINGLE '            flappy bird y location
43.         yvel AS SINGLE '         flappy bird y velocity
44.         flap AS INTEGER '        wing flap position
45.         flapframe AS INTEGER '   wing flap frame counter
46.         angle AS INTEGER '       angle of flappy bird
47.
48.     TYPE PIPE '                  pipe characteristics
49.         x AS INTEGER '           pipe x location
50.         y AS INTEGER '           pipe y location
51.
52.     DIM Pipes(3) AS PIPE '       define 3 moving sets of pipes
53.     DIM Pipe&(1) '               pipe images 0=top 1=bottom
54.     DIM PipeImage& '             all three pipes drawn image
55.     DIM Birdie AS INFLIGHT '     bird flight characteristics
56.     DIM Scenery(4) AS PARALLAX ' define 4 moving scenes in parallax
57.     DIM Fbird&(8, 3) '           flapping bird images
58.     DIM Num&(9, 1) '             big and small numeral images
59.     DIM Plaque& '                medal/score plaque
60.     DIM FlappyBird& '            Flappy Bird title image
61.     DIM GameOver& '              Game Over image
63.     DIM Medal&(1, 1) '           gold/silver medal images
64.     DIM Finger& '                tap finger image
65.     DIM ScoreButton& '           score button image
66.     DIM ShareButton& '           share button image
67.     DIM StartButton& '           start button image
68.     DIM OKButton& '              OK button image
69.     DIM RateButton& '            RATE button image
71.     DIM PlayButton& '            PLAY [|>] button image
72.     DIM PauseButton& '           PAUSE [||] button image
73.     DIM HazardBar& '             Hazard bar parallax image
74.     DIM Clouds& '                Clouds parallax image
75.     DIM City& '                  Cityscape parallax image
76.     DIM Bushes& '                Bushes parallax image
77.     DIM New& '                   red NEW image
78.     DIM Clean& '                 clean playing screen image
79.     DIM HitBird% '               boolean: TRUE if bird hits something
80.     DIM HighScore% '             high score
81.     DIM Score% '                 current score
82.     DIM Paused% '                boolean: TRUE if game paused
83.     DIM Ding& '                  ding sound
84.     DIM Flap& '                  flapping sound
85.     DIM Smack& '                 bird smack sound
86.     DIM Latch% '                 boolean: TRUE if mouse button held down
87.     DIM WinX% '                  stops player from exiting program at will
88.
89.     '------------------------
90.     '- Main Program Section -
91.     '------------------------
92.
93.     SCREEN _NEWIMAGE(432, 768, 32) '                        create 432x768 game screen
94.     _TITLE "FlappyBird" '                                   give window a title
95.     CLS '                                                   clear the screen
96.     _DELAY .5 '                                             slight delay before moving screen to middle
97.     _SCREENMOVE _MIDDLE '                                   move window to center of desktop
98.     WinX% = _EXIT '                                         program will handle all window close requests
100.     Birdie.flap = 1 '                                       set initial wing position of bird
101.     DO '                                                    BEGIN MAIN GAME LOOP
102.         _LIMIT 60 '                                         60 frames per second
103.         UPDATESCENERY '                                     update parallaxing scenery
104.         _PUTIMAGE (40, 265), FlappyBird& '                  place game title on screen
105.         _PUTIMAGE (350, 265), Fbird&(2, FLAPTHEBIRD%) '     place flapping bird on screen
106.         IF BUTTON%(64, 535, StartButton&) THEN PLAYGAME '   if start button pressed play game
107.         IF BUTTON%(248, 535, ScoreButton&) THEN SHOWSCORE ' if score button pressed show scores
108.         IF BUTTON%(248, 480, RateButton&) THEN RATEGAME '   if rate button pressed bring up browser
109.         _DISPLAY '                                          update screen with changes
110.     LOOP UNTIL _KEYDOWN(27) OR _EXIT '                      END MAIN GAME LOOP when ESC pressed or window closed
111.     CLEANUP '                                               clean the computer's RAM before leaving
113.
114.     '-------------------------------------
115.     '- Subroutines and Functions section -
116.     '-------------------------------------
117.
118.     '----------------------------------------------------------------------------------------------------------------------
119.
120.     FUNCTION FLAPTHEBIRD% ()
121.
122.         '*
123.         '* Returns the next index value used in Fbird&() to animate the bird's
124.         '* flapping wings.
125.         '*
126.
127.         SHARED Birdie AS INFLIGHT
128.
129.         Birdie.flapframe = Birdie.flapframe + 1 '     increment frame counter
130.         IF Birdie.flapframe = 4 THEN '                hit limit?
131.             Birdie.flapframe = 0 '                    yes, reset frame counter
132.             Birdie.flap = Birdie.flap + 1 '           increment flap counter
133.             IF Birdie.flap = 4 THEN Birdie.flap = 1 ' reset flap counter when limit hit
134.         FLAPTHEBIRD% = Birdie.flap '                  return next index value
135.
136.
137.     '----------------------------------------------------------------------------------------------------------------------
138.
139.     SUB MOVEPIPES ()
140.
141.         '*
142.         '* Creates and moves the pipe images across the screen.
143.         '*
144.
145.         SHARED Pipes() AS PIPE, Pipe&(), PipeImage&, Paused%, Score%, Ding&
146.
147.         DIM p% ' counter indicating which pipe being worked on
148.
149.         _DEST PipeImage& '                                    work on this image
150.         CLS , _RGBA32(0, 0, 0, 0) '                           clear image with transparent black
151.         _DEST 0 '                                             back to work on screen
152.         DO '                                                  BEGIN PIPE LOOP
153.             p% = p% + 1 '                                     increment pipe counter
154.             IF NOT Paused% THEN '                             is game paused?
155.                 Pipes(p%).x = Pipes(p%).x - 3 '               no, move pipe to the left
156.                 IF Pipes(p%).x < -250 THEN '                  hit lower limit?
157.                     Pipes(p%).x = 500 '                       yes, move pipe all the way right
158.                     Pipes(p%).y = -(INT(RND(1) * 384) + 12) ' generate random pipe height position
159.                 IF Pipes(p%).x = 101 THEN '                   is pipe crossing bird location?
160.                     _SNDPLAY Ding& '                          play ding sound
161.                     Score% = Score% + 1 '                     increment player score
162.             IF Pipes(p%).x > -78 AND Pipes(p%).x < 432 THEN ' is pipe currently seen on screen?
163.                 _PUTIMAGE (Pipes(p%).x, Pipes(p%).y), Pipe&(0), PipeImage& ' place top pipe
164.                 _PUTIMAGE (Pipes(p%).x, Pipes(p%).y + 576), Pipe&(1), PipeImage& ' place bottom pipe
165.         LOOP UNTIL p% = 3 '                                   END PIPE LOOP when all pipes moved
166.         _PUTIMAGE (0, 0), PipeImage& '                        place pipe image on screen
167.
168.
169.     '----------------------------------------------------------------------------------------------------------------------
170.
171.     SUB FLYBIRDIE ()
172.
173.         '*
174.         '* Controls the flight of bird on screen.
175.         '*
176.
177.         SHARED Birdie AS INFLIGHT, Fbird&(), Paused%, Flap&, HitBird%, Latch%, Smack&
178.
179.         DIM b% '     boolean: TRUE if left mouse button pressed
180.         DIM Angle% ' angle of bird in flight
181.
182.         IF NOT Paused% THEN '                             is game paused?
183.             WHILE _MOUSEINPUT: WEND '                     no, get latest mouse information
184.             b% = _MOUSEBUTTON(1) '                        get left mouse button status
185.             IF _KEYHIT > 0 THEN b% = -1 '                 any key will also make bird flap    (added 04/29/20)
186.             IF NOT b% THEN Latch% = FALSE '               release latch if button let go
187.             IF NOT HitBird% THEN '                        has bird hit something?
188.                 IF NOT Latch% THEN '                      no, has left button been release?
189.                     IF b% THEN '                          yes, was left button pressed?
190.                         Birdie.yvel = -8 '                yes, reset bird y velocity
191.                         _SNDPLAY Flap& '                  play flap sound
192.                         Latch% = TRUE '                   remember mouse button pressed
193.             Birdie.yvel = Birdie.yvel + .5 '              bleed off some bird y velocity
194.             Birdie.y = Birdie.y + Birdie.yvel '           add velocity to bird's y direction
195.             IF NOT HitBird% THEN '                        has bird hit something?
196.                 IF Birdie.y < -6 OR Birdie.y > 549 THEN ' no, has bird hit top/bottom of screen?
197.                     HitBird% = TRUE '                     yes, remeber bird hit something
198.                     _SNDPLAY Smack& '                     play smack sound
199.             IF Birdie.yvel < 0 THEN '                     is bird heading upward?
200.                 Birdie.angle = 1 '                        yes, set angle of bird accordingly
201.                 Angle% = INT(Birdie.yvel * .5) + 1 '      calculate angle according to bird velocity
202.                 IF Angle% > 8 THEN Angle% = 8 '           keep angle within limits
203.                 Birdie.angle = Angle% '                   set bird angle
204.         _PUTIMAGE (100, Birdie.y), Fbird&(Birdie.angle, FLAPTHEBIRD%) ' place bird on screen
205.
206.
207.     '----------------------------------------------------------------------------------------------------------------------
208.
210.
211.         '*
212.         '* Displays player's score on screen.
213.         '*
214.
215.         SHARED Num&(), Score%
216.
217.         DIM s\$ ' score in string format
218.         DIM w% ' width of score string
219.         DIM x% ' x location of score digits
220.         DIM p% ' position counter
221.
222.         s\$ = LTRIM\$(RTRIM\$(STR\$(Score%))) ' convert score to string
223.         w% = LEN(s\$) * 23 '                 calculate width of score
224.         x% = (432 - w%) \ 2 '               calculate x position of score
225.         FOR p% = 1 TO LEN(s\$) '             cycle through each position in score string
226.             _PUTIMAGE (x%, 100), Num&(ASC(MID\$(s\$, p%, 1)) - 48, LARGE) ' place score digit on screen
227.             x% = x% + 23 '                  move to next digit position
228.         NEXT p%
229.
230.
231.     '----------------------------------------------------------------------------------------------------------------------
232.
234.
235.         '*
236.         '* displays instructions to the player and waits for player to start game.
237.         '*
238.
240.
241.         DIM b% ' boolean: TRUE if left mouse button pressed
242.
243.         DO '                                 BEGIN READY LOOP
244.             _LIMIT 60 '                      60 frames per second
245.             UPDATESCENERY '                  move parallax scenery
246.             _PUTIMAGE (180, 350), Finger& '  place finger instructions on screen
247.             _PUTIMAGE (85, 225), GetReady& ' place get ready image on screen
248.             _PUTIMAGE (100, 375), Fbird&(2, FLAPTHEBIRD%) ' place bird on screen
249.             UPDATESCORE '                    place score on screen
250.             _DISPLAY '                       update screen with changes
251.             WHILE _MOUSEINPUT: WEND '        get latest mouse information
252.             b% = _MOUSEBUTTON(1) '           get status of left mouse button
253.             IF _KEYHIT > 0 THEN b% = -1 '    any key press will also begin game           (added 04/29/20)
254.             IF _EXIT THEN CLEANUP: SYSTEM '  leave game if user closes game window
255.         LOOP UNTIL b% '                      END READY LOOP when left button pressed
256.         _DELAY .2 '                          slight delay to allow mouse button release
257.
258.
259.     '----------------------------------------------------------------------------------------------------------------------
260.
261.     SUB PLAYGAME ()
262.
263.         '*
264.         '* Allows player to play the game.
265.         '*
266.
267.         SHARED Pipes() AS PIPE, Birdie AS INFLIGHT, PauseButton&, PlayButton&, Paused%, HitBird%, Score%
268.
269.         RANDOMIZE TIMER '                                seed random number generator
270.         Score% = 0 '                                     reset player score
271.         Birdie.y = 0 '                                   reset bird y location
272.         Birdie.yvel = 0 '                                reset bird y velocity
273.         Birdie.flap = 1 '                                reset bird wing flap index
274.         Pipes(1).x = 500 '                               reset position of first pipe
275.         Pipes(2).x = 749 '                               reset position of second pipe
276.         Pipes(3).x = 998 '                               reset position of third pipe
277.         Pipes(1).y = -(INT(RND(1) * 384) + 12) '         calculate random y position of pipe 1
278.         Pipes(2).y = -(INT(RND(1) * 384) + 12) '         calculate random y position of pipe 2
279.         Pipes(3).y = -(INT(RND(1) * 384) + 12) '         calculate random y position of pipe 3
280.         READY '                                          display instructions to player
281.         DO '                                             BEGIN GAME PLAY LOOP
282.             _LIMIT 60 '                                  60 frames per second
283.             UPDATESCENERY '                              move parallax scenery
284.             MOVEPIPES '                                  move pipes
285.             UPDATESCORE '                                display player score
286.             FLYBIRDIE '                                  move and display bird
287.             CHECKFORHIT '                                check for bird hits
288.             IF NOT Paused% THEN '                        is game paused?
289.                 IF BUTTON%(30, 100, PauseButton&) THEN ' no, was pause button pressed?
290.                     Paused% = TRUE '                     yes, place game in pause state
291.             ELSE '                                       no, game is not paused
292.                 IF BUTTON%(30, 100, PlayButton&) THEN '  was play button pressed?
293.                     Paused% = FALSE '                    yes, take game out of pause state
294.             _DISPLAY '                                   update screen with changes
295.             IF _EXIT THEN CLEANUP: SYSTEM '              leave game if user closes game window
296.         LOOP UNTIL HitBird% '                            END GAME PLAY LOOP if bird hits something
297.         DO '                                             BEGIN BIRD DROPPING LOOP
298.             _LIMIT 60 '                                  60 frames per second
299.             Paused% = TRUE '                             place game in paused state
300.             UPDATESCENERY '                              draw parallax scenery
301.             MOVEPIPES '                                  draw pipes
302.             Paused% = FALSE '                            take game out of pause state
303.             FLYBIRDIE '                                  move bird on screen
304.             _DISPLAY '                                   update screen with changes
305.             IF _EXIT THEN CLEANUP: SYSTEM '              leave game if user closes game window
306.         LOOP UNTIL Birdie.y >= 546 '                     END BIRD DROPPING LOOP when bird hits ground
307.         SHOWSCORE '                                      display player's score plaque
308.         HitBird% = FALSE '                               reset bird hit indicator
309.
310.
311.     '----------------------------------------------------------------------------------------------------------------------
312.
313.     SUB CHECKFORHIT ()
314.
315.         '*
316.         '* Detects if bird hits a pipe.
317.         '*
318.
319.         SHARED Pipes() AS PIPE, Birdie AS INFLIGHT, HitBird%, Smack&
320.
321.         DIM p% ' pipe counter
322.
323.         FOR p% = 1 TO 3 '                                      cycle through all pipe positions
324.             IF Pipes(p%).x <= 153 AND Pipes(p%).x >= 22 THEN ' is pipe in bird territory?
325.                 IF BOXCOLLISION(105, Birdie.y + 6, 43, 41, Pipes(p%).x, Pipes(p%).y, 78, 432) THEN ' collision?
326.                     HitBird% = TRUE '                          yes, remember bird hit pipe
327.                 IF BOXCOLLISION(105, Birdie.y + 6, 43, 41, Pipes(p%).x, Pipes(p%).y + 576, 78, 432) THEN ' collision?
328.                     HitBird% = TRUE '                          yes, remember bird hit pipe
329.         NEXT p%
330.         IF HitBird% THEN _SNDPLAY Smack& '                     play smack sound if bird hit pipe
331.
332.
333.     '----------------------------------------------------------------------------------------------------------------------
334.
335.     SUB RATEGAME ()
336.
337.         '*
338.         '* Allows player to rate game.
339.         '*
340.
341.         SHELL "https://www.qb64.org/forum/index.php?topic=437.0" ' go to QB64 web site forum area for flappy bird
342.
343.
344.     '----------------------------------------------------------------------------------------------------------------------
345.
346.     SUB SHOWSCORE ()
347.
348.         '*
349.         '* Display's current and high scores on score plaque
350.         '*
351.
352.         SHARED Fbird&(), Num&(), Medal&(), FlappyBird&, GameOver&, Plaque&, OKButton&, ShareButton&
353.         SHARED HitBird%, HighScore%, Score%, New&
354.
355.         DIM Ok% '        boolean: TRUE if OK button pressed
356.         DIM Scores%(1) ' current and high scores
357.         DIM sc% '        current score being drawn
358.         DIM x% '         x location of score digits
359.         DIM p% '         digit position counter
360.         DIM ShowNew% '   boolean: TRUE if score is a new high score
361.         DIM s\$ '         score in string format
362.
363.         IF Score% > HighScore% THEN '                               is this a new high score?
364.             OPEN "fbird.sco" FOR OUTPUT AS #1 '                     yes, open score file
365.             PRINT #1, Score% '                                      save new high score
366.             CLOSE #1 '                                              close score file
367.             HighScore% = Score% '                                   remember new high score
368.             ShowNew% = TRUE '                                       remember this is a new high score
369.         Scores%(0) = Score% '                                       place score in array
370.         Scores%(1) = HighScore% '                                   place high score in array
371.         Ok% = FALSE '                                               reset OK button status indicator
372.         DO '                                                        BEGIN SCORE LOOP
373.             _LIMIT 60 '                                             60 frames per second
374.             IF HitBird% THEN '                                      did bird hit something?
375.                 _PUTIMAGE (75, 200), GameOver& '                    yes, place game over image on screen
376.             ELSE '                                                  no, bird did not hit anything
377.                 UPDATESCENERY '                                     move parallax scenery
378.                 _PUTIMAGE (40, 200), FlappyBird& '                  place flappy bird title on screen
379.                 _PUTIMAGE (350, 200), Fbird&(2, FLAPTHEBIRD%) '     place flapping bird on screen
380.             _PUTIMAGE (46, 295), Plaque& '                          place plaque on screen
381.             SELECT CASE HighScore% '                                what is range of high score?
382.                 CASE 25 TO 49 '                                     from 25 to 49
383.                     _PUTIMAGE (85, 360), Medal&(SILVER, LIGHT) '    display a light silver medal
384.                 CASE 50 TO 99 '                                     from 50 to 99
385.                     _PUTIMAGE (85, 360), Medal&(SILVER, DARK) '     display a dark silver medal
386.                 CASE 100 TO 199 '                                   from 100 to 199
387.                     _PUTIMAGE (85, 360), Medal&(GOLD, LIGHT) '      display a light gold medal
388.                 CASE IS > 199 '                                     from 200 and beyond
389.                     _PUTIMAGE (85, 360), Medal&(GOLD, DARK) '       display a dark gold medal
390.             FOR sc% = 0 TO 1 '                                      cycle through both scores
391.                 s\$ = LTRIM\$(RTRIM\$(STR\$(Scores%(sc%)))) '           convert score to string
392.                 x% = 354 - LEN(s\$) * 23 '                           calculate position of score digit
393.                 FOR p% = 1 TO LEN(s\$) '                             cycle through score string
394.                     _PUTIMAGE (x%, 346 + sc% * 64), Num&(ASC(MID\$(s\$, p%, 1)) - 48, LARGE) ' place digit on plaque
395.                     x% = x% + 23 '                                  increment digit position
396.                 NEXT p%
397.             NEXT sc%
398.             IF ShowNew% THEN _PUTIMAGE (250, 382), New& '           display red new image if new high score
399.             IF BUTTON%(64, 535, OKButton&) THEN Ok% = TRUE '        remember if OK button was pressed
400.             IF BUTTON%(248, 535, ShareButton&) THEN '               was share button pressed?
401.                 SHAREPROGRAM '                                      yes, share program with others
402.                 UPDATESCENERY '                                     draw parallax scenery
403.                 MOVEPIPES '                                         draw pipes
404.             _DISPLAY '                                              update screen with changes
405.             IF _EXIT THEN CLEANUP: SYSTEM '                         leave game if user closes game window
406.         LOOP UNTIL Ok% '                                            END SCORE LOOP when OK button pressed
407.
408.
409.     '----------------------------------------------------------------------------------------------------------------------
410.
411.     SUB SHAREPROGRAM ()
412.
413.         '*
414.         '* Allows player to share program with others
415.         '*
416.
417.         SHARED Fbird&(), FlappyBird&, OKButton&
418.
419.         DIM Message& ' composed message to player's friend(s)
420.         DIM Ok% '      boolean: TRUE if OK button pressed
421.
422.         Message& = _NEWIMAGE(339, 174, 32) '                   create image to hold message to player
423.         _CLIPBOARD\$ = "I just discovered a great game! You can download it here: http:\\www.qb64sourcecode.com\fbird.exe"
424.         _PRINTMODE _KEEPBACKGROUND '                           printed text will save background
425.         LINE (58, 307)-(372, 453), _RGB32(219, 218, 150), BF ' clear plaque image
426.         COLOR _RGB32(210, 170, 79) '                           compose message to player on plaque
427.         _PRINTSTRING (66, 316), "The following message has been copied"
428.         COLOR _RGB32(82, 55, 71)
429.         _PRINTSTRING (65, 315), "The following message has been copied"
430.         COLOR _RGB32(210, 170, 79)
431.         _PRINTSTRING (66, 331), "to your computer's clipboard:"
432.         COLOR _RGB32(82, 55, 71)
433.         _PRINTSTRING (65, 330), "to your computer's clipboard:"
434.         COLOR _RGB32(210, 170, 79)
435.         _PRINTSTRING (66, 351), "'I just discovered a great game! You"
436.         COLOR _RGB32(82, 55, 71)
437.         _PRINTSTRING (65, 350), "'I just discovered a great game! You"
438.         COLOR _RGB32(210, 170, 79)
440.         COLOR _RGB32(82, 55, 71)
442.         COLOR _RGB32(210, 170, 79)
443.         _PRINTSTRING (66, 381), "www.qb64sourcecode.com\fbird.exe'"
444.         COLOR _RGB32(82, 55, 71)
445.         _PRINTSTRING (65, 380), "www.qb64sourcecode.com\fbird.exe'"
446.         COLOR _RGB32(210, 170, 79)
447.         _PRINTSTRING (66, 401), "Create an email for your friends and"
448.         COLOR _RGB32(82, 55, 71)
449.         _PRINTSTRING (65, 400), "Create an email for your friends and"
450.         COLOR _RGB32(210, 170, 79)
451.         _PRINTSTRING (66, 416), "paste this message into it! Go ahead,"
452.         COLOR _RGB32(82, 55, 71)
453.         _PRINTSTRING (65, 415), "paste this message into it! Go ahead,"
454.         COLOR _RGB32(210, 170, 79)
455.         _PRINTSTRING (66, 431), "do it now before you change your mind!"
456.         COLOR _RGB32(82, 55, 71)
457.         _PRINTSTRING (65, 430), "do it now before you change your mind!"
458.         _PUTIMAGE , _DEST, Message&, (46, 295)-(384, 468) '    place message in image
459.         DO '                                                   BEGIN SHARE LOOP
460.             _LIMIT 60 '                                        60 frames per second
461.             UPDATESCENERY '                                    move parallax scenery
462.             _PUTIMAGE (40, 200), FlappyBird& '                 place flappy bird title on screen
463.             _PUTIMAGE (350, 200), Fbird&(2, FLAPTHEBIRD%) '    place flapping bird on screen
464.             _PUTIMAGE (46, 295), Message& '                    place message on plaque
465.             IF BUTTON%(156, 535, OKButton&) THEN Ok% = TRUE '  remeber if OK button pressed
466.             _DISPLAY '                                         update screen with changes
467.             IF _EXIT THEN CLEANUP: SYSTEM '                    leave game if user closes game window
468.         LOOP UNTIL Ok% '                                       END SHRE LOOP when OK button pressed
469.         _FREEIMAGE Message& '                                  message image no longer needed
470.
471.
472.     '----------------------------------------------------------------------------------------------------------------------
473.
474.     FUNCTION BUTTON% (xpos%, ypos%, Image&)
475.
476.         '*
477.         '* Creates a button on the screen the player can click with the mouse button.
478.         '*
479.         '* xpos%  - x coordinate position of button on screen
480.         '* ypos%  - y coordinate position of button on screen
481.         '* Image& - button image
482.         '*
483.         '* Returns: boolean: TRUE  if button pressed
484.         '*                   FALSE if button not pressed
485.         '*
486.
487.         DIM x% ' current mouse x coordinate
488.         DIM y% ' current mouse y coordinate
489.         DIM b% ' boolean: TRUE if left mouse button pressed
490.
491.         _PUTIMAGE (xpos%, ypos%), Image& '                      place button image on the screen
492.         WHILE _MOUSEINPUT: WEND '                               get latest mouse information
493.         x% = _MOUSEX '                                          get current mouse x coordinate
494.         y% = _MOUSEY '                                          get current mouse y coordinate
495.         b% = _MOUSEBUTTON(1)
496.         IF b% THEN '                                            is left mouse button pressed?
497.             IF x% >= xpos% THEN '                               yes, is mouse x within lower limit of button?
498.                 IF x% <= xpos% + _WIDTH(Image&) THEN '          yes, is mouse x within upper limit of button?
499.                     IF y% >= ypos% THEN '                       yes, is mouse y within lower limit of button?
500.                         IF y% <= ypos% + _HEIGHT(Image&) THEN ' yes, is mouse y within upper limit of button?
501.                             BUTTON% = TRUE '                    yes, remember that button was clicked on
502.                             _DELAY .2 '                         slight delay to allow button to release
503.
504.
505.     '----------------------------------------------------------------------------------------------------------------------
506.
508.
509.         '*
510.         '* Updates the moving parallax scenery
511.         '*
512.
513.         SHARED Scenery() AS PARALLAX, Clean&, HazardBar&, Paused%
514.
515.         DIM c% ' scenery index indicator
516.
517.         _PUTIMAGE , Clean& '                                              clear screen with clean image
518.         DO '                                                              BEGIN SCENERY LOOP
519.             c% = c% + 1 '                                                 increment index value
520.             IF NOT Paused% THEN '                                         is game in paused state?
521.                 Scenery(c%).frame = Scenery(c%).frame + 1 '               no, update frame counter of current scenery
522.                 IF Scenery(c%).frame = Scenery(c%).fmax THEN '            frame counter hit limit?
523.                     Scenery(c%).frame = 0 '                               yes, reset frame counter
524.                     Scenery(c%).x = Scenery(c%).x - 1 '                   move scenery 1 pixel to left
525.                     IF Scenery(c%).x = -432 THEN '                        scenery hit lower limit?
526.                         Scenery(c%).x = 0 '                               yes, reset scenery to start position
527.             _PUTIMAGE (Scenery(c%).x, Scenery(c%).y), Scenery(c%).image ' place current scenery on screen
528.         LOOP UNTIL c% = 3 '                                               END SCENERY LOOP when all scenery updated
529.         IF NOT Paused% THEN '                                             is game in paused state?
530.             Scenery(4).x = Scenery(4).x - 3 '                             no, move hazard bar 3 pixels to left
531.             IF Scenery(4).x = -21 THEN Scenery(4).x = 0 '                 reset to start position if lower limit hit
532.         _PUTIMAGE (Scenery(4).x, Scenery(4).y), HazardBar& '              place hazard bar on screen
533.
534.
535.     '----------------------------------------------------------------------------------------------------------------------
536.
538.
539.         '*
540.         '* Loads game graphics, sounds and initial settings.
541.         '*
542.
543.         SHARED Scenery() AS PARALLAX, Birdie AS INFLIGHT, Pipes() AS PIPE, Pipe&(), Fbird&()
544.         SHARED Num&(), Medal&(), Plaque&, FlappyBird&, GameOver&, GetReady&, Finger&
545.         SHARED ScoreButton&, ShareButton&, StartButton&, OKButton&, RateButton&, MenuButton&
546.         SHARED PlayButton&, PauseButton&, HazardBar&, Clouds&, City&, Bushes&, New&, Clean&
547.         SHARED HighScore%, PipeImage&, Ding&, Flap&, Smack&
548.
549.         DIM Sheet& '    sprite sheet image
550.         DIM x% '        generic counter
551.         DIM y% '        generic counter
552.         DIM PipeTop& '  temporary top of pipe image
553.         DIM PipeTube& ' temporary pipe tube image
554.
555.         Ding& = _SNDOPEN("fbding.ogg", "VOL,SYNC") '         load game sounds
556.         Flap& = _SNDOPEN("fbflap.ogg", "VOL,SYNC")
557.         Smack& = _SNDOPEN("fbsmack.ogg", "VOL,SYNC")
559.         FOR y% = 0 TO 2 '                                          cycle through bird image rows
560.             FOR x% = 0 TO 7 '                                      cycle through bird image columns
561.                 Fbird&(x% + 1, y% + 1) = _NEWIMAGE(53, 53, 32) '   create image holder then get image
562.                 _PUTIMAGE , Sheet&, Fbird&(x% + 1, y% + 1), (x% * 53, y% * 53)-(x% * 53 + 52, y% * 53 + 52)
563.             NEXT x%
564.         NEXT y%
565.         FOR x% = 0 TO 9 '                                          cycle trough 9 numeral images
566.             Num&(x%, 0) = _NEWIMAGE(21, 30, 32) '                  create image holder for big
567.             Num&(x%, 1) = _NEWIMAGE(18, 21, 32) '                  create image holder for small
568.             _PUTIMAGE , Sheet&, Num&(x%, 0), (x% * 21, 159)-(x% * 21 + 20, 188) ' get images
569.             _PUTIMAGE , Sheet&, Num&(x%, 1), (x% * 18 + 210, 159)-(x% * 18 + 227, 179)
570.         NEXT x%
571.         Plaque& = _NEWIMAGE(339, 174, 32) '                        define remaining image sizes
572.         FlappyBird& = _NEWIMAGE(288, 66, 32)
573.         GameOver& = _NEWIMAGE(282, 57, 32)
574.         GetReady& = _NEWIMAGE(261, 66, 32)
575.         PipeTop& = _NEWIMAGE(78, 36, 32)
576.         PipeTube& = _NEWIMAGE(78, 36, 32)
577.         Pipe&(0) = _NEWIMAGE(78, 432, 32)
578.         Pipe&(1) = _NEWIMAGE(78, 432, 32)
579.         PipeImage& = _NEWIMAGE(432, 596, 32)
580.         Medal&(0, 0) = _NEWIMAGE(66, 66, 32)
581.         Medal&(0, 1) = _NEWIMAGE(66, 66, 32)
582.         Medal&(1, 0) = _NEWIMAGE(66, 66, 32)
583.         Medal&(1, 1) = _NEWIMAGE(66, 66, 32)
584.         Finger& = _NEWIMAGE(117, 147, 32)
585.         ScoreButton& = _NEWIMAGE(120, 42, 32)
586.         ShareButton& = _NEWIMAGE(120, 42, 32)
587.         StartButton& = _NEWIMAGE(120, 42, 32)
588.         OKButton& = _NEWIMAGE(120, 42, 32)
589.         RateButton& = _NEWIMAGE(120, 42, 32)
590.         MenuButton& = _NEWIMAGE(120, 42, 32)
591.         PlayButton& = _NEWIMAGE(39, 42, 32)
592.         PauseButton& = _NEWIMAGE(39, 42, 32)
593.         HazardBar& = _NEWIMAGE(462, 24, 32)
594.         Clouds& = _NEWIMAGE(864, 120, 32)
595.         City& = _NEWIMAGE(864, 57, 32)
596.         Bushes& = _NEWIMAGE(864, 27, 32)
597.         New& = _NEWIMAGE(48, 21, 32)
598.         _PUTIMAGE , Sheet&, Plaque&, (0, 189)-(338, 362) '         grab images from sprite sheet
599.         _PUTIMAGE , Sheet&, FlappyBird&, (0, 363)-(287, 428)
600.         _PUTIMAGE , Sheet&, GameOver&, (588, 246)-(869, 302)
601.         _PUTIMAGE , Sheet&, GetReady&, (588, 303)-(847, 368)
602.         _PUTIMAGE , Sheet&, Medal&(0, 0), (339, 327)-(404, 392)
603.         _PUTIMAGE , Sheet&, Medal&(0, 1), (405, 327)-(470, 392)
604.         _PUTIMAGE , Sheet&, Medal&(1, 0), (339, 261)-(404, 326)
605.         _PUTIMAGE , Sheet&, Medal&(1, 1), (405, 261)-(470, 326)
606.         _PUTIMAGE , Sheet&, Finger&, (471, 246)-(587, 392)
607.         _PUTIMAGE , Sheet&, ScoreButton&, (288, 417)-(407, 458)
608.         _PUTIMAGE , Sheet&, ShareButton&, (408, 417)-(527, 458)
609.         _PUTIMAGE , Sheet&, StartButton&, (528, 417)-(647, 458)
610.         _PUTIMAGE , Sheet&, OKButton&, (424, 204)-(543, 245)
611.         _PUTIMAGE , Sheet&, RateButton&, (544, 204)-(663, 245)
612.         _PUTIMAGE , Sheet&, MenuButton&, (664, 204)-(783, 245)
613.         _PUTIMAGE , Sheet&, PlayButton&, (784, 204)-(822, 245)
614.         _PUTIMAGE , Sheet&, PauseButton&, (823, 204)-(861, 245)
615.         _PUTIMAGE , Sheet&, HazardBar&, (288, 393)-(749, 416)
616.         _PUTIMAGE (0, 0)-(431, 119), Sheet&, Clouds&, (424, 0)-(855, 119)
617.         _PUTIMAGE (432, 0)-(863, 119), Sheet&, Clouds&, (424, 0)-(855, 119)
618.         _PUTIMAGE (0, 0)-(431, 56), Sheet&, City&, (424, 120)-(855, 176)
619.         _PUTIMAGE (432, 0)-(863, 56), Sheet&, City&, (424, 120)-(855, 176)
620.         _PUTIMAGE (0, 0)-(431, 26), Sheet&, Bushes&, (424, 177)-(855, 203)
621.         _PUTIMAGE (432, 0)-(863, 26), Sheet&, Bushes&, (424, 177)-(855, 203)
622.         _PUTIMAGE , Sheet&, New&, (289, 363)-(336, 383)
623.         _PUTIMAGE , Sheet&, PipeTop&, (339, 189)-(416, 224)
624.         _PUTIMAGE , Sheet&, PipeTube&, (339, 225)-(416, 260)
625.         _PUTIMAGE (0, 431)-(77, 395), PipeTop&, Pipe&(0) '         create bottom of upper tube image
626.         _PUTIMAGE (0, 0), PipeTop&, Pipe&(1) '                     create top of lower tube image
627.         FOR y% = 0 TO 395 STEP 36 '                                cycle through tube body of pipes
628.             _PUTIMAGE (0, y% + 35)-(77, y%), PipeTube&, Pipe&(0) ' draw tube on upper pipe image
629.             _PUTIMAGE (0, 36 + y%), PipeTube&, Pipe&(1) '          draw tube on lower pipe image
630.         NEXT y%
631.         _FREEIMAGE PipeTop& '                                      temporary image no longer needed
632.         _FREEIMAGE PipeTube& '                                     temporary image no longer needed
633.         _FREEIMAGE Sheet& '                                        sprite sheet no longer needed
634.         Clean& = _NEWIMAGE(432, 768, 32) '                         create clean image holder
635.         _DEST Clean& '                                             work on clean image
636.         CLS , _RGB32(84, 192, 201) '                               clear image with sky blue color
637.         LINE (0, 620)-(431, 767), _RGB32(219, 218, 150), BF '      create brown ground portion of image
638.         LINE (0, 577)-(431, 595), _RGB32(100, 224, 117), BF '      create green grass portion of image
639.         _DEST 0 '                                                  back to work on screen
640.         Scenery(1).image = Clouds& '                               set scenery parallax information
641.         Scenery(1).y = 457
642.         Scenery(1).fmax = 8
643.         Scenery(2).image = City&
644.         Scenery(2).y = 510
645.         Scenery(2).fmax = 4
646.         Scenery(3).image = Bushes&
647.         Scenery(3).y = 550
648.         Scenery(3).fmax = 2
649.         Scenery(4).image = HazardBar&
650.         Scenery(4).y = 596
651.         IF _FILEEXISTS("fbird.sco") THEN '                         does high score file exist?
652.             OPEN "fbird.sco" FOR INPUT AS #1 '                     yes, open high score file
653.             INPUT #1, HighScore% '                                 get high score from file
654.             CLOSE #1 '                                             close high score file
655.
656.
657.     '----------------------------------------------------------------------------------------------------------------------
658.
659.     FUNCTION BOXCOLLISION% (Box1X%, Box1Y%, Box1Width%, Box1Height%, Box2X%, Box2Y%, Box2Width%, Box2Height%)
660.
661.         '**
662.         '** Detects if two bounding box areas are in collision
663.         '**
664.         '** INPUT : Box1X%      - upper left corner X location of bounding box 1
665.         '**         Box1Y%      - upper left corner Y location of bounding box 1
666.         '**         Box1Width%  - the width of bounding box 1
667.         '**         Box1Height% - the height of bounding box 1
668.         '**         Box2X%      - upper left corner X location of bounding box 2
669.         '**         Box2Y%      - upper left corner Y location of bounding box 2
670.         '**         Box2Width%  - the width of bounding box 2
671.         '**         Box2Height% - the height of bounding box 2
672.         '**
673.         '** OUTPUT: BOXCOLLISION - 0 (FALSE) for no collision, -1 (TRUE) for collision
674.         '**
675.
676.         IF Box1X% <= Box2X% + Box2Width% - 1 THEN '              is box1 x within lower limit of box2 x?
677.             IF Box1X% + Box1Width% - 1 >= Box2X% THEN '          yes, is box1 x within upper limit of box2 x?
678.                 IF Box1Y% <= Box2Y% + Box2Height% - 1 THEN '     yes, is box1 y within lower limit of box2 y?
679.                     IF Box1Y% + Box1Height% - 1 >= Box2Y% THEN ' yes, is box1 y within upper limit of box2 y?
680.                         BOXCOLLISION% = TRUE '                   yes, then a collision occured, return result
681.
682.
683.     '----------------------------------------------------------------------------------------------------------------------
684.
685.     SUB CLEANUP ()
686.
687.         '*
688.         '* Removes all game assets from the computer's RAM.
689.         '*
690.
691.         SHARED Fbird&(), Pipe&(), Num&(), Medal&(), Plaque&, FlappyBird&, GameOver&, GetReady&
692.         SHARED Finger&, ScoreButton&, ShareButton&, StartButton&, OKButton&, RateButton&
693.         SHARED MenuButton&, PlayButton&, PauseButton&, HazardBar&, Clouds&, City&, Bushes&
694.         SHARED New&, Clean&, PipeImage&, Ding&, Flap&, Smack&
695.
696.         DIM x% '              generic counter
697.         DIM y% '              generic counter
698.
699.         _SNDCLOSE Ding& '                           remove game sounds from RAM
700.         _SNDCLOSE Flap&
701.         _SNDCLOSE Smack&
702.         FOR y% = 0 TO 2 '                           cycle through bird image rows
703.             FOR x% = 0 TO 7 '                       cycle through bird image columns
704.                 _FREEIMAGE Fbird&(x% + 1, y% + 1) ' remove bird image from RAM
705.             NEXT x%
706.         NEXT y%
707.         FOR x% = 0 TO 9 '                           cycle trough 9 numeral images
708.             _FREEIMAGE Num&(x%, 0) '                remove large numeral image from RAM
709.             _FREEIMAGE Num&(x%, 1) '                remove small numeral image from RAM
710.         NEXT x%
711.         _FREEIMAGE Plaque& '                        remove all remaining images from RAM
712.         _FREEIMAGE FlappyBird&
713.         _FREEIMAGE GameOver&
715.         _FREEIMAGE Pipe&(0)
716.         _FREEIMAGE Pipe&(1)
717.         _FREEIMAGE PipeImage&
718.         _FREEIMAGE Medal&(0, 0)
719.         _FREEIMAGE Medal&(0, 1)
720.         _FREEIMAGE Medal&(1, 0)
721.         _FREEIMAGE Medal&(1, 1)
722.         _FREEIMAGE Finger&
723.         _FREEIMAGE ScoreButton&
724.         _FREEIMAGE ShareButton&
725.         _FREEIMAGE StartButton&
726.         _FREEIMAGE OKButton&
727.         _FREEIMAGE RateButton&
729.         _FREEIMAGE PlayButton&
730.         _FREEIMAGE PauseButton&
731.         _FREEIMAGE HazardBar&
732.         _FREEIMAGE Clouds&
733.         _FREEIMAGE City&
734.         _FREEIMAGE Bushes&
735.         _FREEIMAGE New&
736.         _FREEIMAGE Clean&
737.
738.
739.     '----------------------------------------------------------------------------------------------------------------------
740.

« Last Edit: April 30, 2020, 04:32:51 AM by Qwerkey »