first post here :)
I'm working on coding a game of Pong and have hit a wall. My code works, but I'd like to fancy it up a little. I have 3 images that I'd like to use (one for each paddle, and one for the ball). As the code is written right now, I just have rectangles for the paddles and a square for the ball.
Here's my code:
// pongGame.cpp
#include "stdafx.h"
#include <string>
#include <Windows.h>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <math.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include "GL/freeglut.h"
#pragma comment(lib, "Opengl32.lib")
#define VK_W 0x57
#define VK_S 0x53
using namespace std;
//window size and update rate
int width = 500;
int height = 300;
int interval = 1000 / 60; // 60 frames per-second
//scoring
int p1Score = 0; //Player 1's score
int p2Score = 0; //Player 2's score
int winner = 0;
//the paddles
int paddleWidth = 10;
int paddleHeight = 80;
int paddleSpeed = 3;
float paddleLeftX = 10.0f;
float paddleLeftY = 50.0f;
float paddleRightX = width - paddleWidth - 10;
float paddleRightY = 50;
//the ball
float ballPositionX = width / 2;
float ballPositionY = height / 2;
float ballDirectionX = -1.0f;
float ballDirectionY = 0.0f;
int ballSize = 8;
int ballSpeed = 4;
std::string int2str(int x) { //used to convert an integer to a string
std::stringstream ss;
ss << x;
return ss.str( );
}
void drawText(float x, float y, std::string text) {
glRasterPos2f(x, y);
glutBitmapString(GLUT_BITMAP_8_BY_13, (const unsigned char*)text.c_str());
}
void drawPaddle(float x, float y, float width, float height) {
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//draws the paddles
drawPaddle(paddleLeftX, paddleLeftY, paddleWidth, paddleHeight);
drawPaddle(paddleRightX, paddleRightY, paddleWidth, paddleHeight);
//draws the ball
drawPaddle(ballPositionX - ballSize / 2, ballPositionY- ballSize / 2, ballSize, ballSize);
//draws the score at the top center of the screen
drawText(width / 2 - 10, height - 15, int2str(p1Score) + ":" + int2str(p2Score));
glutSwapBuffers();
}
void enable2D(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, width, 0.0f, height, 0.0f, 1.0f);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
void keyboard() { //allows teh paddles to be controled from the keyboard
//moves left paddle (player 1)
if (GetAsyncKeyState(VK_W))
paddleLeftY += paddleSpeed; //move paddle up with "W" key
if (GetAsyncKeyState(VK_S))
paddleLeftY -= paddleSpeed; //move paddle down with "S" key
//moves right paddle (player 2)
if (GetAsyncKeyState(VK_UP))
paddleRightY += paddleSpeed; //move paddle up with "up" arrow
if (GetAsyncKeyState(VK_DOWN))
paddleRightY -= paddleSpeed; //move paddle down with "down" arrow
}
void vec2_norm(float& x, float &y) {
float length = sqrt((x * x) + (y * y));
if(length != 0.0f) {
length = 1.0f / length;
x *= length;
y *= length;
}
}
void updateBall() { //allows teh ball to move
ballPositionX += ballDirectionX * ballSpeed;
ballPositionY += ballDirectionY * ballSpeed;
if(ballPositionX < paddleLeftX + paddleWidth && ballPositionX > paddleLeftX && ballPositionY < paddleLeftY + paddleHeight && ballPositionY > paddleLeftY) { //if ball is hit by player 1's paddle
float t = ((ballPositionY - paddleLeftY) / paddleHeight) - 0.5f;
ballDirectionX = fabs(ballDirectionX);
ballDirectionY = t;
}
if (ballPositionX > paddleRightX && ballPositionX < paddleRightX + paddleWidth && ballPositionY < paddleRightY + paddleHeight && ballPositionY > paddleRightY) { //if ball is hit by player 2's paddle
float t = ((ballPositionY - paddleRightY) / paddleHeight) - 0.5f;
ballDirectionX = -fabs(ballDirectionX);
ballDirectionY = t;
}
if (ballPositionX < 0) { //if ball hits the top wall
++p2Score;
ballPositionX = width / 2;
ballPositionY = height / 2;
ballDirectionX = fabs(ballDirectionX);
ballDirectionY = 0;
}
if (ballPositionX > width) { //if ball hits the right wall
++p1Score;
ballPositionX = width / 2;
ballPositionY = height / 2;
ballDirectionX = -fabs(ballDirectionX);
ballDirectionY = 0;
}
if (ballPositionY > height) { //ball hits top wall
ballDirectionY = -fabs(ballDirectionY);
}
if (ballPositionY < 0) { //ball hits bottom wall
ballDirectionY = fabs(ballDirectionY);
}
vec2_norm(ballDirectionX, ballDirectionY);
}
void gameOverCheck() {
const int maxScore = 10;
if(p1Score == maxScore) {
cout << "Player 1 Wins!" << endl;
winner = 1;
}
else if(p2Score == maxScore) {
cout << "Player 2 Wins!" << endl;
winner = 2;
}
}
void update(int value) {
keyboard();
if(winner == 0) {
updateBall();
glutTimerFunc(interval, update, 0);
glutPostRedisplay();
gameOverCheck();
}
}
int _tmain(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 200);
glutCreateWindow("Pong");
glutDisplayFunc(draw);
glutTimerFunc(interval, update, 0);
enable2D(width, height);
glColor3f(1.0f, 0.0f, 0.0f);
glutMainLoop();
return 0;
}
Along the same lines, if someone could help me figure out how to add a background to the game window, that'd be awesome too :)
Put textures on your quads.
Inspiration: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/