Archived

This topic is now archived and is closed to further replies.

SSJCORY

Why don't my bullets fire?

Recommended Posts

I recently decide to make a game i thought pong but changed my mind to a game like galaga. It draws the rect for a ship and is supposed to fire a bullet when you press up, but when i do press it it does nothing. Why?
#define WIN32_LEAN_AND_MEAN
#include<windows.h>
HINSTANCE gInstance;
HWND ghwnd;
void initWindow();
void killWindow();
void initBullet();
void initPaddle();
void moveBullet(bool shot);
void movePaddle();
void drawBullet();
void drawBulletWithPaddle();
void drawPaddle();
void MessageHandler();
const int height = 480;
const int width = 640;
const int paddlewidth = 40;
const int paddleheight = 10;
const int ballradius = 5;
int pb = 440 , pt = 430, pl = 300, pr = 340;
int bt,bb,bl,br;
int topy;
int topl;
bool shoot = false;
bool gameover = false;
MSG msg;
HDC hdc;
RECT rect;
RECT bullet;
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
	switch(msg){
	case WM_DESTROY:
		gameover = true;
		break;
	case WM_KEYDOWN:
		switch(wParam){
		case 37:
			if(pl-10 >= 1){
			FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
			pl -= 10;
			pr -= 10;
			}
			else{
				pl = 4;
				pr = 44;
			}
			break;
		case 39:
			if(pr+10 <= 639){
			FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
			pl += 10;
			pr += 10;
			}
			else{
				pl = 596;
				pr = 636;
			}
			break;
		}
		case 38:
			shoot = true;
			break;
		break;
	}
	return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR CmdLine,int iCmd){
	gInstance = hInstance;
	hdc = GetDC(ghwnd);
	initWindow();
	initBullet();
	initPaddle();
	while(!gameover){
		MessageHandler();
		moveBullet(shoot);
		movePaddle();
		drawPaddle();
		drawBullet();
	}
	killWindow();
	return msg.wParam;
}
void initWindow(){
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = gInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "Class";
	wc.lpszMenuName = NULL;
	wc.style = NULL;
	RegisterClass(&wc);
	ghwnd = CreateWindow("Class","Pong",WS_OVERLAPPEDWINDOW,0,0,width,height,NULL,NULL,gInstance,NULL);
	ShowWindow(ghwnd,SW_SHOW);
	UpdateWindow(ghwnd);
}
void killWindow(){
}
void initBullet(){
}
void initPaddle(){
}
void drawBullet(){
	FillRect(hdc,&bullet,(HBRUSH)GetStockObject(BLACK_BRUSH));
}
void drawBulletWithPaddle(){
	bt = pt;
    bb = pb;
	bl = pl;
	br = pr;
	bullet.bottom = bb;
	bullet.top = bt;
	bullet.left = bl;
	bullet.right = br;
	FillRect(hdc,&bullet,(HBRUSH)GetStockObject(BLACK_BRUSH));
}

void drawPaddle(){	
	rect.bottom = pb;
	rect.top = pt;
	rect.left = pl;
	rect.right = pr;
	FillRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));
}
void moveBullet(bool shot){
	if(shoot == true){
		bt -= 1;
		bb -= 1;
		if(bt >= 0){
			shot = false;
		}
	}
}
void movePaddle(){
}
void MessageHandler(){
	MSG msg;
	if(PeekMessage(&msg,ghwnd,0,0,PM_REMOVE)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}
Thanks a lot guys, Cory
Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
UPDATE , but still doesn''t fire:

#define WIN32_LEAN_AND_MEAN
#include<windows.h>
HINSTANCE gInstance;
HWND ghwnd;
void initWindow();
void killWindow();
void initBullet();
void initPaddle();
void moveBullet();
void movePaddle();
void drawBullet();
void drawBulletWithPaddle();
void drawPaddle();
void MessageHandler();
int bxmove = 0,bymove = 0;
const int height = 480;
const int width = 640;
const int paddlewidth = 40;
const int paddleheight = 10;
const int ballradius = 5;
int pb = 440 , pt = 430, pl = 300, pr = 340;
int bt,bb,bl,br;
int topy;
int topl;
bool shoot = false;
bool gameover = false;
MSG msg;
HDC hdc;
RECT rect;
RECT bullet;
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
case WM_DESTROY:
gameover = true;
break;
case WM_KEYDOWN:
switch(wParam){
case 37:
if(pl-10 >= 1){
FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
pl -= 10;
pr -= 10;
bl -= 10;
br -= 10;
}
else{
pl = 4;
pr = 44;
bl = 4;
br = 44;
}
break;
case 39:
if(pr+10 <= 639){
FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
pl += 10;
pr += 10;
bl += 10;
br += 10;
}
else{
pl = 596;
pr = 636;
bl = 596;
br = 636;
}
break;
}
case 38:
bymove = 10;
break;
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR CmdLine,int iCmd){
gInstance = hInstance;
hdc = GetDC(ghwnd);
initWindow();
initBullet();
initPaddle();
while(!gameover){
MessageHandler();
moveBullet();
movePaddle();
drawPaddle();
drawBullet();
}
killWindow();
return msg.wParam;
}
void initWindow(){
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = gInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "Class";
wc.lpszMenuName = NULL;
wc.style = NULL;
RegisterClass(&wc);
ghwnd = CreateWindow("Class","Pong",WS_OVERLAPPEDWINDOW,0,0,width,height,NULL,NULL,gInstance,NULL);
ShowWindow(ghwnd,SW_SHOW);
UpdateWindow(ghwnd);
}
void killWindow(){
hdc = NULL;
}
void initBullet(){
bullet.bottom = pb;
bullet.left = pl;
bullet.right = pr;
bullet.top = pt;
}
void initPaddle(){
}
void drawBullet(){
FillRect(hdc,&bullet,(HBRUSH)GetStockObject(WHITE_BRUSH));
bullet.top = bt;
bullet.bottom = bb;
bullet.left = bl;
bullet.right = br;
FillRect(hdc,&bullet,(HBRUSH)GetStockObject(BLACK_BRUSH));
}
void drawPaddle(){
rect.bottom = pb;
rect.top = pt;
rect.left = pl;
rect.right = pr;
FillRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));
}
void moveBullet(){
bt -= bymove;
bb -= bymove;
bymove = 0;
}
void movePaddle(){
}
void MessageHandler(){
MSG msg;
if(PeekMessage(&msg,ghwnd,0,0,PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}




Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
Put a breakpoint in the function, and press up, and trace through the code, and see which path it takes.

Hint: you have a closing brace in the wrong place. But I''ll let you use the debugger, as specified above, to see what the problem is.

Share this post


Link to post
Share on other sites
What''s a breakpoint?




Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
Ok i inserted a breakpoint at the drawBullets() and ran debug but then the window goes out of focus and doesn''t come up. And i have to ctrl-alt-delete it.



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
I found the improperly placed closing brace, but it still doesn''t work:

#define WIN32_LEAN_AND_MEAN
#include<windows.h>
HINSTANCE gInstance;
HWND ghwnd;
void initWindow();
void killWindow();
void initBullet();
void initPaddle();
void moveBullet();
void movePaddle();
void drawBullet();
void drawBulletWithPaddle();
void drawPaddle();
void MessageHandler();
int bxmove = 0,bymove = 0;
const int height = 480;
const int width = 640;
const int paddlewidth = 40;
const int paddleheight = 10;
const int ballradius = 5;
int pb = 440 , pt = 430, pl = 300, pr = 340;
int bt,bb,bl,br;
int topy;
int topl;
bool shoot = false;
bool gameover = false;
MSG msg;
HDC hdc;
RECT rect;
RECT bullet;
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
case WM_DESTROY:
gameover = true;
break;
case WM_KEYDOWN:
switch(wParam){
case 37:
if(pl-10 >= 1){
FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
pl -= 10;
pr -= 10;
bl -= 10;
br -= 10;
}
else{
pl = 4;
pr = 44;
bl = 4;
br = 44;
}
break;
case 39:
if(pr+10 <= 639){
FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
pl += 10;
pr += 10;
bl += 10;
br += 10;
}
else{
pl = 596;
pr = 636;
bl = 596;
br = 636;
}
break;
case 38:
bymove = 10;
break;
}
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR CmdLine,int iCmd){
gInstance = hInstance;
hdc = GetDC(ghwnd);
initWindow();
initBullet();
initPaddle();
while(!gameover){
MessageHandler();
moveBullet();
movePaddle();
drawPaddle();
drawBullet();
}
killWindow();
return msg.wParam;
}
void initWindow(){
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = gInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "Class";
wc.lpszMenuName = NULL;
wc.style = NULL;
RegisterClass(&wc);
ghwnd = CreateWindow("Class","Pong",WS_OVERLAPPEDWINDOW,0,0,width,height,NULL,NULL,gInstance,NULL);
ShowWindow(ghwnd,SW_SHOW);
UpdateWindow(ghwnd);
}
void killWindow(){
hdc = NULL;
}
void initBullet(){
bullet.bottom = pb;
bullet.left = pl;
bullet.right = pr;
bullet.top = pt;
}
void initPaddle(){
}
void drawBullet(){
bullet.top = bt;
bullet.bottom = bb;
bullet.left = bl;
bullet.right = br;
FillRect(hdc,&bullet,(HBRUSH)GetStockObject(BLACK_BRUSH));
}
void drawPaddle(){
rect.bottom = pb;
rect.top = pt;
rect.left = pl;
rect.right = pr;
FillRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));
}
void moveBullet(){
bt -= bymove;
bb -= bymove;
bl -= bxmove;
br -= bxmove;
bymove = 0;
bxmove = 0;
}
void movePaddle(){
}
void MessageHandler(){
MSG msg;
if(PeekMessage(&msg,ghwnd,0,0,PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

Thanks



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
Learn to use the debugger more accurately. It''s going out of focus because it''s paused at a breakpoint. So step through the break point, and watch it work frame by frame. Then use the debugger to examine your variables and see if anything strange is happening.

Share this post


Link to post
Share on other sites
I can''t find the exact error, but you should try go through EACH stage in your game. For example... make sure when you initialize your bullet it is a) actually a proper rectangle ie: the Right coordinate is greater than the Left coordinate. b) the bullet''s variables aren''t overwritten by improper data. Another thing to check is that you are actually displaying a bullet. a) make a bullet and place it up in the corner of the screen to make sure it is created at the beginning. b) display the bullet next to the player so to make sure the player isn''t drawing over the bullet every time. You should also check the move bullet function, a) is it moving the bullet in the right diretion, b) is it changing the coordinates simultaneously. Best of luck, if you need any help feel free to e-mail me at bdstrand@georgefox.edu

Share this post


Link to post
Share on other sites
I figured it all out, but my code is very sloppy, so I think i''ll clean it up with oop.



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
<< I recently decide to make a game i thought pong but changed my mind to a game like galaga >>

My God, finish Pong first! Don''t tell me tomorrow you''ll give up on Galaga and try to make Doom. Then you''ve given up on Doom and try to make Halo. When will this stop.....

It stops with Pong, finish Pong dammit! And make the difficulty: easy, medium, hard, and impossible, where the last one the computer can''t lose.

If you can''t handle the amazing graphics of Pong, you can do the next best thing

Pong The Text Based Game

Phil P

Share this post


Link to post
Share on other sites