Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
#include <SDL.h> #include <SDL_image.h> #include <SDL_mixer.h> #include <SDL_ttf.h> #include <iostream> SDL_Surface* screen = NULL; int shoot_1( SDL_Surface* screen); int main(int argc, char* argv[]) { // SDL mit dem Grafik- und Audiosystem initialisieren. if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1) { // Ups, das hat nicht funktioniert! // Wir geben die Fehlermeldung aus. std::cerr << "Konnte SDL nicht initialisieren! Fehler: " << SDL_GetError() << std::endl; return -1; } // Hier erzeugen wir ein Fenster der Gr��e 800x600 Pixel mit 32-Bit Farbtiefe und Double Buffer. // Wenn Vollbildmodus erw�nscht ist, f�gen wir noch das Flag SDL_FULLSCREEN mittels |-Operator hinzu. //SDL_Surface* screen = SDL_SetVideoMode(800, 600, 32, SDL_DOUBLEBUF); screen = SDL_SetVideoMode(800,600,32,SDL_DOUBLEBUF); if(!screen) { // Fehler! Fehlermeldung ausgeben und Programm beenden. std::cerr << "Konnte SDL-Fenster nicht erzeugen! Fehler: " << SDL_GetError() << std::endl; return -1; } // Nun setzen wir noch den Titel des Fensters. SDL_WM_SetCaption("Game", "Game"); // Hier laden wir unser Bild mit SDL_image. SDL_Surface* image = IMG_Load("ship.jpg"); SDL_Surface* background_a = IMG_Load("background_a.jpg"); SDL_Surface* shoot_a =IMG_Load("shoot.jpg"); if(!image) { // Fehler! std::cerr << "Konnte das Bild nicht laden! Fehler: " << IMG_GetError() << std::endl; return -1; } // SDL_mixer starten. const int samplingFrequency = 44100; // 44100 Hz Abtastfrequenz Uint16 audioFormat = AUDIO_S16SYS; // 16 Bits pro Sample const int numChannels = 2; // 2 Kan�le = Stereo const int chunkSize = 4096; // ein guter Wert ... if(Mix_OpenAudio(samplingFrequency, audioFormat, numChannels, chunkSize) == -1) { std::cerr << "Konnte SDL_mixer nicht starten! Fehler: " << Mix_GetError() << std::endl; return -1; } // WAV-Datei laden. Mix_Chunk* sound = Mix_LoadWAV("SDL_Example_Sound.wav"); if(!sound) { std::cerr << "Konnte WAV-Datei nicht laden! Fehler: " << Mix_GetError() << std::endl; return -1; } // OGG-Datei als Musik laden. Mix_Music* music = Mix_LoadMUS("SDL_Example_Music.ogg"); if(!music) { std::cerr << "Konnte OGG-Datei nicht laden! Fehler: " << Mix_GetError() << std::endl; return -1; } // Musik abspielen. Mix_PlayMusic(music, -1); // SDL_ttf initialisieren. if(TTF_Init() == -1) { std::cerr << "Konnte SDL_ttf nicht initialisieren! Fehler: " << TTF_GetError() << std::endl; return -1; } // Schriftart "FreeSans.ttf" laden. TTF_Font* font = TTF_OpenFont("FreeSans.ttf", 12); if(!font) { std::cerr << "Konnte Schriftart nicht laden! Fehler: " << TTF_GetError() << std::endl; return -1; } // Einen Text zeichnen. SDL_Color textColor = {255, 255, 1}; SDL_Surface* text = TTF_RenderText_Blended(font, "written by Patrick Ratz", textColor); // In diesem Array merken wir uns, welche Tasten gerade gedr�ckt sind. bool keyPressed[SDLK_LAST]; memset(keyPressed, 0, sizeof(keyPressed)); // Position des Bilds initialisieren. SDL_Rect shoot_Position; shoot_Position.x = 400; shoot_Position.y = 300; SDL_Rect imagePosition; imagePosition.x = 400; imagePosition.y = 500; SDL_Rect background_aPosition; background_aPosition.x = 0; background_aPosition.y = 0; bool run = true; int shoot_event = 0; // die Event-Schleife while(run) { // Wir holen uns so lange neue Ereignisse, bis es keine mehr gibt. SDL_Event event; while(SDL_PollEvent(&event)) { // Was ist passiert? switch(event.type) { case SDL_QUIT: // Das Programm soll beendet werden. // Wir merken uns das und brechen beim n�chsten Mal die Schleife ab. run = false; break; case SDL_KEYDOWN: keyPressed[event.key.keysym.sym] = true; if(event.key.keysym.sym == SDLK_SPACE) { // Sound abspielen //shoot_1(screen); Mix_PlayChannel(-1, sound, 0); } break; case SDL_KEYUP: keyPressed[event.key.keysym.sym] = false; break; } } // Hier w�rden bei einem Spiel die eigentliche Spiellogik berechnet // und Grafiken angezeigt werden. // Bild je nach gedr�ckten Tasten bewegen. if(keyPressed[SDLK_LEFT] && imagePosition.x > 0) --imagePosition.x; // Bild nach links bewegen. if(keyPressed[SDLK_RIGHT] && imagePosition.x + image->w < screen->w) ++imagePosition.x; // Bild nach rechts bewegen. if(keyPressed[SDLK_UP] && imagePosition.y > 0) --imagePosition.y; // Bild nach oben bewegen. if(keyPressed[SDLK_DOWN] && imagePosition.y + image->h < screen->h) ++imagePosition.y; // Bild nach unten bewegen. if(keyPressed[SDLK_SPACE ]) shoot_event = 1; if (shoot_event == 1) { shoot_event = 0; shoot_1(screen); } if(keyPressed[SDLK_ESCAPE]) run = false; // Programm beenden. // Bildschirm mit Blau f�llen und dann das Bild zeichnen. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 255)); SDL_BlitSurface(background_a, 0, screen, &background_aPosition); SDL_BlitSurface(image, 0, screen, &imagePosition); // Den Text ausgeben. SDL_Rect textPosition; textPosition.x = 10; textPosition.y = 50; SDL_BlitSurface(text, 0, screen, &textPosition); // Dann aktualisieren wir den Bildschirm, damit wir auch etwas sehen. SDL_Flip(screen); } // Bild wieder freigeben SDL_FreeSurface(image); SDL_FreeSurface(background_a); // Sound und Musik aufr�umen und SDL_mixer stoppen Mix_FreeChunk(sound); Mix_FreeMusic(music); Mix_CloseAudio(); // Schriftart und gezeichneten Text freigeben und SDL_ttf herunterfahren SDL_FreeSurface(text); TTF_CloseFont(font); TTF_Quit(); // SDL herunterfahren SDL_Quit(); return 0; } int shoot_1(SDL_Surface *screen) { SDL_Surface* shoot = IMG_Load("laser.png"); SDL_Rect shoot_Position; shoot_Position.y = 200; while(shoot_Position.y > 150 ) { --shoot_Position.y; SDL_FillRect(screen,0,0); SDL_BlitSurface(shoot, 0, screen, &shoot_Position); SDL_Flip(screen); } SDL_FreeSurface(shoot); return 0; } |
Quellcode |
|
1 2 3 4 |
if(keyPressed[SDLK_SPACE ] && shoot_event == 0) shoot_event = 1; if(keyPressed[SDLK_ESCAPE]) run = false; // Programm beenden. // Bildschirm mit Blau f�llen und dann das Bild zeichnen. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 255)); SDL_BlitSurface(background_a, 0, screen, &background_aPosition); SDL_BlitSurface(image, 0, screen, &imagePosition); if (shoot_event == 1) { SDL_BlitSurface(shoot, 0, screen, &shoot_Position); shoot_Position.y -= 5; if(shoot_Position.y == 100) { shoot_event = 0; shoot_Position.y = imagePosition.y; shoot_Position.x = imagePosition.x; }}... |
Zitat
Zum Einfügen von Quellcode bitte in den "Quellcode"-Modus des Editors wechseln, da ansonsten die Zeilenumbrüche verloren gehen!
Werbeanzeige