Skip to content

Commit

Permalink
Added feature rapiz1#26, fixed bug rapiz1#28
Browse files Browse the repository at this point in the history
  • Loading branch information
Haceau-Zoac committed Nov 15, 2020
1 parent 2d1d05f commit 447aa99
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 141 deletions.
29 changes: 15 additions & 14 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Compatible with more platforms
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>

#endif // _WIN32
#include "ai.h"
#include "audio.h"
#include "bullet.h"
Expand Down Expand Up @@ -36,6 +40,8 @@ extern Weapon weapons[WEAPONS_SIZE];
extern Sprite commonSprites[COMMON_SPRITE_SIZE];

extern unsigned int renderFrames;
extern Text messages[];

// Map
Block map[MAP_SIZE][MAP_SIZE];
Item itemMap[MAP_SIZE][MAP_SIZE];
Expand Down Expand Up @@ -458,7 +464,7 @@ void attackUpSprite(Sprite* sprite, int duration) {
ani->lifeSpan = duration;
}

void attackUpSnkae(Snake* snake, int duration) {
void attackUpSnake(Snake* snake, int duration) {
if (snake->buffs[BUFF_ATTACK]) return;
snake->buffs[BUFF_ATTACK] += duration;
for (LinkNode* p = snake->sprites->head; p; p = p->nxt) {
Expand Down Expand Up @@ -516,17 +522,15 @@ void destroyGame(int status) {
bullets = NULL;

blackout();
char* msg;
if (status == 0)
msg = "Stage Clear";
else
msg = "Game Over";
extern SDL_Color WHITE;
Text* text = createText(msg, WHITE);
Text* text = (status ? &messages[MSG_GAME_OVER] : &messages[MSG_STAGE_CLEAR]);
renderCenteredText(text, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 2);
destroyText(text);
SDL_RenderPresent(renderer);
// Compatible with more platforms
#ifndef _WIN32
sleep(RENDER_GAMEOVER_DURATION);
#else
Sleep(RENDER_GAMEOVER_DURATION SECOND);
#endif // _WIN32
clearRenderer();
}

Expand Down Expand Up @@ -981,12 +985,9 @@ void pauseGame() {
pauseSound();
playAudio(AUDIO_BUTTON1);
dim();
const char msg[] = "Paused";
extern SDL_Color WHITE;
Text* text = createText(msg, WHITE);
Text* text = &messages[MSG_PAUSED];
renderCenteredText(text, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 1);
SDL_RenderPresent(renderer);
destroyText(text);
SDL_Event e;
for (bool quit = 0; !quit;) {
while (SDL_PollEvent(&e)) {
Expand Down
6 changes: 4 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <assert.h>
#endif

int main(int argc, char** args) {
prngSrand(time(NULL));
int demain(int argc, char** args) {
prngSrand((unsigned int)time(NULL));
// Start up SDL and create window
if (!init()) {
printf("Failed to initialize!\n");
Expand All @@ -25,4 +25,6 @@ int main(int argc, char** args) {
}
}
cleanup();

return EXIT_SUCCESS;
}
21 changes: 10 additions & 11 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
TCPsocket lanServerSocket;
TCPsocket lanClientSocket;
SDLNet_SocketSet socketSet;
extern Text messages[MSG_COUNT];

static void CHECK() {
CASSERT(sizeof(LanPacket) == 4);
Expand Down Expand Up @@ -76,11 +77,10 @@ void hostGame() {

extern SDL_Color WHITE;
Text* listening[4] = {
createText("Listening", WHITE),
createText("Listening.", WHITE),
createText("Listening..", WHITE),
createText("Listening...", WHITE),
};
&messages[MSG_LAN_LISTENING0],
&messages[MSG_LAN_LISTENING1],
&messages[MSG_LAN_LISTENING2],
&messages[MSG_LAN_LISTENING3]};

SDL_Event e;
unsigned frameCount = 0;
Expand All @@ -102,7 +102,6 @@ void hostGame() {
// Clean up after connecting
SDLNet_TCP_Close(lanServerSocket);
lanServerSocket = NULL;
for (unsigned i = 0; i < 4; i++) destroyText(listening[i]);

blackout();

Expand Down Expand Up @@ -150,11 +149,11 @@ void joinGame(const char* hostname, Uint16 port) {

extern SDL_Color WHITE;
Text* connecting[4] = {
createText("Connecting", WHITE),
createText("Connecting.", WHITE),
createText("Connecting..", WHITE),
createText("Connecting...", WHITE),
};
&messages[MSG_LAN_CONNECTING0],
&messages[MSG_LAN_CONNECTING1],
&messages[MSG_LAN_CONNECTING2],
&messages[MSG_LAN_CONNECTING3]
};

bool quit = false;
SDL_Event e;
Expand Down
7 changes: 4 additions & 3 deletions src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern int texturesCount;
extern Texture textures[TEXTURES_SIZE];
extern int textsCount;
extern Text texts[TEXTSET_SIZE];
extern Text messages[MSG_COUNT];
Text* stageText;
Text* taskText;
Text* scoresText[MAX_PALYERS_NUM];
Expand Down Expand Up @@ -58,7 +59,7 @@ void initCountDownBar() {
void initInfo() {
extern int stage;
char buf[1 << 8];
sprintf(buf, "Stage:%3d", stage);
sprintf(buf, messages[MSG_INFO_STAGE_FORMAT].text, stage);
if (stageText)
setText(stageText, buf);
else
Expand Down Expand Up @@ -374,7 +375,7 @@ void renderInfo() {
for (int i = 0; i < playersCount; i++) {
char buf[1 << 8];
calcScore(spriteSnake[i]->score);
sprintf(buf, "Player%d:%5d", i + 1,
sprintf(buf, messages[MSG_INFO_PLAYER_FORMAT].text, i + 1,
(int)(spriteSnake[i]->score->rank + 0.5));
setText(scoresText[i], buf);
renderText(scoresText[i], startX, startY, 1);
Expand All @@ -383,7 +384,7 @@ void renderInfo() {
if (playersCount == 1) {
extern int GAME_WIN_NUM;
char buf[1 << 8];
sprintf(buf, "Find %d more heros!",
sprintf(buf, messages[MSG_INFO_FIND_HEROS].text,
GAME_WIN_NUM > spriteSnake[0]->num
? GAME_WIN_NUM - spriteSnake[0]->num
: 0);
Expand Down
Loading

0 comments on commit 447aa99

Please sign in to comment.