SDL2'Publet'运动不起作用?(SDL2 'Bullet' movement not working?)

编程入门 行业动态 更新时间:2024-10-23 11:25:01
SDL2'Publet'运动不起作用?(SDL2 'Bullet' movement not working?)

我正在尝试用SDL2制作一个简单的滚动射击游戏。 我在屏幕上有一个移动的玩家,我试图让玩家使用阵列射击子弹(所以他们可以射击多个子弹)然而,当我按空格键时,没有任何反应,而是子弹图像的类型在左上角闪烁。

在代码板中有相同的代码: http : //codepad.org/rOhE1AqY

#include <SDL.h> #include <stdio.h> //use for things like printf, same as cout #include <iostream> #include <string> #include <time.h> using namespace std; //screend dimensions& sprtie dimensions const int SCREEN_HEIGHT = 600; const int SCREEN_WIDTH = 400; const int SPRITE_WIDTH = 60; const int SPRITE_HEIGHT = 80; const int MAX_BULLETS = 50; SDL_Window* Window = NULL;//the window rendering to SDL_Surface* ScreenSurface = NULL;//surface contained by window SDL_Surface* Background = NULL; SDL_Surface* Player = NULL; SDL_Surface* Enemy = NULL; SDL_Surface* Bullet = NULL; SDL_Surface* newBullet = NULL; SDL_Rect posPlayer, posEnemy, posBullet, posnewBullet; const Uint8* keystate = SDL_GetKeyboardState(NULL); SDL_Event event; class thePlayer { public: thePlayer(); void player_movement(); void show_player(); private: }; class theBullet { public: theBullet(); bool isActive; int x_position; int y_position; void bullet_movement(); void add_new_bullet(); void show_bullet(); private: }; theBullet arrayofBullets[MAX_BULLETS]; class theEnemy { public: theEnemy(); void enemy_movement(); void show_enemy(); private: }; thePlayer::thePlayer() { posPlayer.x = 170; posPlayer.y = SCREEN_HEIGHT; posPlayer.w = 20; posPlayer.h = 30; } void thePlayer::player_movement() { if(keystate[SDL_SCANCODE_LEFT]) { posPlayer.x -= 2; } if(keystate[SDL_SCANCODE_RIGHT]) { posPlayer.x += 2; } if(keystate[SDL_SCANCODE_UP]) { posPlayer.y -= 2; } if(keystate[SDL_SCANCODE_DOWN]) { posPlayer.y += 2; } if ((posPlayer.x + SPRITE_WIDTH) > SCREEN_WIDTH) { posPlayer.x = (SCREEN_WIDTH - SPRITE_WIDTH); } if ((posPlayer.y + SPRITE_HEIGHT) > SCREEN_HEIGHT) { posPlayer.y = (SCREEN_HEIGHT - SPRITE_HEIGHT); } } void thePlayer::show_player() { SDL_BlitSurface(Player, NULL, ScreenSurface, &posPlayer); SDL_SetColorKey(Player, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255)); } theBullet::theBullet() { /*posBullet.x; posBullet.y; posBullet.w = 10; posBullet.h = 15;*/ } void theBullet::bullet_movement() { /*if(keystate[SDL_SCANCODE_SPACE]) { posBullet.x = posPlayer.x + 25; posBullet.y = posPlayer.y + 10; } posBullet.y -= 2; if(posBullet.y < 0) { posBullet.y = -50; }*/ } void theBullet::show_bullet() { //SDL_BlitSurface(Bullet, NULL, ScreenSurface, &posBullet); //SDL_SetColorKey(Bullet, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));//removes white background } theEnemy::theEnemy() { srand (time(NULL)); posEnemy.x = rand() % 300 + 50; posEnemy.y =0; posEnemy.w = 35; posEnemy.h = 60; } void theEnemy::enemy_movement() { posEnemy.y += 1; if(posEnemy.y > SCREEN_HEIGHT) { posEnemy.y = SCREEN_HEIGHT +50; } } void theEnemy::show_enemy() { SDL_BlitSurface(Enemy, NULL, ScreenSurface, &posEnemy); SDL_SetColorKey(Enemy, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255)); } bool initialise() { bool success = true; if (SDL_Init(SDL_INIT_EVERYTHING) !=0) { cout<<"SDL_Init Error."<<SDL_GetError()<<endl; success = false; } else { //create the window for game Window = SDL_CreateWindow("Scrolling Shooter Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (Window == NULL) { cout<<"Window Error"<<SDL_GetError()<<endl; success = false; } else { //get window surface ScreenSurface = SDL_GetWindowSurface(Window); } } return success; } bool LoadingMedia() { bool success = true; Background = SDL_LoadBMP("background.bmp"); if (Background == NULL) { cout<<"Error in loading background."<<SDL_GetError()<<endl; success = false; } Player = SDL_LoadBMP("spaceship.bmp"); if (Player == NULL) { cout<<"Error in loading player."<<SDL_GetError()<<endl; success = false; } Enemy = SDL_LoadBMP("enemy.bmp"); if (Enemy == NULL) { cout<<"Error in loading enemy."<<SDL_GetError()<<endl; success = false; } Bullet = SDL_LoadBMP("bullet.bmp"); if (Bullet == NULL) { cout<<"Error in loading bullet."<<SDL_GetError()<<endl; success = false; } return success; } void closedown() { SDL_FreeSurface(Background); Background = NULL; SDL_FreeSurface(Player); Player = NULL; SDL_FreeSurface(Enemy); Enemy = NULL; SDL_DestroyWindow(Window); Window = NULL; SDL_Quit(); } int main(int argc, char** argv) { bool quit = false; thePlayer myPlayer; theEnemy myEnemy; theBullet myBullet; if (!initialise()) { cout<<"Failed to initialise"<<SDL_GetError()<<endl; } else { if (!LoadingMedia()) { cout<<"Error loading media"<<SDL_GetError()<<endl; } } //makes all bullets false for (int i=0; i<MAX_BULLETS; i++) { arrayofBullets[i].isActive = false; } //GAME LOOP while (quit == false) { SDL_BlitSurface(Background, NULL, ScreenSurface, NULL); myPlayer.show_player(); myPlayer.player_movement(); while (SDL_PollEvent(&event)) { if( event.type == SDL_QUIT ) { quit = true; break; } if(keystate[SDL_SCANCODE_SPACE]) { for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == false) { arrayofBullets[i].x_position = posPlayer.x + 25; arrayofBullets[i].y_position = posPlayer.y + 10; arrayofBullets[i].isActive = true; break; } } } //update game objects for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == true) { arrayofBullets[i].y_position -= 2; if (arrayofBullets[i].y_position < 0) { arrayofBullets[i].isActive = false; } } } for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == true) { SDL_BlitSurface(Bullet, NULL, ScreenSurface, NULL); } } } //myPlayer.show_player(); //myBullet.show_bullet(); //myEnemy.show_enemy(); //myPlayer.player_movement(); //myBullet.bullet_movement(); //myEnemy.enemy_movement(); SDL_UpdateWindowSurface(Window); //updates screen } closedown(); return 0; }

I am attempting to make a simple scrolling shooter game with SDL2. I have a moving player on a screen, and I am trying to make the player shoot a bullet using an array (so they can shoot multiple bullets) however, when I press the space bar, nothing happens, and instead the bullet image sort of flashes in the top left corner.

Heres the same code in codepad: http://codepad.org/rOhE1AqY

#include <SDL.h> #include <stdio.h> //use for things like printf, same as cout #include <iostream> #include <string> #include <time.h> using namespace std; //screend dimensions& sprtie dimensions const int SCREEN_HEIGHT = 600; const int SCREEN_WIDTH = 400; const int SPRITE_WIDTH = 60; const int SPRITE_HEIGHT = 80; const int MAX_BULLETS = 50; SDL_Window* Window = NULL;//the window rendering to SDL_Surface* ScreenSurface = NULL;//surface contained by window SDL_Surface* Background = NULL; SDL_Surface* Player = NULL; SDL_Surface* Enemy = NULL; SDL_Surface* Bullet = NULL; SDL_Surface* newBullet = NULL; SDL_Rect posPlayer, posEnemy, posBullet, posnewBullet; const Uint8* keystate = SDL_GetKeyboardState(NULL); SDL_Event event; class thePlayer { public: thePlayer(); void player_movement(); void show_player(); private: }; class theBullet { public: theBullet(); bool isActive; int x_position; int y_position; void bullet_movement(); void add_new_bullet(); void show_bullet(); private: }; theBullet arrayofBullets[MAX_BULLETS]; class theEnemy { public: theEnemy(); void enemy_movement(); void show_enemy(); private: }; thePlayer::thePlayer() { posPlayer.x = 170; posPlayer.y = SCREEN_HEIGHT; posPlayer.w = 20; posPlayer.h = 30; } void thePlayer::player_movement() { if(keystate[SDL_SCANCODE_LEFT]) { posPlayer.x -= 2; } if(keystate[SDL_SCANCODE_RIGHT]) { posPlayer.x += 2; } if(keystate[SDL_SCANCODE_UP]) { posPlayer.y -= 2; } if(keystate[SDL_SCANCODE_DOWN]) { posPlayer.y += 2; } if ((posPlayer.x + SPRITE_WIDTH) > SCREEN_WIDTH) { posPlayer.x = (SCREEN_WIDTH - SPRITE_WIDTH); } if ((posPlayer.y + SPRITE_HEIGHT) > SCREEN_HEIGHT) { posPlayer.y = (SCREEN_HEIGHT - SPRITE_HEIGHT); } } void thePlayer::show_player() { SDL_BlitSurface(Player, NULL, ScreenSurface, &posPlayer); SDL_SetColorKey(Player, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255)); } theBullet::theBullet() { /*posBullet.x; posBullet.y; posBullet.w = 10; posBullet.h = 15;*/ } void theBullet::bullet_movement() { /*if(keystate[SDL_SCANCODE_SPACE]) { posBullet.x = posPlayer.x + 25; posBullet.y = posPlayer.y + 10; } posBullet.y -= 2; if(posBullet.y < 0) { posBullet.y = -50; }*/ } void theBullet::show_bullet() { //SDL_BlitSurface(Bullet, NULL, ScreenSurface, &posBullet); //SDL_SetColorKey(Bullet, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));//removes white background } theEnemy::theEnemy() { srand (time(NULL)); posEnemy.x = rand() % 300 + 50; posEnemy.y =0; posEnemy.w = 35; posEnemy.h = 60; } void theEnemy::enemy_movement() { posEnemy.y += 1; if(posEnemy.y > SCREEN_HEIGHT) { posEnemy.y = SCREEN_HEIGHT +50; } } void theEnemy::show_enemy() { SDL_BlitSurface(Enemy, NULL, ScreenSurface, &posEnemy); SDL_SetColorKey(Enemy, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255)); } bool initialise() { bool success = true; if (SDL_Init(SDL_INIT_EVERYTHING) !=0) { cout<<"SDL_Init Error."<<SDL_GetError()<<endl; success = false; } else { //create the window for game Window = SDL_CreateWindow("Scrolling Shooter Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (Window == NULL) { cout<<"Window Error"<<SDL_GetError()<<endl; success = false; } else { //get window surface ScreenSurface = SDL_GetWindowSurface(Window); } } return success; } bool LoadingMedia() { bool success = true; Background = SDL_LoadBMP("background.bmp"); if (Background == NULL) { cout<<"Error in loading background."<<SDL_GetError()<<endl; success = false; } Player = SDL_LoadBMP("spaceship.bmp"); if (Player == NULL) { cout<<"Error in loading player."<<SDL_GetError()<<endl; success = false; } Enemy = SDL_LoadBMP("enemy.bmp"); if (Enemy == NULL) { cout<<"Error in loading enemy."<<SDL_GetError()<<endl; success = false; } Bullet = SDL_LoadBMP("bullet.bmp"); if (Bullet == NULL) { cout<<"Error in loading bullet."<<SDL_GetError()<<endl; success = false; } return success; } void closedown() { SDL_FreeSurface(Background); Background = NULL; SDL_FreeSurface(Player); Player = NULL; SDL_FreeSurface(Enemy); Enemy = NULL; SDL_DestroyWindow(Window); Window = NULL; SDL_Quit(); } int main(int argc, char** argv) { bool quit = false; thePlayer myPlayer; theEnemy myEnemy; theBullet myBullet; if (!initialise()) { cout<<"Failed to initialise"<<SDL_GetError()<<endl; } else { if (!LoadingMedia()) { cout<<"Error loading media"<<SDL_GetError()<<endl; } } //makes all bullets false for (int i=0; i<MAX_BULLETS; i++) { arrayofBullets[i].isActive = false; } //GAME LOOP while (quit == false) { SDL_BlitSurface(Background, NULL, ScreenSurface, NULL); myPlayer.show_player(); myPlayer.player_movement(); while (SDL_PollEvent(&event)) { if( event.type == SDL_QUIT ) { quit = true; break; } if(keystate[SDL_SCANCODE_SPACE]) { for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == false) { arrayofBullets[i].x_position = posPlayer.x + 25; arrayofBullets[i].y_position = posPlayer.y + 10; arrayofBullets[i].isActive = true; break; } } } //update game objects for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == true) { arrayofBullets[i].y_position -= 2; if (arrayofBullets[i].y_position < 0) { arrayofBullets[i].isActive = false; } } } for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == true) { SDL_BlitSurface(Bullet, NULL, ScreenSurface, NULL); } } } //myPlayer.show_player(); //myBullet.show_bullet(); //myEnemy.show_enemy(); //myPlayer.player_movement(); //myBullet.bullet_movement(); //myEnemy.enemy_movement(); SDL_UpdateWindowSurface(Window); //updates screen } closedown(); return 0; }

最满意答案

SDL_BlitSurface(Bullet, NULL, ScreenSurface, NULL);

您尚未指定目标rect,因此它将在左上角显示blit。

它应该是

SDL_Rect dstrect; dstrect.x = arrayofBullets[i].x_position; dstrect.y = arrayofBullets[i].y_position; dstrect.w = Bullet->w; dstrect.h = Bullet->h; SDL_BlitSurface(Bullet, NULL, ScreenSurface, &dstrect); SDL_BlitSurface(Bullet, NULL, ScreenSurface, NULL);

You haven't specified destination rect, so it will blit on left-top corner.

It should be

SDL_Rect dstrect; dstrect.x = arrayofBullets[i].x_position; dstrect.y = arrayofBullets[i].y_position; dstrect.w = Bullet->w; dstrect.h = Bullet->h; SDL_BlitSurface(Bullet, NULL, ScreenSurface, &dstrect);

更多推荐

本文发布于:2023-07-16 12:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   Publet   working   movement   Bullet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!