| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | //		Collect the Balls Game
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | //		Purpose of the game is to figure out how to program.
 | 
					
						
							|  |  |  | //		The goal of the game, is to simply to collect balls until a score or time limit has been reached.
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | //		created by Rune; with massive help from sigonasr2 on Javidx9's One Lone Coder Discord server
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | #define OLC_PGE_APPLICATION
 | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include "pixelGameEngine.h"	// use this for drawing stuff to screen
 | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | using namespace olc; | 
					
						
							| 
									
										
										
										
											2022-08-21 22:14:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | //		class
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | class BallGame : public olc::PixelGameEngine | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | 	BallGame() | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | 		sAppName = "Rectangle Collision"; | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | 	struct Rectangle{ | 
					
						
							|  |  |  | 		vd2d pos; | 
					
						
							|  |  |  | 		vd2d size; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	vd2d pos = {0,0}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Rectangle r1 = {{64,64},{16,16}}; | 
					
						
							|  |  |  | 	Rectangle r3 = {{128,64},{16,16}}; | 
					
						
							|  |  |  | 	Rectangle r2 = {pos,{8,8}}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | //		Create stuff for game
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | 	bool OnUserCreate() override | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | //		main game function
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | 	bool OnUserUpdate(float fElapsedTime) override | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Clear(VERY_DARK_CYAN); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (GetKey(A).bHeld) { | 
					
						
							|  |  |  | 			r2.pos.x-=20*fElapsedTime; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (GetKey(D).bHeld) { | 
					
						
							|  |  |  | 			r2.pos.x+=20*fElapsedTime; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (GetKey(W).bHeld) { | 
					
						
							|  |  |  | 			r2.pos.y-=20*fElapsedTime; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (GetKey(S).bHeld) { | 
					
						
							|  |  |  | 			r2.pos.y+=20*fElapsedTime; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (Collision(r1,r2)) { | 
					
						
							|  |  |  | 			FillRect(r1.pos,r1.size,YELLOW); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			FillRect(r1.pos,r1.size,RED); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (Collision(r3,r2)) { | 
					
						
							|  |  |  | 			FillRect(r3.pos,r1.size,YELLOW); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			FillRect(r3.pos,r1.size,GREEN); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		FillRect(r2.pos,r2.size,BLUE); | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	bool Collision(Rectangle r1,Rectangle r2) { | 
					
						
							|  |  |  | 		return abs(r1.pos.x-r2.pos.x)<r1.size.x&&abs(r1.pos.y-r2.pos.y)<r1.size.y; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	bool Collision2(Rectangle r1,Rectangle r2) { | 
					
						
							|  |  |  | 		return abs(r1.pos.x-r2.pos.x)<r1.size.x&&abs(r1.pos.y-r2.pos.y)<r1.size.y; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | int main() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | 	BallGame game; | 
					
						
							|  |  |  | 	if (game.Construct(256, 240, 4, 4)) | 
					
						
							|  |  |  | 		game.Start(); | 
					
						
							| 
									
										
										
										
											2022-08-21 21:53:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2022-09-24 11:04:09 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | //		END OF FILE
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------------------------------------
 |