#define WIN32_LEAN_AND_MEAN
#include<windows.h>
HDC hdc;
class CGame{
public:
CGame(){
Gameover = false;
}
bool Gameover;
}Game;
class CPaddle{
private:
int x;
int y;
int xchange;
int ychange;
RECT paddle;
public:
CPaddle(){
paddle.left = 300;
paddle.right = 340;
paddle.top = 430;
paddle.bottom = 440;
x = 320;
y = 435;
xchange = 0;
ychange = 0;
}
int getX(){
return x;
}
int getY(){
return y;
}
void setX(int newx){
x = newx;
}
void setY(int newy){
y = newy;
}
int getXChange(){
return xchange;
}
int getYChange(){
return ychange;
}
void setXChange(int change){
xchange = change;
}
void setYChange(int change){
ychange = change;
}
void movePaddle(){
x += xchange;
y += ychange;
xchange = 0;
ychange = 0;
}
void drawPaddle(HBRUSH brush){
paddle.left = x - 20;
paddle.right = x + 20;
paddle.top = y + 5;
paddle.bottom = y - 5;
FillRect(hdc,&paddle,brush);
}
}paddle;
class CBall{
private:
int x;
int y;
int xchange;
int ychange;
public:
CBall(){
x = 320;
y = 435;
xchange = 0;
ychange = 0;
}
int getX(){
return x;
}
int getY(){
return y;
}
void setX(int newx){
x = newx;
}
void setY(int newy){
y = newy;
}
int getXChange(){
return xchange;
}
int getYChange(){
return ychange;
}
void setXChange(int change){
xchange = change;
}
void setYChange(int change){
ychange = change;
}
void moveBall(){
x += xchange;
y += ychange;
xchange = 0;
ychange = 0;
}
void drawBall(HBRUSH brush){
SelectObject(hdc,(HPEN)GetStockObject(BLACK_PEN));
SelectObject(hdc,brush);
Ellipse(hdc,x,y,x+10,y+10);
}
}ball;
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_DESTROY:
Game.Gameover = true;
break;
case WM_KEYDOWN:
switch(wParam){
case 37:
paddle.drawPaddle((HBRUSH)GetStockObject(WHITE_BRUSH));
paddle.setXChange(-5);
break;
case 38:
SelectObject(hdc,(HPEN)GetStockObject(WHITE_PEN));
SelectObject(hdc,(HBRUSH)GetStockObject(WHITE_BRUSH));
Ellipse(hdc,ball.getX(),ball.getY(),ball.getX()+10,ball.getY()+10);
ball.setYChange(-20);
break;
case 39:
paddle.drawPaddle((HBRUSH)GetStockObject(WHITE_BRUSH));
paddle.setXChange(5);
break;
}
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hp,PSTR CmdLine,int iCmd){
WNDCLASS wc;
HWND hwnd;
MSG msg;
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 = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "Class";
wc.lpszMenuName = NULL;
wc.style = NULL;
RegisterClass(&wc);
hwnd = CreateWindow("Class","GalagaMe",WS_OVERLAPPEDWINDOW,0,0,640,480,NULL,NULL,hInstance,NULL);
hdc = GetDC(hwnd);
ShowWindow(hwnd,iCmd);
UpdateWindow(hwnd);
while(!Game.Gameover){
if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
paddle.movePaddle();
paddle.drawPaddle((HBRUSH)GetStockObject(BLACK_BRUSH));
ball.moveBall();
ball.drawBall((HBRUSH)GetStockObject(BLACK_BRUSH));
}
return 0;
}
Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|
Does this look better?
I rewrote this Do you think this is better:
Instead of having get/set methods that do nothing special why not just make those variable public?
Colin Jeanne | Invader''s Realm
Colin Jeanne | Invader''s Realm
Maybe some descriptive comments in your code would make it neater and easier understand what is going on.
Nice job though.
----
Mike
Portfolio: Http://members.iinet.net.au/~slyons
Team AI: Http://members.iinet.net.au/~slyons/teamai
Nice job though.
----
Mike
Portfolio: Http://members.iinet.net.au/~slyons
Team AI: Http://members.iinet.net.au/~slyons/teamai
I don't know, I was just taught to always make members private.
EDIT: and i planned on adding comments I normally add them when i'm done.
Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
[edited by - ssjcory on January 6, 2004 10:08:26 PM]
EDIT: and i planned on adding comments I normally add them when i'm done.
Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
[edited by - ssjcory on January 6, 2004 10:08:26 PM]
quote:Original post by SSJCORY
I don''t know, I was just taught to always make members private.
EDIT: and i planned on adding comments I normally add them when i''m done.
What''s the point of making a member private if you have functions that provide unrestricted (ie. public) access to it?
Colin Jeanne | Invader''s Realm
quote:Original post by Invader Xquote:Original post by SSJCORY
I don''t know, I was just taught to always make members private.
EDIT: and i planned on adding comments I normally add them when i''m done.
What''s the point of making a member private if you have functions that provide unrestricted (ie. public) access to it?
Colin Jeanne | Invader''s Realm
Well, if later on you want to limit access to variables, or put range checking on something or something, you''d be able to do that by simply modifying the function code rather than going through all your code and changing every public variable access...
this is just personal taste, but i think the using one line for a brace is cleaner that writing it after the arguments. for example like this:
int function(int x, int y, int z)
{
//whatever
}
but ONLY for functions
for nested if''s, while''s, for''s, etc.
it should be:
if (whatever) {
//whatever....
}
but again, it''s just what i do.
int function(int x, int y, int z)
{
//whatever
}
but ONLY for functions
for nested if''s, while''s, for''s, etc.
it should be:
if (whatever) {
//whatever....
}
but again, it''s just what i do.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement