The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Game Development
|
My first attempt at a game using opengl and a little bit of sdl
Discuss My first attempt at a game using opengl and a little bit of sdl in the Game Development forum on Dev Shed. My first attempt at a game using opengl and a little bit of sdl Game Development forum covering non language specific programming - game creation, design, modding, theories and math. A place for developers and gamers of all levels to discuss and debate all things involved in game creation and modding.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 28th, 2012, 01:56 PM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 14
Time spent in forums: 3 h 15 m 8 sec
Reputation Power: 0
|
|
|
My first attempt at a game using opengl and a little bit of sdl
Pong
This is my first attempt at a game, it isn't really completed, there are some problems with it, like sometimes the ball doesn't stay in the screen, it'll hit the top or bottom and keep going, it stays in the left and right of the window though.
Also, the score just keeps going up and up.
But, I was wondering if someone could suggest a better way to do the 'AI' since the way it is right now, the computer hits it every time, so it isn't very fun.
Let me know what you think of it, and if you can help with any of the problems I mentioned let me know!
Thanks!
Code:
#include <GL/glut.h>
#include <GL/GL.h>
#include <GL/GLU.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <iostream>
// variables for ball position, slope, if its moving
// right / up, ****ball speed to be added****
float a = 250.0f;
float b = 250.0f;
float m = 3.0f/2.0f;
float x = a;
float y = b;
bool mvRight = true;
bool mvUp = true;
// variables for the computer's paddle position, the
// computer's movement speed, ****paddle size to be added****
const float CPU_SPEED = 3.0f;
float pcX = 0; float pcY = 480;
// variables for the player's paddle position,
// ****movement speed, and paddle size to be added****
float plX = 0; float plY = 10;
// color values
float red = 1.0f; float green = 1.0f; float blue = 1.0f;
// the scores for player and computer
int scorePl = 0; int scorePc = 0;
// sounds
Mix_Chunk* bounce;
// mouse listener for the player's paddle
void mouse(int mx, int my){
plX = mx;
if(plX > 450)
plX = 450;
}
// set up some opengl options
void init(void){
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
bounce = Mix_LoadWAV("boing2.wav");
glClearColor(0, 0, 0, 1);
glPointSize(10.0f);
glViewport(0, 0, 500, 500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
}
// render the ball, computer and player paddles
// and also the text showing the score
void render(void){
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
// the ball
glColor3f(red, green, blue);
glBegin(GL_POINTS);
glVertex2d(x, y);
glEnd();
// player's paddle
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2d(plX, plY);
glVertex2d(plX + 50, plY);
glVertex2d(plX + 50, plY + 10);
glVertex2d(plX, plY + 10);
glEnd();
// the computer's paddle
glBegin(GL_QUADS);
glVertex2d(pcX, pcY);
glVertex2d(pcX + 50, pcY);
glVertex2d(pcX + 50, pcY + 10);
glVertex2d(pcX, pcY + 10);
glEnd();
// the scores
glColor3f(0.25f, 0.25f, 0.25f);
glRasterPos2f(235.0, 250.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, '0' + scorePc);
glRasterPos2f(250.0, 250.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, '-');
glRasterPos2f(265.0, 250.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, '0' + scorePl);
// ...swap buffers
glutSwapBuffers();
}
// update the computer paddle's position
void updateCPU(void){
if(x < pcX)
pcX -= CPU_SPEED;
else if((pcX + 50) < x)
pcX += CPU_SPEED;
}
// game logic for the ball's movement and position
void update(void){
y = (m * (x - a) + b);
if(mvRight){ // make sure ball stays in window - right
if(x < 500){
x += 2.5;
}else{
mvRight = false;
m *= -1; a = x; b = y;
}
}
if(!mvRight){ // make sure ball stays in window - left
if(x > 0){
x -= 2.5;
}else{
mvRight = true;
m *= -1; a = x; b = y;
}
}
if(mvUp){ // detect computer's paddle collision
if((y >= pcY) && (x <= pcX + 50) && (x >= pcX)){
m *= -1; a = x; b = y;
mvUp = false;
red = 1.0f; green = 0.0f; blue = 0.0f;
scorePc++;
Mix_PlayChannel(-1, bounce, 0);
}
if(y >= 500){ // make sure ball stays in window - top
m *= -1; a = x; b = y;
mvUp = false;
if(scorePc > 0)
scorePc--;
}
}
if(!mvUp){ // detect player paddle collision
if((y <= plY + 10) && (x <= plX + 50) && (x >= plX)){
m *= -1; a = x; b = y;
mvUp = true;
red = 0.0f; green = 1.0f; blue = 0.0f;
scorePl++;
Mix_PlayChannel(-1, bounce, 0);
}
if(y <= 0){ // make sure ball stays in window - bottom
m*= -1; a = x; b = y;
mvUp = true;
if(scorePl > 0)
scorePl--;
}
}
updateCPU(); // update the computer paddle's position
render(); // render everything
}
// callback for when window gets reshaped
void reshape(int width, int height){
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
glMatrixMode(GL_MODELVIEW);
}
// for animation around 60 fps
void timer(int iUnused){
glutPostRedisplay();
glutTimerFunc(16, timer, 0); // call itself every 12 ms
}
// application entry point - create window, set up some opengl
// options and set up callbacks for mouse listener,
// window resizing, and the main game loop
int main(int argc, char **argv){
glutInit(&argc, argv); // create the window and set some options
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Pong in OpenGL ... sort of");
init(); // set some rendering options and setup audio
glutDisplayFunc(update); // set the function for display callback
glutReshapeFunc(reshape); // set the function for reshape callback
timer(0); // setup a timer to refresh the canvas (every 12 ms, roughly 60 fps)
glutPassiveMotionFunc(mouse); // set the function for mouse movement callback
glutMainLoop(); // start the gl main loop
Mix_FreeChunk(bounce); // do some cleaning up
Mix_CloseAudio();
SDL_Quit();
exit(0);
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|