1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
|
#include "pong.h"
#include "raylib.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
Music Background = LoadMusicStream("resources/versus.wav");
Background.looping = true;
PlayMusicStream(Background);
// 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;
while(!WindowShouldClose() && GameGoing == true) {
UpdateMusicStream(Background);
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) {
StopSoundMulti();
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) {
StopSoundMulti();
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();
}
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);
BeginDrawing();
ClearBackground(BLACK);
BeginMode2D(MainCamera);
DrawTexture(PaddleSprite, 0, Player.Y, WHITE);
DrawTexture(PaddleSprite, 1200, Enemy.Y, WHITE);
DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE);
//DrawRectangleRec(Enemy.BallDetector, RED);
DrawText(PlayerScore, 0, 0, 32, GREEN);
DrawText(EnemyScore, 1130, 688, 32, RED);
EndMode2D();
EndDrawing();
}
return;
}
|