【发布时间】:2021-12-10 01:23:30
【问题描述】:
我目前卡住了,因为程序运行得非常好,但是当我尝试吃食物时它不起作用,因为食物处于奇怪的位置。我所说的奇怪位置的意思是,当我用蛇越过它时,它不会注册。 起初,我以为是因为我得到了一个小数,所以我试图将它放入一个 int 中,但显然,这不起作用。 贪吃蛇游戏,SDL,c++
#include <SDL.h>
#include <SDL_keycode.h>
#include <stdio.h>
#include <iostream>
using namespace std;
struct Food
{
int Height;
int Width;
int x_Pos;
int y_Pos;
void move()
{
int x_Pos = rand() % 400 + 40;//random pos
int y_Pos = rand() % 400 + 40;
}
void draw(SDL_Renderer* renderer)
{
SDL_Rect r{ x_Pos, y_Pos, 20, 20 };
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0);
SDL_RenderFillRect(renderer, &r);
}
};
struct v2
{
int x, y;
};
v2 operator+ (const v2& a, const v2& b)
{
return v2{ a.x + b.x, a.y + b.y };
}
void operator+= (v2& a, const v2& b)
{
a.x += b.x;
a.y += b.y;
}
struct Snake
{
int Height;
int Width;
v2 pos;
v2 vel;
int vX, vY;
uint32_t accumulator;
void update(uint32_t delta_time, Food& food)
{
accumulator += delta_time;
if (accumulator > 50)// update every 50 ms
{
accumulator = 0;
pos.x += vel.x;
pos.y += vel.y;
if (pos.x == food.x_Pos && pos.y == food.y_Pos)
{
food.move();
}
}
}
void draw(SDL_Renderer* renderer)
{
SDL_Rect r{ pos.x, pos.y, 20, 20 };
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 0);
SDL_RenderFillRect(renderer, &r);
}
};
bool isRunning = true;
//Screen dimension constants
const int SCREEN_WIDTH = 700;
const int SCREEN_HEIGHT = 700;
void setup_Window(int width, int height);
void input(SDL_Renderer* renderer);
void create_Rect(SDL_Renderer* &renderer, int x, int y, int w, int h, int colorRed, int colorGreen, int colorBlue, int colorA);
int main(int argc, char** argv)
{
srand(time(NULL));
int score = 0;
setup_Window(SCREEN_WIDTH, SCREEN_HEIGHT);
return 0;
}
void Calc()
{
}
void setup_Window(int width, int height)
{
if (SDL_Init(SDL_INIT_VIDEO) == 0)
{
std::cout << "Subsystems Initialised!..." << std::endl;
//The window we'll be rendering to
SDL_Window* window = SDL_CreateWindow("Snake Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
// Setup renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
input(renderer);
}
}
void create_Rect(SDL_Renderer* &renderer, int x, int y, int w, int h, int colorRed, int colorGreen, int colorBlue, int colorA)
{
// Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high.
SDL_Rect r;
r.x = x;//615
r.y = y;//0
r.w = w;//25
r.h = h;//480
// x = up and down
// y = left and right
// Set render color to blue ( rect will be rendered in this color )
SDL_SetRenderDrawColor(renderer, colorRed, colorGreen, colorBlue, colorA);
// Render rect
SDL_RenderFillRect(renderer, &r);
// Render the rect to the screen
SDL_RenderPresent(renderer);
}
void input(SDL_Renderer* renderer)
{
uint32_t current_Time = 0, previous_Time, delta_Time;
SDL_Event event;
int rando1 = rand() % 400 + 40;
int rando2 = rand() % 400 + 40;
int rando3 = rand() % 400 + 40;
int rando4 = rand() % 400 + 40;
Snake snake_Body = {};// stuct for Snake object
snake_Body.pos.x = rando1;//random pos
snake_Body.pos.y = rando2;
Food food = {}; // stuct for food object
food.move();
food.x_Pos = SCREEN_WIDTH/2;//random pos
food.y_Pos = SCREEN_HEIGHT/2;
while (isRunning)
{
previous_Time = current_Time;
current_Time = SDL_GetTicks();
delta_Time = current_Time - previous_Time;
// Set render color to black ( rect will be rendered in this color )
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
/* Clear the entire screen to our selected color. */
SDL_RenderClear(renderer);
create_Rect(renderer, 670, 0, 30, 700, 0, 255, 255, 255);// right rectangle
create_Rect(renderer, 1, 0, 30, 700, 0, 255, 255, 255);// left rectangle
create_Rect(renderer, 30, 1, 700, 30, 0, 255, 255, 255);// top rectangle
create_Rect(renderer, 1, 670, 700, 30, 0, 255, 255, 255);// Bottom rectangle
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
isRunning = false;
}
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_LEFT:
{
snake_Body.vel.y = 0;
snake_Body.vel.x = -15;
cout << "Crouiser1" << endl;
break;
}
case SDLK_UP:
{
snake_Body.vel.y = -15;
snake_Body.vel.x = 0;
cout << "Crouiser2" << endl;
break;
}
case SDLK_RIGHT:
{
snake_Body.vel.y = 0;
snake_Body.vel.x = 15;
cout << "Crouiser3" << endl;
break;
}
case SDLK_DOWN:
{
snake_Body.vel.y = 15;
snake_Body.vel.x = 0;
cout << "Crouiser4" << endl;
break;
}
case SDLK_ESCAPE:
{
isRunning = false;
break;
}
default:
{
break;
}
}
}
}
snake_Body.update(delta_Time, food);
snake_Body.draw(renderer);
food.draw(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(100);
}
}
【问题讨论】:
-
当您使用调试器运行此代码时,您看到了什么?这就是调试器的用途,如果您不知道如何使用它,这是一个学习在调试器中一次运行程序、监控所有变量及其值变化并分析您的程序的好机会程序的逻辑和执行。您应该可以使用您的调试器在这个和您编写的所有未来程序中发现所有简单的问题,全部由您自己完成。
-
@SamVarshavchik 我试图这样做,但我不知道这意味着什么。
-
这对于 Stackoverflow 来说听起来是一个很好的问题:源代码示例,调试器显示的内容,以及它的含义!
-
@SamVarshavchik 是的,我试图学习它,但我就是学不会。如果你不打算帮助我,那么我想我只能得到别人的帮助。 p.s.从我在调试器中看到的情况来看,这不是你一晚上就能学会的。
-
抱歉,Stackoverflow 不是帮助网站,也不是教程网站,我们只回答关于编程主题的具体技术问题。 Stackoverflow 的答案应该对每个人、整个社区都有价值,而不仅仅是对编写程序的人。一般"help me" is not an appropriate question, or request,这里的规则就是这些。