about summary refs log tree commit diff stats
path: root/ex3.mu
Commit message (Collapse)AuthorAgeFilesLines
* 7846Kartik K. Agaram2021-03-041-1/+1
|
* 7842 - new directory organizationKartik K. Agaram2021-03-031-0/+31
Baremetal is now the default build target and therefore has its sources at the top-level. Baremetal programs build using the phase-2 Mu toolchain that requires a Linux kernel. This phase-2 codebase which used to be at the top-level is now under the linux/ directory. Finally, the phase-2 toolchain, while self-hosting, has a way to bootstrap from a C implementation, which is now stored in linux/bootstrap. The bootstrap C implementation uses some literate programming tools that are now in linux/bootstrap/tools. So the whole thing has gotten inverted. Each directory should build one artifact and include the main sources (along with standard library). Tools used for building it are relegated to sub-directories, even though those tools are often useful in their own right, and have had lots of interesting programs written using them. A couple of things have gotten dropped in this process: - I had old ways to run on just a Linux kernel, or with a Soso kernel. No more. - I had some old tooling for running a single test at the cursor. I haven't used that lately. Maybe I'll bring it back one day. The reorg isn't done yet. Still to do: - redo documentation everywhere. All the README files, all other markdown, particularly vocabulary.md. - clean up how-to-run comments at the start of programs everywhere - rethink what to do with the html/ directory. Do we even want to keep supporting it? In spite of these shortcomings, all the scripts at the top-level, linux/ and linux/bootstrap are working. The names of the scripts also feel reasonable. This is a good milestone to take stock at.
ef='#n102'>102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
#include "pong.h"

void versus_main() {
    // Init Player Variables
	struct Players Player;
	Player.Y = 0;
	Player.Score = 0;

	// Init Enemy Variables
	struct Players Enemy;
	Enemy.Y = 0;
	Enemy.Score = 0;
	Enemy.Direction = 0;

    // Init Ball Variables
	struct Balls Ball;
	Ball.X = 1280/2.0f;
	Ball.Y = 720/2.0f;
	Ball.Direction = LEFT;
	Ball.Speed = 3.0f;
	Ball.Angle = 0.0f;

	// Init music
	Mix_Music *Background = Mix_LoadMUS("resources/versus.wav");
	Mix_PlayMusic(Background, -1);
	Mix_VolumeMusic(GlobalSettings.MusicVolume);
    
    // Set Sprites
	Texture2D PaddleSprite = LoadTexture("resources/paddle.png");
	Texture2D BallSprite = LoadTexture("resources/ball.png");

	// Set Collision Boxes
	Player.HitBox = (Rectangle){80, Player.Y, 5, PaddleSprite.height};
	Enemy.HitBox = (Rectangle){1200, Enemy.Y, 5, PaddleSprite.height};
	Ball.HitBox = (Rectangle){Ball.X, Ball.Y, BallSprite.width, BallSprite.height};
	Enemy.BallDetector = (Rectangle){0, Enemy.Y+120, 1280, PaddleSprite.height/5.0f};
    char EnemyScore[50];
    char PlayerScore[50]; // Used later to display score on screen.
    // Init Camera
    Camera2D MainCamera;
        MainCamera.target = (Vector2){0, 0};
        MainCamera.offset = (Vector2){0, 0};
        MainCamera.rotation = 0;
	bool VersusGoing = true;
	Ball.NextTick = SDL_AtomicGet(&Ticks);
	Enemy.NextTick = SDL_AtomicGet(&Ticks);
    while(VersusGoing == true && GameGoing == true) {
		
		if (WindowShouldClose()) { //Quit Game if the window is closed.
            GameGoing = false;
        }

		MainCamera.zoom = GetScreenHeight()/720.0f;
		MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f};
		MainCamera.target = (Vector2){1280/2.0f, 720/2.0f};

		// Who won?
		if (Enemy.Score >= 5) {
			Mix_HaltMusic();
			Mix_FreeMusic(Background);
			play_audio(STOP_ALL_SOUNDS);
			play_audio(MUSIC_DEFEAT);
			while(!IsKeyDown(KEY_SPACE)) {
				BeginDrawing();
					EnableCursor();
					ClearBackground(BLACK);
					DrawText("You Lose.", 0, 0, 128, RED);
					DrawText("Press space to return to title screen.", 0, 128, 24, WHITE);
				EndDrawing();
			}
			return;
		} else if (Player.Score >= 5) {
			Mix_HaltMusic();
			Mix_FreeMusic(Background);
			play_audio(STOP_ALL_SOUNDS);
			play_audio(MUSIC_VICTORY);
			while(!IsKeyDown(KEY_SPACE)) {
				BeginDrawing();
					EnableCursor();
					ClearBackground(BLACK);
					DrawText("You Win!", 0, 0, 128, BLUE);
					DrawText("Press space to return to title screen.", 0, 128, 24, WHITE);
				EndDrawing();
			}
			return;
		}

		//Controls
		if(IsKeyDown(KEY_UP)) {
			Player.Y -= 10;
		} else if (IsKeyDown(KEY_DOWN)) {
			Player.Y += 10;
		} else if(IsKeyPressed(KEY_ESCAPE)) {
			EnableCursor();
		} else if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsCursorHidden() == true) {
			Player.Y = GetMouseY()-PaddleSprite.height/2.0f;
			DisableCursor();
		} 
		
		// Leave Game
		if(IsKeyPressed(KEY_Q)) {
			VersusGoing = false;
		}
		
		if(GetMouseY() < 0) {
			SetMousePosition(0, 0);
		} else if(GetMouseY() > 720) {
			SetMousePosition(0, 720);
		}

		//Check if players are off-screen
		if (Player.Y < 0) {
			Player.Y = 0;
		} else if (Player.Y > 480) {
			Player.Y = 480;
		}

		enemy(&Enemy, Ball);
		
		// Collision
		ball(&Player.HitBox, &Enemy.HitBox, &Ball, &Player.Score, &Enemy.Score);

		//Updates hitbox with player's position.
		Player.HitBox.y = Player.Y;

		//Turn Scores into strings.
		snprintf(PlayerScore, 50, "Player: %d", Player.Score);
		snprintf(EnemyScore, 50, "Enemy: %d", Enemy.Score);
		//fprintf(stdout, "%d\n", GetFPS());
		BeginDrawing();
			ClearBackground(BLACK);
			BeginMode2D(MainCamera);
				DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255});
				DrawTexture(PaddleSprite, 0, Player.Y, WHITE);
				DrawTexture(PaddleSprite, 1200, Enemy.Y, WHITE);
				DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE);
				DrawText(PlayerScore, 0, 0, 32, BLUE);
				DrawText(EnemyScore, 1130, 688, 32, RED);
			EndMode2D();
		EndDrawing();
	}

	Mix_HaltMusic();
	Mix_FreeMusic(Background);
	play_audio(STOP_ALL_SOUNDS);
    return;
}