Du bist nicht angemeldet.

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

1

09.11.2007, 21:18

SDL Schuss Problem

Wollte grad ein tolles Spiel programmieren(^^) und habe folgenden Code geschrieben (schlechter stil, ich weiß^^)

C-/C++-Quelltext

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
#include <sdl.h>
#include <windows.h>

#ifdef main
#undef main
#endif

int main(int argc, char *argv[])
{
    SDL_Surface *Screen;
    SDL_Surface *Player;
    SDL_Surface *Stars;
    SDL_Surface *Shot;
    SDL_Rect ShotRect[255];
    SDL_Rect PlayerRect;
    SDL_Rect StarRect;
    bool exit = false;
    float LastTime, CurTime, Elapsed;
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    Screen = SDL_SetVideoMode(1024,768,32,SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
    Shot = SDL_LoadBMP("Schuss.bmp");
    SDL_SetColorKey(Shot, SDL_SRCCOLORKEY, SDL_MapRGB(Shot->format, 255,255,255));
    Player = SDL_LoadBMP("Raumschiff.bmp");
    PlayerRect.x = 150;
    PlayerRect.y = 150;
    PlayerRect.w = Player->w;
    PlayerRect.h = Player->h;
    SDL_SetColorKey(Player,SDL_SRCCOLORKEY, SDL_MapRGB(Player->format, 255,255,255));
    Stars = SDL_LoadBMP("Sterne.bmp");
    StarRect.x = 0;
    StarRect.y = 0;
    StarRect.w = Stars->w;
    StarRect.h = Stars->h;
    LastTime = SDL_GetTicks() / 1000.0f;
    int i=0;
    while(!(exit))
    {
        CurTime = SDL_GetTicks() / 1000.0f;
        Elapsed = CurTime - LastTime;
        LastTime = CurTime;
        SDL_FillRect(Screen, NULL, SDL_MapRGB(Screen->format,255,0,0));
        SDL_BlitSurface(Stars, NULL, Screen, &StarRect);
        SDL_BlitSurface(Player, NULL, Screen,&PlayerRect);
        for(int j=0; j==254;j++)
        {
                SDL_BlitSurface(Shot, NULL, Screen, &ShotRect[j]);
        }
        SDL_Flip(Screen);
        if(GetAsyncKeyState(VK_SPACE))
        {
            if(i==254)
            {
                i = 0;
            }
            else
            {
                i++;
            }
            ShotRect[i].x = PlayerRect.x + PlayerRect.w + 1;
            ShotRect[i].y = PlayerRect.y;
        }
        if(GetAsyncKeyState(VK_RIGHT) & 0x8000)
        {
            if(PlayerRect.x + PlayerRect.w < 1024)
            {
                PlayerRect.x += 500.0f * Elapsed;
            }
        }
        if(GetAsyncKeyState(VK_LEFT) & 0x8000)
        {
            if(PlayerRect.x > 0)
            {
                PlayerRect.x -= 500.0f * Elapsed;
            }
        }
        if(GetAsyncKeyState(VK_DOWN) & 0x8000)
        {
            if(PlayerRect.y + PlayerRect.h < 768)
            {
                PlayerRect.y += 500.0f * Elapsed;
            }
        }
        if(GetAsyncKeyState(VK_UP) & 0x8000)
        {
            if(PlayerRect.y > 0)
            {
                PlayerRect.y -= 500.0f * Elapsed;
            }
        }
        if(GetAsyncKeyState(VK_ESCAPE) & 0x8000)
        {
            exit = true;
        }
        for(int j=0; j== 254;j++)
        {
            ShotRect[j].x += 500.0f * Elapsed;
        }
    }
    SDL_Quit();
    return 0;
}


naja theoretisch sollte wenn ich jetzt die Leertaste drücke ein neuer Schuss gerendert werden... aber das wird er nicht.

kann mir jemand sagen wieso?

2

10.11.2007, 00:48

soo...

hab einen teil meines fehlers erkannt:

hatte höhe und breite nicht festgelegt.

jetzt hab ich folgendes:

C-/C++-Quelltext

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
#include <sdl.h> 
#include <windows.h> 

#ifdef main 
#undef main 
#endif 

int main(int argc, char *argv[]) 
{ 
    SDL_Surface *Screen; 
    SDL_Surface *Player; 
    SDL_Surface *Stars; 
    SDL_Surface *Shot; 
    SDL_Rect ShotRect[255]; 
    SDL_Rect PlayerRect; 
    SDL_Rect StarRect; 
    bool exit = false; 
    float LastTime, CurTime, Elapsed; 
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); 
    Screen = SDL_SetVideoMode(1024,768,32,SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN); 
    Shot = SDL_LoadBMP("Schuss.bmp"); 
    for(int a = 0; a==254; a++)
    {
        ShotRect[a].x = 0;
        ShotRect[a].y = 769;
        ShotRect[a].w = Shot->w;
        ShotRect[a].h = Shot->h;
    }
    SDL_SetColorKey(Shot, SDL_SRCCOLORKEY, SDL_MapRGB(Shot->format, 255,255,255)); 
    Player = SDL_LoadBMP("Raumschiff.bmp"); 
    PlayerRect.x = 150; 
    PlayerRect.y = 150; 
    PlayerRect.w = Player->w; 
    PlayerRect.h = Player->h; 
    SDL_SetColorKey(Player,SDL_SRCCOLORKEY, SDL_MapRGB(Player->format, 255,255,255)); 
    Stars = SDL_LoadBMP("Sterne.bmp"); 
    StarRect.x = 0; 
    StarRect.y = 0; 
    StarRect.w = Stars->w; 
    StarRect.h = Stars->h; 
    LastTime = SDL_GetTicks() / 1000.0f; 
    int i=0; 
    while(!(exit)) 
    { 
        CurTime = SDL_GetTicks() / 1000.0f; 
        Elapsed = CurTime - LastTime; 
        LastTime = CurTime; 
        SDL_FillRect(Screen, NULL, SDL_MapRGB(Screen->format,255,0,0)); 
        SDL_BlitSurface(Stars, NULL, Screen, &StarRect); 
        SDL_BlitSurface(Player, NULL, Screen,&PlayerRect); 
        for(int j=0; j==254;j++) 
        { 
                SDL_BlitSurface(Shot, NULL, Screen, &ShotRect[j]); 
        } 
        SDL_Flip(Screen); 
        if(GetAsyncKeyState(VK_SPACE)) 
        { 
            ShotRect[i].x = PlayerRect.x + PlayerRect.w + 1; 
            ShotRect[i].y = PlayerRect.y; 
            if(i==254) 
            { 
                i = 0; 
            } 
            else 
            { 
                ++i; 
            } 
        } 
        if(GetAsyncKeyState(VK_RIGHT) & 0x8000) 
        { 
            if(PlayerRect.x + PlayerRect.w < 1024) 
            { 
                PlayerRect.x += 500.0f * Elapsed; 
            } 
        } 
        if(GetAsyncKeyState(VK_LEFT) & 0x8000) 
        { 
            if(PlayerRect.x > 0) 
            { 
                PlayerRect.x -= 500.0f * Elapsed; 
            } 
        } 
        if(GetAsyncKeyState(VK_DOWN) & 0x8000) 
        { 
            if(PlayerRect.y + PlayerRect.h < 768) 
            { 
                PlayerRect.y += 500.0f * Elapsed; 
            } 
        } 
        if(GetAsyncKeyState(VK_UP) & 0x8000) 
        { 
            if(PlayerRect.y > 0) 
            { 
                PlayerRect.y -= 500.0f * Elapsed; 
            } 
        } 
        if(GetAsyncKeyState(VK_ESCAPE) & 0x8000) 
        {    
            exit = true; 
        } 
        for(int k=0; k== 254;k++) 
        { 
            ShotRect[k].x += 550.0f * Elapsed; 
        } 
    } 
    SDL_Quit(); 
    return 0; 
}


passiert aber immer noch nix bei druck auf leertaste

Das Gurke

Community-Fossil

Beiträge: 1 996

Wohnort: Pinneberg

Beruf: Schüler

  • Private Nachricht senden

3

10.11.2007, 01:22

Ohne den Code studiert zu haben:

C-/C++-Quelltext

1
2
3
4
for(int k=0; k== 254;k++)
        {
            ShotRect[k].x += 550.0f * Elapsed;
        } 
Das schaut für mich doch sehr merkwürdig aus ;)

4

10.11.2007, 01:34

is nur ne schleife um jeden Schuss um 550 mal der Zeit die seit dem letzten frame abgelaufen ist nach rechts zu bewegen^^

5

10.11.2007, 01:52

Deine for-Schleifen sind etwas komisch:

C-/C++-Quelltext

1
2
3
4
        for(int j=0; j==254;j++)
        {
                SDL_BlitSurface(Shot, NULL, Screen, &ShotRect[j]);
        } 

C-/C++-Quelltext

1
2
3
4
        for(int j=0; j== 254;j++)
        {
            ShotRect[j].x += 500.0f * Elapsed;
        } 


Die sollten eher so aussehen:

C-/C++-Quelltext

1
for(int j=0; j<=254; j++)

6

10.11.2007, 01:59

lol jetzt klappts.

aber wieso eigentlich? soll eine for schleife nicht wiederholen, bis etwas erreicht ist?

oder hab ich da was falsch verstanden?^^

und noch was: wie kann ich dafür sorgen, das nicht bei jedem schleifendurchlauf ich die taste drücke sondern wenn ich die taste drücke nur ein einzelner schuss abgefeuert wird?

7

10.11.2007, 02:02

For-Schleifen sollen wiederholen solange die Bedingung erfüllt ist.
Also genau das Gegenteil ;)

8

10.11.2007, 02:06

lol dann hab ich da seit ich programmiere fast immer glück gehabt, dass nix schief ging^^

Aso ja: hab mein schussprolem mit den 1schuss pro frame gelöst^^

T-VIRUS

Alter Hase

Beiträge: 548

Wohnort: Göttingen(West)/Nordhausen(Ost)

Beruf: Schüler

  • Private Nachricht senden

9

10.11.2007, 12:37

Hallo Mr. Data,
naja glück kann man das nicht wirklich nennen.
Du hast nur deine For-Schleifen so entwickelt, das die Bedingung immer eingetroffen ist.
Aber du hattest nie eine Absicherung.

In der Schule muss ich jetzt C und später C++ lernen.
Das Drama möchte ich euch noch im Oftopic Bereich vorstellen.

Wirtschaftslehrer sollten NIE Programmierung lehren!

MFG T-Virus
Meine Blog:)

Wer Bugs im Text findet kann sie melden, fix erscheint irgendwann :D

MFG T-VIRUS

10

10.11.2007, 12:42

hehe

ich hab auch nen Lehrer der uns Visual Basic beibrungen will und Formeln mit Excel schon programmieren nennt.

und letztens hat er case sensitive als Käse sensitive bezeichnet

Werbeanzeige