我正在使用惊人的raylib在C中制作一个简单的游戏,我尝试打印我的玩家坐标,但应该更新的x值保持在0
,而y值应该保持在同样打印一个根据我的玩家的x值改变的垃圾值
我的代码如下: (here是评论版,抱歉如果帖子中嵌入的代码太多)
// WIP
#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//#define PLATFORM_WEB
#define DEBUG
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
#define PLAYER_MAX_LIFE 5
typedef enum GameScreen { LOGO, TITLE, GAMEPLAY, ENDING } GameScreen;
typedef struct Player {
Vector2 position;
Vector2 size;
int life;
} Player;
typedef struct Food {
Vector2 position;
int value;
} Food;
static int screenWidth = 800;
static int screenHeight = 450;
static int framesCounter;
static bool gameOver;
static bool pause;
#if defined(DEBUG)
static char* debugText;
#endif
static Player player;
static void InitGame(void); // Initialize game
static void UpdateGame(void); // Update game (one frame)
static void DrawGame(void); // Draw game (one frame)
static void UnloadGame(void); // Unload game
static void UpdateDrawFrame(void); // Update and Draw (one frame)
int main()
{
InitWindow(screenWidth, screenHeight, "what should i name this piece of s##t game");
InitGame();
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
UpdateGame();
DrawGame();
}
#endif
UnloadGame(); // Unload loaded data (textures, sounds, models etc)
CloseWindow(); // Close window and OpenGL context
return 0;
}
void InitGame(void)
{
player.position = (Vector2){ screenWidth/2, screenHeight - screenHeight/8 };
player.size = (Vector2){ screenWidth/10, 20 };
player.life = PLAYER_MAX_LIFE;
debugText = malloc(sizeof(char) * 20);
}
void UpdateGame(void)
{
if (!gameOver)
{
if (IsKeyPressed('P')) pause = !pause;
if (!pause)
{
if (IsKeyDown(KEY_LEFT)) player.position.x -= 5;
if ((player.position.x - player.size.x/2) <= 0) player.position.x = player.size.x/2;
if (IsKeyDown(KEY_RIGHT)) player.position.x += 5;
if ((player.position.x + player.size.x/2) >= screenWidth) player.position.x = screenWidth - player.size.x/2;
#if defined(DEBUG)
debugText = FormatText("screen: %dx%d\nx: %d\ny: %d", screenWidth, screenHeight, player.position.x, player.position.y);
#endif
if (player.life <= 0) gameOver = true;
}
}
else
{
if (IsKeyPressed(KEY_ENTER))
{
InitGame();
gameOver = false;
}
}
}
void DrawGame(void)
{
BeginDrawing();
ClearBackground(RAYWHITE);
if (!gameOver)
{
#if defined(DEBUG)
DrawText(debugText, 50, 70, 20, BLACK);
#endif
DrawRectangle(0, screenHeight - screenHeight/8 + (screenHeight - screenHeight/8)/36, screenWidth, screenHeight/9, GREEN);
DrawRectangle(player.position.x - player.size.x/2, player.position.y - player.size.y/2, player.size.x, player.size.y, BLACK);
for (int i = 0; i < player.life; i++) DrawRectangle(20 + 40*i, screenHeight - 30, 35, 10, LIGHTGRAY);
if (pause) DrawText("GAME PAUSED", screenWidth / 2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
}
else DrawText("PRESS [ENTER] TO PLAY AGAIN", screenWidth/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, screenHeight/2 - 50, 20, GRAY);
EndDrawing();
}
void UnloadGame(void)
{
free(debugText);
}
void UpdateDrawFrame(void)
{
UpdateGame();
DrawGame();
}
static void UpdateFood(void)
{
//TODO: Implement logic
}
我认为有问题的部分是:
debugText = FormatText("screen: %dx%d\nx: %d\ny: %d", screenWidth, screenHeight, player.position.x, player.position.y);
访问player.position.x和player.position.y不起作用或者可能存在某种溢出?我真的不知道,所以任何帮助都会受到赞赏。
答案 0 :(得分:0)
原来其中一个问题是溢出而另一个问题是使用了错误的格式说明符 改变这两行修正了它:
-- debugText = malloc(sizeof(char) * 20);
++ debugText = malloc(sizeof(char) * 40);
-- debugText = FormatText("screen: %dx%d\nx: %d\ny: %d", screenWidth, screenHeight, player.position.x, player.position.y);
++ debugText = FormatText("screen: %dx%d\nx: %f\ny: %f", screenWidth, screenHeight, player.position.x, player.position.y);