├── LICENSE ├── README.md ├── _config.yml ├── resources └── screenshot1.png └── src └── mini-racing.pb /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 xdvrx1 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mini-Racing 2D Game 2 | 3 | [![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fxdvrx1%2FPureBasic-2D-Game&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=PAGE+VIEWS&edge_flat=false)](https://hits.seeyoufarm.com) 4 | 5 | ![GUI](resources/screenshot1.png) 6 | 7 | I'm happy to share with you my simple 2D game purely written in 8 | PureBasic. 9 | 10 | When you create games, whether 2D or 3D, there is no such thing 11 | as *real movement*. Rather, when you flip the frames and you 12 | had changed the position of an object relative to its original position, 13 | you create the illusion of movement! That's all there is in creating 14 | computer games! Of course, complex movements will require math computation. 15 | 16 | Remember the screen resolution: 1024x768. This is the most common one. 17 | When there is an error, don't worry, it will be handled by the 18 | `HandleError` in the source code. 19 | 20 | ## Disclaimer 21 | Please note that this project is presented as a showcase of my work during a 22 | specific period. It represents a snapshot of my skills and accomplishments 23 | at that time. As such, this project is no longer actively maintained or updated. 24 | It is kept public for demonstration purposes and may not reflect my current 25 | abilities or the latest best practices in the field. 26 | 27 | However, feel free to learn from this archived project, 28 | preserved as it was during that specific period ! 29 | 30 | ## Compiling 31 | To compile/run, just click `Compile/Run` in the PureBasic IDE. 32 | To create executable file, go to `Compiler` tab and click 33 | `Create Executable`. You will be asked to name your executable 34 | file. 35 | 36 | ## License 37 | MIT- the permissive license 38 | 39 | ## The Good Old Days 40 | PureBasic was my first ever programming language, so it reminds me of the good old days. 41 | Before PureBasic, I first learned Excel formulas, which really captured my interest. 42 | The very first time I encountered Excel was in college, 43 | and it was the IF function presented by the professor that really caught my attention. 44 | 45 | ## More PureBasic Projects 46 | for more PureBasic discussion and other details, 47 | check the Main Page -> [PureBasic](https://github.com/jdevfullstack/PureBasic) 48 | 49 | ## More Of My Content 50 | - [jdevfullstack Profile](https://github.com/jdevfullstack) 51 | - [jdevfullstack Repos](https://github.com/jdevfullstack?tab=repositories) 52 | - [jdevfullstack Projects](https://github.com/jdevfullstack-projects) 53 | - [jdevfullstack Tutorials](https://github.com/jdevfullstack-tutorials) 54 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /resources/screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfullstackdev/PureBasic-2D-Game/e76be31d8410f593c7df70f2840b6a26eb375c53/resources/screenshot1.png -------------------------------------------------------------------------------- /src/mini-racing.pb: -------------------------------------------------------------------------------- 1 | ;Set the width, height and bit depth of the screen 2 | Global ScrW.l = 1024 3 | Global ScrH.l = 768 4 | Global ScrD.l = 32 5 | Global Quit.b = #False 6 | Global Start.b = #False 7 | Global KeepMoving.b = #True 8 | 9 | ;this can easily be changed for manual testing 10 | Racer1 = 0 11 | Racer2 = 0 12 | Racer3 = 0 13 | FirstPlace$ = "" 14 | 15 | Header$ = "Mini-Racing Competition by xdvrx1" 16 | 17 | ;Simple error checking procedure 18 | Procedure HandleError(Result.l, Text.s) 19 | If Result = 0 20 | MessageRequester("Error", Text, #PB_MessageRequester_Ok) 21 | End 22 | EndIf 23 | EndProcedure 24 | 25 | ;initialize environment through HandleError Procedure 26 | HandleError(InitSprite(), "InitSprite() command failed.") 27 | HandleError(InitKeyboard(), "InitKeyboard() command failed.") 28 | HandleError(OpenScreen(ScrW, ScrH, ScrD, "Error"), "Could not open screen.") 29 | 30 | ;common framerate 31 | SetFrameRate(60) 32 | 33 | ;my fave font 34 | Font1 = LoadFont(#PB_Any, "Consolas", 22) 35 | 36 | ;main loop: 37 | ;you want to take note of this 38 | ;main loop, because there is a tendency 39 | ;you put another iteration in one 40 | ;part that will cause the program to crash 41 | ;because there will be no exit condition 42 | Repeat 43 | ClearScreen(RGB(0, 0, 0)) 44 | 45 | If StartDrawing (ScreenOutput()) 46 | ;the area of the game 47 | DrawingFont(FontID(Font1)) 48 | DrawText(ScrW/2-250, 10,Header$, RGB(244, 232, 102),RGB(0, 0, 0)) 49 | LineXY(ScrW-40, ScrH, ScrW-40,0, RGB(255, 0, 200)) 50 | DrawRotatedText(ScrW-45, ScrH/2+100, "FINISH LINE", 90,(RGB(255,0,200))) 51 | 52 | ;racer1 53 | Line(0, ScrH-200, ScrW, 1, RGB(255, 255, 255)) 54 | Box(Racer1, ScrH-95, 60, 40, RGB(0, 150, 0)) 55 | DrawText(50, ScrH-130+20, Str(Racer1)) 56 | DrawText(20, ScrH-170+20, "Racer 1") 57 | 58 | ;racer2 59 | Line(0, ScrH/2-150, ScrW,1, RGB(255, 255, 255)) 60 | Box(Racer2, ScrH/2, 60, 40 , RGB(0, 0, 150)) 61 | DrawText(50, ScrH/2-20, Str(Racer2)) 62 | DrawText(20, ScrH/2-60, "Racer 2") 63 | 64 | ;racer3 65 | Box(Racer3, 90, 60, 40, RGB(150, 0, 0)) 66 | DrawText(50, 80, Str(Racer3)) 67 | DrawText(20, 40, "Racer 3") 68 | 69 | ;draw text if paused 70 | If Start = #False 71 | DrawText(80, ScrH/2-140, "Press `R` to start/pause |") 72 | DrawText(Scrw/2, ScrH/2-140, "Press `Space` to restart") 73 | DrawText(Scrw/2-180, ScrH/2-100, "Press `esc` to terminate") 74 | EndIf 75 | 76 | If KeepMoving = #True 77 | ;any if condition here 78 | ;that evaluates to true 79 | ;(`KeepMoving` becomes false) 80 | ;will make the flip buffers to stop 81 | If Racer3 >= (ScrW-100+1) 82 | KeepMoving = #False 83 | EndIf 84 | 85 | If Racer1 >= (ScrW-100+1) 86 | KeepMoving = #False 87 | EndIf 88 | 89 | If Racer2 >= (ScrW-100+1) 90 | KeepMoving = #False 91 | EndIf 92 | 93 | EndIf 94 | 95 | If KeepMoving = #False 96 | 97 | ;for first place 98 | If Racer1 > Racer2 99 | If Racer1 > Racer3 100 | FirstPlace$ = "Racer 1" 101 | DrawText(140, ScrH-130+20, "Winner: " + FirstPlace$) 102 | EndIf 103 | EndIf 104 | 105 | If Racer2 > Racer1 106 | If Racer2 > Racer3 107 | FirstPlace$ = "Racer 2" 108 | DrawText(140 ,ScrH/2-20, "Winner: " + FirstPlace$) 109 | EndIf 110 | EndIf 111 | 112 | If Racer3 > Racer1 113 | If Racer3 > Racer2 114 | FirstPlace$ = "Racer 3" 115 | DrawText(140, 80, "Winner: " + FirstPlace$) 116 | EndIf 117 | EndIf 118 | 119 | ;for first place draw 120 | ;Racer 1 & Racer 2 Tie 121 | If Racer2 = Racer1 122 | If Racer2 > Racer3 123 | DrawText(Scrw/2-150, ScrH/2, "Racer 1 & Racer 2 Tie") 124 | EndIf 125 | EndIf 126 | 127 | ;Racer 1 & Racer 3 Tie 128 | If Racer1 = Racer3 129 | If Racer1 > Racer2 130 | DrawText(Scrw/2-150, ScrH/2, "Racer 1 & Racer 3 Tie") 131 | EndIf 132 | EndIf 133 | 134 | ;Racer 3 & Racer 2 Tie 135 | If Racer3 = Racer2 136 | If Racer3 > Racer1 137 | DrawText(Scrw/2-150, ScrH/2, "Racer 3 & Racer 2 Tie") 138 | EndIf 139 | EndIf 140 | 141 | ;all draw 142 | If Racer1 = Racer2 And Racer2 = Racer3 143 | DrawText(Scrw/2-40, ScrH/2, "Draw!") 144 | EndIf 145 | EndIf 146 | 147 | EndIf 148 | 149 | StopDrawing( ) 150 | 151 | ;this is where the illusion 152 | ;of movement is created 153 | FlipBuffers( ) 154 | 155 | ;don't get values here 156 | ;it's the control for the movement 157 | ;Racer1, Racer2, Racer3 158 | ;they are the `x` positions of the 159 | ;three boxes, so it seems when `x` 160 | ;position is changed in every frame 161 | ;there is horizontal movement, 162 | ;but there is none 163 | If Start = #True 164 | If KeepMoving = #True 165 | 166 | RandomInt3 = Random(3) 167 | Racer3 = Racer3 + 5 + RandomInt3 168 | 169 | RandomInt1 = Random(3) 170 | Racer1 = Racer1 + 5 + RandomInt1 171 | 172 | RandomInt2 = Random(3) 173 | Racer2 = Racer2 + 5 + RandomInt2 174 | 175 | EndIf 176 | EndIf 177 | 178 | If ExamineKeyboard() 179 | 180 | If KeyboardPushed(#PB_Key_Space) 181 | Racer1 = 0 182 | Racer2 = 0 183 | Racer3 = 0 184 | KeepMoving = #True 185 | FirstPlace$ = "" 186 | EndIf 187 | 188 | If KeyboardReleased(#PB_Key_R) 189 | ;make `R` key a push button 190 | If Start = #False 191 | Start = #True 192 | Else 193 | Start = #False 194 | EndIf 195 | EndIf 196 | 197 | If KeyboardReleased(#PB_Key_Escape) 198 | Quit = #True 199 | EndIf 200 | 201 | EndIf 202 | 203 | Until Quit = #True 204 | End 205 | --------------------------------------------------------------------------------