Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- below is my code for pong that i am currently working on but have come to a bit of a dead end i cant firgure out how to write collision for my code any advice or sample code would be nice have fun reading i tried to keep my code neat.
- // SDL_App.cpp : Defines the entry point for our Project
- //
- //#include "stdafx.h"
- #include <Windows.h>
- #include "GL_Functions.h"
- #include "pctimer.h"
- #include <string>
- #include <cmath>
- #include <crtdbg.h>
- // theme of project is pong from a metroid perspective
- //{ make this a function later =D
- enum STATE
- {
- MAINMENU, // will take user to main menu at start of game or during
- SINGLEPLAYER, // one player mode
- MULTIPLAYER, // player vs player mode
- HIGHSCORES, //lists high score
- OPTIONS, // opens the options menu
- QUIT // when user presses escape key program closes
- };
- int main(int argc, char* argv[])
- {
- //Lets open the window and initialise opengl
- InitGL(1024,768);
- int background = LoadTexture ("./images/background.png");
- int mball = LoadTexture ("./images/mball.png");
- int leftpaddle_2 = LoadTexture ("./images/paddle_2.png");
- int rightpaddle_1 = LoadTexture ("./images/paddle_1.png");
- //int Menu = LoadTexture ("./images/MENU.png");
- //stuff to complete.......!!!!
- //STATE currentstate;
- //bool singeplayer()
- //{
- //currentState = MAINMENU
- //while(bContinueGame)
- //{
- //case
- //}
- float backgroundx = 0;
- float backgroundy = 0;
- float mballx = 492; // 'M' stand for metroid.
- float mbally = 384;
- float ballxspeed = 4;
- float ballyspeed = 2;
- float rightpaddle_1x = 990;
- float rightpaddle_1y = 20;
- float leftpaddle_2x = 15;
- float leftpaddle_2y = 450;
- float ballrside = mballx + 40;
- float position = 0;
- float paddle_width = 5;
- float paddle_length = 15;
- float ball_width = 2.5;
- float ball_length = 5;
- //while true()
- //{ make this a function later =D
- // enum STATE
- // MAINMENU // will take user to main menu at start of game or during
- // SINGLEPLAYER // one player mode
- // MULTIPLAYER // player vs player mode
- // HIGHSCORES //lists high score
- // OPTIONS // opens the options menu
- // QUIT // when user selects 'quit' program closes
- //}
- do
- {
- //Clear the screen, so previous frames don't build up
- ClearScreen();
- mballx = mballx + ballxspeed;
- mbally = mbally + ballyspeed;
- //////////draws images to screen////////////////////////////////////////////////////
- DrawSprite(background, backgroundx, backgroundy, 1024, 768); //////////////
- DrawLine(512,0,512,768); //////////////
- DrawSprite(mball, mballx, mbally, 40, 40); //////////////
- DrawSprite(leftpaddle_2, leftpaddle_2x, leftpaddle_2y, 20, 100); //////////
- DrawSprite(rightpaddle_1, rightpaddle_1x, rightpaddle_1y, 20, 100); ////////
- ////////////////////////////////////////////////////////////////////////////////////
- //highscores
- //{
- // int iHighscore[6}
- // int iScore = 0
- // char scorestring[] = "score"
- // while (iscore < 6)
- // printf("Highscore: %i %i/n"), (iScore+1), iHighscore[iScore]);
- // ++iScore;
- // for{
- // (int iHighscore = 0;, iHighscore < 10, iHighscore++)
- // printf(
- // }
- //}
- // ball speed/control
- //
- if (mballx > 984)
- {
- mballx = 492;
- mbally = 384;
- ballxspeed *= -1;
- }
- if ( mballx < 0)
- {
- mballx = 492;
- mbally = 384;
- ballxspeed *= -1;
- }
- if (mbally > 1024 || mbally < 0)
- ballyspeed *= -.5;
- // controls for players one and two
- // player one's controls ( 'w' and 's')
- // player two's controls ( 'up' and 'down')
- if (IsKeyDown(KEY_UP))
- {
- rightpaddle_1y = rightpaddle_1y -5;
- if( rightpaddle_1y < 0 )
- {
- rightpaddle_1y = 0;
- }
- }
- if (IsKeyDown(KEY_DOWN) && rightpaddle_1 < 668)
- rightpaddle_1y = rightpaddle_1y +5;
- if (IsKeyDown('w') && leftpaddle_2 > 0)
- leftpaddle_2y = leftpaddle_2y -5;
- if (IsKeyDown('s') && leftpaddle_2 < 668)
- leftpaddle_2y = leftpaddle_2y +5;
- // closes program down atfer user presses escape!
- if (IsKeyDown(KEY_ESCAPE))
- CloseDown();
- //limit paddles so that stay with in boundaries placed
- // well they would if i could program
- {
- if(leftpaddle_2y < 0)
- leftpaddle_2y = 0;
- if(leftpaddle_2y + paddle_width > 668)
- leftpaddle_2y = 668 - paddle_width;
- if(rightpaddle_1y + paddle_width > 668)
- rightpaddle_1y = 668 - paddle_width;
- }
- //ball impact values below
- //makes ball go bouncy bouncy
- //ball collision should be here....
- {
- if(ball_length + ball_width > paddle_width + paddle_length)
- mballx,mbally = 0;
- // these do stuff
- // without them game crashes....
- if(mballx > rightpaddle_1x + rightpaddle_1y)
- if(mballx = leftpaddle_2x - position && leftpaddle_2y > ball_width)
- if(mbally = rightpaddle_1x + position && rightpaddle_1x > ball_width)
- return(0);
- }
- //Stop it from running too fast! Sleep ZZzzz
- Sleep(5);
- } while (FrameworkUpdate()); //Do some secret stuff,
- FreeTexture(leftpaddle_2);
- FreeTexture(rightpaddle_1);
- FreeTexture(background);
- FreeTexture(mball);
- //Close down
- CloseDown();
- _CrtDumpMemoryLeaks();
- //Quit!
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment