# Help with Galaxian.

Printable View

• 01-29-2005
anthonymcf
Help with Galaxian.
I am currently programming a version of Galaxian as part of a HNC course I am doing, the problem is with getting my arrays working (the first 10x3 aliens are working fine, it's just the next two alien lines). Here is the code:
globals.cpp
ENEMY Enemies[10][3]; (this one is working)
ENEMY2 Enemies2[10][1]; (this is the one that is not)

globals.h has the same two lines

Game_loop
void Initialise()
{
int xpos,ypos,xsub,ysub;

Draw_Ship();

for (xpos=100, xsub=0; xpos<=640; xpos+=60, xsub++)
{
for (ypos=280, ysub=0; ypos<=400; ypos+=60, ysub++)
{
Enemies[xsub][ysub].x=xpos;
Enemies[xsub][ysub].y=ypos;
Enemies[xsub][ysub].alive=1;
Draw_Enemies(xpos,ypos);
}
}

starttime=GetTickCount();

for (xpos=100, xsub=0; xpos<=640; xpos+=60, xsub++)
{
for (ypos=460, ysub=0; ypos<=520; ypos+=60, ysub++)
{
Enemies[xsub][ysub].x=xpos;
Enemies[xsub][ysub].y=ypos;
Enemies[xsub][ysub].alive=1;
Draw_Enemies2(xpos,ypos);
}
}

Any help will GREATLY appreciated. Thanks.
• 01-29-2005
Sang-drax
Please enclose your code with [CODE] and [/CODE].
• 01-29-2005
Salem
Well you don't use Enemies2 in your 2nd set of loops.

Even if you did, for (ypos=460, ysub=0; ypos<=520; ypos+=60, ysub++) loops twice, so on the 2nd iteration, you would be accessing out of bounds.

It might be a good idea to check your other cases as well, to make sure they're not off-by-1 as well.
• 01-29-2005
anthonymcf
Green=Game Loop Red=Globals.cpp Blue=Globals.h
Code:

```#include "Globals.h" void Main_Loop() {                 if (KEYDOWN(VK_ESCAPE))         {                            SendMessage(GameHandle, WM_CLOSE, 0,0);                        }         weaponendtime=(GetTickCount()-weaponstarttime);         if ((int)weaponendtime>weaponspeed)         {                 if (KEYDOWN(VK_RIGHT))                 {                         if (ShipX<757)                         {                                 Clear_Ship();                                 ShipX+=5;                                 Draw_Ship();                                 //Sleep(1);                         }                 }                 if (KEYDOWN(VK_LEFT))                 {                         if (ShipX>3)                         {                                 Clear_Ship();                                 ShipX-=5;                                 Draw_Ship();                                 //Sleep(1);                         }                 }                 weaponstarttime=GetTickCount();         }         endtime=(GetTickCount()-starttime);         if ((int)endtime>movespeed)         {                 Move_Enemies();                 starttime=GetTickCount();         } } void Initialise() {         int xpos,ypos,xsub,ysub;         Draw_Ship();         for (xpos=100, xsub=0; xpos<=640; xpos+=60, xsub++)         {                 for (ypos=280, ysub=0; ypos<=400; ypos+=60, ysub++)                 {                         Enemies[xsub][ysub].x=xpos;                         Enemies[xsub][ysub].y=ypos;                         Enemies[xsub][ysub].alive=1;                         Draw_Enemies(xpos,ypos);                 }         }         starttime=GetTickCount();         for (xpos=100, xsub=0; xpos<=640; xpos+=60, xsub++)         {                 for (ypos=460, ysub=0; ypos<=520; ypos+=60, ysub++)                 {                         Enemies2[xsub][ysub].x=xpos;                         Enemies2[xsub][ysub].y=ypos;                         Enemies2[xsub][ysub].alive=1;                         Draw_Enemies2(xpos,ypos);                 }         }         /*for (xpos=260; xpos<=530; xpos+=30)         {                 for (ypos=116; ypos<=148; ypos+=30)                 {                         Draw_Enemies3(xpos,ypos);                 }         }*/ } void Draw_Ship() {         HDC hdc;         HBRUSH hbrush;         hdc = GetDC(GameHandle);         RECT rect; // used to hold rect info         hbrush = CreateSolidBrush(RGB(255,0,0));         rect.left  = ShipX+13;         rect.top    = ShipY+3;         rect.right  = ShipX+26;         rect.bottom = ShipY+26;         FillRect(hdc,&rect,hbrush);         DeleteObject(hbrush);         hbrush = CreateSolidBrush(RGB(0,0,255));         rect.left  = ShipX+3;         rect.top    = ShipY+10;         rect.right  = ShipX+13;         rect.bottom = ShipY+16;         FillRect(hdc,&rect,hbrush);         DeleteObject(hbrush);         hbrush = CreateSolidBrush(RGB(0,0,255));         rect.left  = ShipX+26;         rect.top    = ShipY+10;         rect.right  = ShipX+36;         rect.bottom = ShipY+16;         FillRect(hdc,&rect,hbrush);         DeleteObject(hbrush);         hbrush = CreateSolidBrush(RGB(0,255,0));         rect.left  = ShipX-1;         rect.top    = ShipY+5;         rect.right  = ShipX+3;         rect.bottom = ShipY+22;         FillRect(hdc,&rect,hbrush);         DeleteObject(hbrush);         hbrush = CreateSolidBrush(RGB(0,255,0));         rect.left  = ShipX+36;         rect.top    = ShipY+5;         rect.right  = ShipX+40;         rect.bottom = ShipY+22;         FillRect(hdc,&rect,hbrush);         DeleteObject(hbrush);         hbrush = CreateSolidBrush(RGB(255,255,0));         rect.left  = ShipX+16;         rect.top    = ShipY;         rect.right  = ShipX+23;         rect.bottom = ShipY+3;         FillRect(hdc,&rect,hbrush);         DeleteObject(hbrush);         ReleaseDC(GameHandle,hdc); } void Clear_Ship() {         RECT BlankRect; // used to write a blank         HDC hdc;         HBRUSH hbrush;         hdc = GetDC(GameHandle);         hbrush = CreateSolidBrush(RGB(0,0,0));         BlankRect.left                = ShipX-5;         BlankRect.top                = ShipY;         BlankRect.right                = ShipX+40;         BlankRect.bottom        = ShipY+26;         FillRect(hdc,&BlankRect,hbrush); } void Draw_Enemies(int x, int y) {         HDC hdc;         HBRUSH hbrush;         hdc = GetDC(GameHandle);         RECT rect; // used to hold rect info         hbrush = CreateSolidBrush(RGB(0,100,255));         rect.left  = x+5;         rect.top    = y+3;         rect.right  = x+25;         rect.bottom = y+20;         FillRect(hdc,&rect,hbrush);         rect.left  = x+7;         rect.top    = y+20;         rect.right  = x+23;         rect.bottom = y+25;         FillRect(hdc,&rect,hbrush);         rect.left  = x;         rect.top    = y+5;         rect.right  = x+5;         rect.bottom = y+7;         FillRect(hdc,&rect,hbrush);         rect.left  = x+25;         rect.top    = y+5;         rect.right  = x+30;         rect.bottom = y+7;         FillRect(hdc,&rect,hbrush);         rect.left  = x;         rect.top    = y;         rect.right  = x+2;         rect.bottom = y+5;         FillRect(hdc,&rect,hbrush);         rect.left  = x+28;         rect.top    = y;         rect.right  = x+30;         rect.bottom = y+5;         FillRect(hdc,&rect,hbrush);         rect.left  = x+12;         rect.top    = y+25;         rect.right  = x+18;         rect.bottom = y+30;         FillRect(hdc,&rect,hbrush);                 DeleteObject(hbrush);         hbrush = CreateSolidBrush(RGB(255,255,255));         rect.left  = x+8;         rect.top    = y+7;         rect.right  = x+12;         rect.bottom = y+10;         FillRect(hdc,&rect,hbrush);         rect.left  = x+18;         rect.top    = y+7;         rect.right  = x+22;         rect.bottom = y+10;         FillRect(hdc,&rect,hbrush);         DeleteObject(hbrush);         ReleaseDC(GameHandle,hdc);         } void Draw_Enemies2(int x, int y) {         HDC hdc;         HBRUSH hbrush;         hdc = GetDC(GameHandle);         RECT rect; // used to hold rect info         hbrush = CreateSolidBrush(RGB(255,0,50));         rect.left  = x+5;         rect.top    = y+3;         rect.right  = x+25;         rect.bottom = y+20;         FillRect(hdc,&rect,hbrush);         rect.left  = x+7;         rect.top    = y+20;         rect.right  = x+23;         rect.bottom = y+25;         FillRect(hdc,&rect,hbrush);         rect.left  = x;         rect.top    = y+5;         rect.right  = x+5;         rect.bottom = y+7;         FillRect(hdc,&rect,hbrush);         rect.left  = x+25;         rect.top    = y+5;         rect.right  = x+30;         rect.bottom = y+7;         FillRect(hdc,&rect,hbrush);         rect.left  = x;         rect.top    = y;         rect.right  = x+2;         rect.bottom = y+5;         FillRect(hdc,&rect,hbrush);         rect.left  = x+28;         rect.top    = y;         rect.right  = x+30;         rect.bottom = y+5;         FillRect(hdc,&rect,hbrush);         rect.left  = x+12;         rect.top    = y+25;         rect.right  = x+18;         rect.bottom = y+30;         FillRect(hdc,&rect,hbrush);                 DeleteObject(hbrush);         hbrush = CreateSolidBrush(RGB(255,255,255));         rect.left  = x+8;         rect.top    = y+7;         rect.right  = x+12;         rect.bottom = y+10;         FillRect(hdc,&rect,hbrush);         rect.left  = x+18;         rect.top    = y+7;         rect.right  = x+22;         rect.bottom = y+10;         FillRect(hdc,&rect,hbrush);         DeleteObject(hbrush);         ReleaseDC(GameHandle,hdc); } /*void Draw_Enemies3(int x, int y) {         HDC hdc;         HBRUSH hbrush;                 hdc = GetDC(GameHandle);         hbrush = CreateSolidBrush (RGB(0,200,0));         SelectObject(hdc,hbrush);         int num_points=11;         POINT point_list[11];         point_list[0].x=x;         point_list[0].y=y+2;         point_list[1].x=x+5;         point_list[1].y=y+2;         point_list[2].x=x+5;         point_list[2].y=y;         point_list[3].x=x+7;         point_list[3].y=y;         point_list[4].x=x+7;         point_list[4].y=y+2;         point_list[5].x=x+13;         point_list[5].y=y+2;         point_list[6].x=x+13;         point_list[6].y=y;         point_list[7].x=x+15;         point_list[7].y=y;         point_list[8].x=x+15;         point_list[8].y=y+2;         point_list[9].x=x+20;         point_list[9].y=y+2;         point_list[10].x=x+10;         point_list[10].y=y+22;         Polygon(hdc,point_list,num_points); //Main body of the enemies         DeleteObject(hbrush); }*/ void Clear_Enemies(int x, int y) {         RECT BlankRect; //used to write a blank         HDC hdc;         HBRUSH hbrush;         hdc = GetDC(GameHandle);         hbrush = CreateSolidBrush(RGB(0,0,0));         BlankRect.left                = x;         BlankRect.top                = y;         BlankRect.right                = x+30;         BlankRect.bottom        = y+30;         FillRect(hdc,&BlankRect,hbrush);         DeleteObject(hbrush);         ReleaseDC(GameHandle,hdc); } void Move_Enemies() {         HDC hdc;         hdc = GetDC(GameHandle);         if (direction=='L')         {                 trackx-=pixelmove;         }         else         {                 trackx+=pixelmove;         }         for (int enemyx=0; enemyx<10; enemyx++)         {                 for (int enemyy=0; enemyy<5; enemyy++)                 {                         if (Enemies[enemyx][enemyy].alive==1)                         {                                 Clear_Enemies(Enemies[enemyx][enemyy].x,                                         Enemies[enemyx][enemyy].y);                                 if (direction=='L')                                 {                                         Enemies[enemyx][enemyy].x-=pixelmove;                                 }                                 else                                 {                                         Enemies[enemyx][enemyy].x+=pixelmove;                                 }                                 Draw_Enemies(Enemies[enemyx][enemyy].x, Enemies[enemyx][enemyy].y);                         }                 }         }         if (direction=='L')         {                 for (int pos=100; pos<600; pos++)                 {                         if (GetPixel(hdc,5,pos)==RGB(255,0,0))                         {                                 direction='R';                         }                 }         }                         if (direction=='R')         {                 for (int pos=100; pos<600; pos++)                 {                         if (GetPixel(hdc,790,pos)==RGB(255,0,0))                         {                                 direction='L';                         }                 }         }         ReleaseDC(GameHandle,hdc); } #include "Globals.h" HWND GameHandle; int ShipX=380, ShipY=570; int weaponspeed=1; int enemyx=50, enemyy=250; int movespeed=300, trackx=50, tracky=250, pixelmove=6; char direction='L'; ENEMY Enemies[10][3]; ENEMY2 Enemies2[10][1]; DWORD weaponstarttime, weaponendtime; DWORD starttime, endtime; #pragma once #define WIN32_LEAN_AND_MEAN  // Compile only necessary elements #include <windows.h>  // include all the windows headers #include <stdlib.h> #include <stdio.h> // defines for windows #define WINDOW_CLASS_NAME "WINCLASS1" #define TITLE "Space_Engagement" #define WINDOW_WIDTH  808 // Need these settings to give 800x600 ex border #define WINDOW_HEIGHT 634 // Macros #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEYUP(vk_code)  ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) // Structures struct ENEMY {         int x;         int y;         int alive; // 1=alive, 0=dead; } ; extern ENEMY Enemies[10][3]; extern ENEMY2 Enemies2[10][1]; //Globals extern int ShipX, ShipY; extern HWND GameHandle; extern DWORD weaponstarttime, weaponendtime; extern int weaponspeed; extern int enemyx, enemyy; extern DWORD endtime, starttime; // Function Prototypes //////////////////////////////// void Main_Loop(); void Draw_Ship(); void Initialise(); void Clear_Ship(); void Draw_Enemies(int x, int y); void Draw_Enemies2(int x, int y); /*void Draw_Enemies3(int x, int y);*/ void Clear_Enemies(int x, int y); extern int movespeed, trackx, tracky, pixelmove; extern char direction; void Move_Enemies(); ```
Can anyone see where I am going wrong?
• 02-02-2005
anthonymcf
no odds now, the deadline has come and gone. I just went for a 10x5 layout but I had other problems too. I could not get the attacks right (where they leave and re-enter the screen unless blown away or if they have collided with the ship) I also had problems with getting the enemies to fire and getting a score to calculate and print out on screen. Any Ideas as it could be useful for future refernce.