![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 22
Rep Power: 0
![]() |
still working on my game... (for-loop)
the problem I'm having now is trying to show the output of the "ray gun"
#include <conio.h>
#include <stdio.h>
#include <dos.h>
#include <iostream.h>
int main()
{
char firstchoice,a,dir,length;
int locationx,locationy,same,timer,locationxbullet,locationybullet;
timer=50;
_setcursortype(_NOCURSOR);
textcolor(YELLOW);
textbackground(BLUE);
locationx=40;
locationy=24;
locationxbullet=40;
locationybullet=23;
clrscr();
gotoxy(19,12);
cout << "Welcome to Steve's Awesome Computer Game!";
gotoxy(19,13);
cout << "Press H for help.";
gotoxy(19,14);
cout << "Otherwise, Press any other key to continue.";
firstchoice=getch();
if(firstchoice == 'h')
{
clrscr();
cout << "You're a rogue space pilot on a mission to destroy other ASCII characters.\nUse A to move left and D to move right.\nUse the spacebar to shoot.\nYou win when all the other ASCII characters are dead.\nGOOD LUCK!";
}
else
{
timer=timer-1;
clrscr();
for(a=1;a<=79;a++)
{
gotoxy(a,1);printf("X");
gotoxy(a,25);printf("X");
}
for(a=2;a<=24;a++)
{
gotoxy(1,a);printf("X");
gotoxy(79,a);printf("X");
}
// insert the code from other program here for the aliens
do
{
if(kbhit() !=0)
{
dir=getch();
if(dir=='a')
{
gotoxy(locationx,locationy);
printf(" ^_^ ");
locationx--;
locationxbullet--;
}
if(dir=='d')
{
gotoxy(locationx,locationy);
printf(" ^_^ ");
locationx++;
locationxbullet++;
}
if(dir=='m')
{
gotoxy(locationxbullet,locationybullet);
for(locationybullet=23;locationybullet>=2;locationybullet--)
printf(" I ");
}
}
}while(dir!='x');
}
}near the end, I have a for loop. what I'm trying to get the loop to do is have the "I" start right in front of the ship (the ^_^), and then shoot upwards. the problem is, with this for-loop, the I's just output in a strange horizontal pattern and don't go upwards in a straight line at all. what is the proper loop I should be using? any help is appreciated. edit: BTW, I'm working on a space invaders clone |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
You need to put the gotoxy call inside the for loop, so that you position the cursor just before you call printf.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 22
Rep Power: 0
![]() |
thanks! it worked. but...it goes directly to the top without any kind of trail. is there an easy fix for this?
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Show your modified loop?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Posts: 22
Rep Power: 0
![]() |
if(dir=='m')
{
for(locationybullet=23;locationybullet>=2;locationybullet--)
gotoxy(locationxbullet,locationybullet);
printf(" I ");
} |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
You need braces around the statements in the for loop - at the moment it is just running through the gotoxy's
|
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Mar 2006
Posts: 22
Rep Power: 0
![]() |
Quote:
can you give an example please? |
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Apr 2006
Posts: 2
Rep Power: 0
![]() |
if(dir=='m')
{
for(locationybullet=23;locationybullet>=2;locationybullet--)
{
gotoxy(locationxbullet,locationybullet);
printf(" I ");
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|