-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameScene.c
90 lines (87 loc) · 2.25 KB
/
GameScene.c
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
#include "GameScene.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include "Ball.h"
#include "Paddle.h"
#include "Renderer.h"
void eventHandler(SDL_Event event, Paddle* player1, Paddle* player2, int* running, int* paused){
while(SDL_PollEvent(&event))
switch(event.type){
case SDL_QUIT:
*running = 1;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.scancode){
case SDL_SCANCODE_W:
player1->controls[0]=1;
break;
case SDL_SCANCODE_UP:
player2->controls[0]=1;
break;
case SDL_SCANCODE_S:
player1->controls[1]=1;
break;
case SDL_SCANCODE_DOWN:
player2->controls[1]=1;
break;
case SDL_SCANCODE_ESCAPE:
*paused *= -1;
break;
default:
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.scancode){
case SDL_SCANCODE_W:
player1->controls[0]=0;
break;
case SDL_SCANCODE_UP:
player2->controls[0]=0;
break;
case SDL_SCANCODE_S:
player1->controls[1]=0;
break;
case SDL_SCANCODE_DOWN:
player2->controls[1]=0;
break;
default:
break;
}
}
}
void one_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player, Paddle* cpu, char* score_str, int* paused){
//Exit and Input Handling
eventHandler(event, player, cpu, running, paused);
if(*paused == -1){
//Player Movement Logic
playerLogic(player);
//Ball Movement Logic
ballLogic(ball, player, cpu, score_str);
//CPU Logic
cpuLogic(cpu, ball);
//Win Conditions
if(player->score >= 5 || cpu->score >= 5){
sprintf(score_str, "%d %d", player->score, cpu->score);
player->score = 0;
cpu->score = 0;
}
}
}
void two_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player1, Paddle* player2, char* score_str, int* paused){
//Exit and Input Handling
eventHandler(event, player1, player2, running, paused);
if(*paused == -1){
//Player Movement Logic
playerLogic(player1);
playerLogic(player2);
//Ball Movement Logic
ballLogic(ball, player1, player2, score_str);
//Win Conditions
if(player1->score >= 5 || player2->score >= 5){
sprintf(score_str, "%d %d", player1->score, player2->score);
player1->score = 0;
player2->score = 0;
}
}
}