gpt4 book ai didi

c++ - SDL side-scroller 滚动不一致

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:02:17 31 4
gpt4 key购买 nike

所以我正在对我以前的项目(我在此处发布用于代码审查)进行升级,这次实现了一个重复的背景(就像卡通中使用的那样),这样 SDL 就不必加载非常大的图像对于一个级别。然而,程序中存在一个奇怪的不一致:用户第一次一直滚动到右侧时,显示的面板比指定的少 2 个。向后(向左)显示正确数量的面板(即面板重复代码中指定的次数)。在那之后,似乎再次向右走(一次一直在左边)显示正确数量的面板并且同样向后走。这是一些选定的代码,这是一个 .zip of all my code

构造函数:

Game::Game(SDL_Event* event, SDL_Surface* scr, int level_w, int w, int h, int bpp) {
this->event = event;
this->bpp = bpp;
level_width = level_w;
screen = scr;
w_width = w;
w_height = h;

//load images and set rects
background = format_surface("background.jpg");
person = format_surface("person.png");

background_rect_left = background->clip_rect;
background_rect_right = background->clip_rect;
current_background_piece = 1; //we are displaying the first clip
rect_in_view = &background_rect_right;
other_rect = &background_rect_left;

person_rect = person->clip_rect;

background_rect_left.x = 0; background_rect_left.y = 0;
background_rect_right.x = background->w; background_rect_right.y = 0;
person_rect.y = background_rect_left.h - person_rect.h;
person_rect.x = 0;
}

这里是 move 方法,它可能会造成所有的麻烦:

void Game::move(SDLKey direction) {
if(direction == SDLK_RIGHT) {
if(move_screen(direction)) {
if(!background_reached_right()) {
//move background right

background_rect_left.x += movement_increment;
background_rect_right.x += movement_increment;

if(rect_in_view->x >= 0) {
//move the other rect in to fill the empty space
SDL_Rect* temp;

other_rect->x = -w_width + rect_in_view->x;

temp = rect_in_view;
rect_in_view = other_rect;
other_rect = temp;

current_background_piece++;
std::cout << current_background_piece << std::endl;
}

if(background_overshoots_right()) {
//sees if this next blit is past the surface
//this is used only for re-aligning the rects when
//the end of the screen is reached

background_rect_left.x = 0;
background_rect_right.x = w_width;
}
}
}
else {
//move the person instead

person_rect.x += movement_increment;
if(get_person_right_side() > w_width) {
//person went too far right

person_rect.x = w_width - person_rect.w;
}
}
}

else if(direction == SDLK_LEFT) {
if(move_screen(direction)) {
if(!background_reached_left()) {
//moves background left

background_rect_left.x -= movement_increment;
background_rect_right.x -= movement_increment;

if(rect_in_view->x <= -w_width) {
//swap the rect in view
SDL_Rect* temp;

rect_in_view->x = w_width;

temp = rect_in_view;
rect_in_view = other_rect;
other_rect = temp;

current_background_piece--;
std::cout << current_background_piece << std::endl;
}

if(background_overshoots_left()) {
background_rect_left.x = 0;
background_rect_right.x = w_width;
}
}
}
else {
//move the person instead

person_rect.x -= movement_increment;
if(person_rect.x < 0) {
//person went too far left

person_rect.x = 0;
}
}
}
}

如果没有其余代码,这就没有多大意义。太多了就上传吧here供测试用。无论如何,有人知道我该如何解决这种不一致问题吗?

最佳答案

打印出代表您认为可能可疑的数据的值,并确保它们正在改变您认为应该的方式。这听起来很典型 fencepost error .

关于c++ - SDL side-scroller 滚动不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2862395/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com