我在学习 raylib 并试图制作最简单的平台游戏时面临问题,这是我用 C 语言编写的代码,而不是 C++ 再次重复 C 不是 C++ 请快速解决此错误,不要使用聊天,这是不对的请用简单的解决方案解释并简单地解释一下。
#include "raylib.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
typedef struct{
int x;
int y;
float size;
int velocity;
}Ball;
typedef struct{
int x;
int y;
int width;
int height;
}Platform;
int ball_on_ground(Ball ball, Platform platform[]){
int count = sizeof(platform)/sizeof(Platform);
for(int i=0; i < count; i++){
Platform platform = platform[i];
Rectangle platform_rec = {.x = platform.x, platform.y, .width = platform.width, .height = platform.height};
Vector2 ball_position = {.x = ball.x, .y = ball.y};
int ballbottom = ball.y + ball.size;
if (CheckCollisionCircleRec(ball_position, ball.size, platform_rec)){
return 1;
}
}
return 0;
}
int main(){
int width = 800;
int height = 600;
Ball ball = {
.x = width/2,
.y = height/2,
.size = 40.0,
.velocity = 4
};
int gravity = 2;
Platform platforms[3];
platforms[0].x = 0;
platforms[0].y = height-10;
platforms[0].width = width;
platforms[0].height = 10;
platforms[1].x = abs(width*0.1);
platforms[1].y = abs(height*0.7);
platforms[1].width = abs(width*0.3);
platforms[1].height = abs(height *0.05);
platforms[1].x = abs(width*0.1);
platforms[1].y = abs(height*0.7);
platforms[1].width = abs(width*0.3);
platforms[1].height = abs(height *0.05);
InitWindow( width, height, "MyGame" );
SetTargetFPS( 60 );
while(!WindowShouldClose()){
BeginDrawing();
int ballbottom = ball.y + ball.size;
if(ball_on_ground(ball, platforms)){
ball.velocity = 0;
//ball.y = height - ball.size;
if(IsKeyPressed(KEY_SPACE)){
ball.velocity = -30;
}
}
if(IsKeyDown(KEY_LEFT)){
ball.x -= 6;
}
if(IsKeyDown(KEY_RIGHT)){
ball.x += 6;
}
ball.y += ball.velocity;
ball.velocity += gravity;
ClearBackground(WHITE);
int count = sizeof(platforms)/sizeof(Platform);
for(int i=0; i < count; i++){
DrawRectangle(platforms[i].x,platforms[i].y,platforms[i].width,platforms[i].height,BLACK);}
DrawCircle(ball.x, ball.y , ball.size, BLUE);
EndDrawing();
}
return 0;
}
这是我在运行代码时遇到的错误:
BallGame.c:22:37: error: subscripted value is neither array nor pointer nor vector
22 | Platform platform = platform[i];
它能够成功运行并打造坚实的平台。请不要过多更改代码。
我刚刚做了这个:
int ball_on_ground(Ball ball, Platform platforms[]){
int count = sizeof(platforms)/sizeof(Platform);
for(int i=0; i < count; i++){
Platform platform = platforms[i];
Rectangle platform_rec = {.x = platform.x, platform.y, .width = platform.width, .height = platform.height};
Vector2 ball_position = {.x = ball.x, .y = ball.y};
int ballbottom = ball.y + ball.size;
if (CheckCollisionCircleRec(ball_position, ball.size, platform_rec)){
return 1;
}
}
return 0;
}