Help with If

hi, im learning c++ alone beacause my teacher dont do nothing.

im creating a game like spacewar, but im getting problems with a IF, i would like when wep hits the enemy to make the program restart to int main.

im sorry for my English but im studying it alone too, my english teacher dont do nothing as well.


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
#include <conio2.h>
#include <stdio.h>
#include <time.h>
#include <dos.h>
#include <windows.h>
#include <ctype.h>
#include <iostream>
#include <math.h>

bool gameover=false;
int shipx=10,shipy=10,wepx=0,wepy=0,i=0,enemyx=0,enemyy=0,score=0;

void ship (int shipx,int shipy)
{
    textcolor (YELLOW);
    gotoxy (shipx,shipy);
    printf ("Y");
}

int shipm ()
{
    if ( GetAsyncKeyState( 'W' ) & 0x8000 )
    {
        shipy=(shipy-1);
    }
    if ( GetAsyncKeyState( 'S' ) & 0x8000 )
    {
        shipy=(shipy+1);
    }
    if ( GetAsyncKeyState( 'A' ) & 0x8000 )
    {
        shipx=(shipx-1);
    }
    if ( GetAsyncKeyState( 'D' ) & 0x8000 )
    {
        shipx=(shipx+1);
    }
}

void wep ()
{
        wepx=i;
        gotoxy (wepx,wepy);
        textcolor(WHITE);
        printf ("-");
        delay (50);
}

void enemy ()
{

    gotoxy (enemyx,enemyy);
    textcolor (RED);
    printf ("N");
}

void clearship ()
{
    gotoxy (shipx,shipy);
    printf (" ");
}

void clearwep()
{
    gotoxy (wepx,wepy);
    printf (" ");
}

void clearenemy()
{
    gotoxy (enemyx,enemyy);
    printf (" ");
}
int main ()
{

    enemyx = rand () % 10 + 20;
    enemyy = rand () % 20;
    enemy();

    do
    {
        shipm ();
        ship (shipx,shipy);
        delay(100);

        if(GetAsyncKeyState( 'M' ) & 0x8000 )
        {
            wepx=shipx+i;
            wepy=shipy;

            for (i=wepx;i<30;i++)
            {
                wep ();
                shipm ();
                ship (shipx,shipy);

                delay(100);
                clearship();
               //cleararma();
            }

            if (i<29)
                i=0;
        }

        if ((enemyx == wepx) && (enemyy == wepy))
        {
            clearenemy();
            enemy();
            score++;
        }

        gotoxy (1,1);
        textcolor (WHITE);
        printf ("Score: %d ",score);

        clearship();
    }
    while (gameover==false);
}

  
OK. Please consider focusing on learning English first. Not knowing the language in the country that you live in is a very isolating experience. Writing code is not going to make you less isolated, often it just contributes to the isolation or works as an escape mechanism.


About the Code;
(I'm on a Linux machine so I can't actually run the code itself, but this is what I see so far)

line 20: expects an int return value but you don't provide one. I'd make this a void returned function.

Breaking back into main:
Make a while loop that contains all changes made in main, when the do-while loop ends break out of it into the larger while loop. Your current do-while loop could be turned into a function called playGame().
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    while(true) // generally ask the user if they want to play again...
    {
        enemyx = rand () % 10 + 20;
        enemyy = rand () % 20;
        enemy();

        playGame();
    }
}


Enemy Destroyed:
line 110:
Create new enemy positions so it looks like a new enemy is created. Right now it looks like the enemy is just redrawn in the same position when it's destroyed... Simply copy and paste lines 77-78 directly above line 110.

Last edited on
OK. Please consider focusing on learning English first. Not knowing the language in the country that you live in is a very isolating experience. Writing code is not going to make you less isolated, often it just contributes to the isolation or works as an escape mechanism.


i live in Italy, i have friends and everything so im not isolated, in Italian's shools are really bad and its not easy to learn a leanguange alone.

ty, for the advise
Last edited on
Topic archived. No new replies allowed.