Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <freeglut.h>
- #include <gl\GL.h>
- #include <gl\GLU.h>
- #include "ball.hpp"
- #define VK_Z 0x5A
- #define VK_S 0x53
- #pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
- int width = 800;
- int height = 600;
- int interval = 1000 / 60;
- int score_player1 = 0;
- int score_player2 = 0;
- int racket_width = 10;
- int racket_height = 80;
- int racket_speed = 8;
- int racket_left_x = 10;
- int racket_left_y = 50;
- int racket_right_x = width - racket_width - 10;
- int racket_right_y = 50;
- float ball_pos_x = width / 2;
- float ball_pos_y = height / 2;
- float ball_dir_x = -1.0f;
- float ball_dir_y = 0.0f;
- ball pong(8,5);
- 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() {
- ball_pos_x += ball_dir_x * pong.getSpeed();
- ball_pos_y += ball_dir_y * pong.getSpeed();
- if (ball_pos_x < racket_left_x + racket_width &&
- ball_pos_x > racket_left_x &&
- ball_pos_y < racket_left_y + racket_height &&
- ball_pos_y > racket_left_y) {
- float t = ((ball_pos_y - racket_left_y) / racket_height) - 0.5f;
- ball_dir_x = fabs(ball_dir_x);
- ball_dir_y = t;
- }
- if (ball_pos_x > racket_right_x &&
- ball_pos_x < racket_right_x + racket_width &&
- ball_pos_y < racket_right_y + racket_height &&
- ball_pos_y > racket_right_y) {
- float t = ((ball_pos_y - racket_right_y) / racket_height) - 0.5f;
- ball_dir_x = -fabs(ball_dir_x);
- ball_dir_y = t;
- }
- if (ball_pos_x < 0) {
- ++score_player2;
- ball_pos_x = width / 2;
- ball_pos_y = height / 2;
- ball_dir_x = fabs(ball_dir_x);
- ball_dir_y = 0;
- }
- if (ball_pos_x > width) {
- ++score_player1;
- ball_pos_x = width / 2;
- ball_pos_y = height / 2;
- ball_dir_x = -fabs(ball_dir_x);
- ball_dir_y = 0;
- }
- if (ball_pos_y > height) {
- ball_dir_y = -fabs(ball_dir_y);
- }
- if (ball_pos_y < 0) {
- ball_dir_y = fabs(ball_dir_y);
- }
- vec2_norm(ball_dir_x, ball_dir_y);
- }
- void keyboard() {
- if (GetAsyncKeyState(VK_Z)) {
- if (racket_left_y >= height - racket_height) {
- racket_left_y += 0;
- } else {
- racket_left_y += racket_speed;
- }
- }
- if (GetAsyncKeyState(VK_S)) {
- if (racket_left_y <= 0) {
- racket_left_y += 0;
- } else {
- racket_left_y -= racket_speed;
- }
- }
- if (GetAsyncKeyState(VK_UP)) {
- if (racket_right_y >= height - racket_height) {
- racket_right_y += 0;
- }
- else {
- racket_right_y += racket_speed;
- }
- }
- if (GetAsyncKeyState(VK_DOWN)) {
- if (racket_right_y <= 0) {
- racket_right_y += 0;
- }
- else {
- racket_right_y -= racket_speed;
- }
- }
- }
- void drawRect(int x, int y, int width, int height) {
- glBegin(GL_QUADS);
- glVertex2i(x, y);
- glVertex2i(x + width, y);
- glVertex2i(x + width, y + height);
- glVertex2i(x, y + height);
- glEnd();
- }
- std::string int2str(int x) {
- std::stringstream ss;
- ss << x;
- return ss.str();
- }
- void drawScore(float x, float y, std::string text) {
- glRasterPos2f(x, y);
- glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)text.c_str());
- }
- void draw() {
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- if (score_player1 >= 10) {
- score_player2--;
- drawScore((width / 2) - 70, height / 2, "Player 1 won");
- } else if (score_player2 >= 10) {
- score_player1--;
- drawScore((width / 2) - 70, height / 2, "Player 2 won");
- }
- else {
- drawScore(width / 2 - 10, height - 40, int2str(score_player1) + ":" + int2str(score_player2));
- drawScore(10.0f, 10.0f, int2str(racket_left_x));
- drawScore(600.0f, 10.0f, int2str(racket_right_x));
- drawRect(ball_pos_x - pong.getSize() / 2, ball_pos_y - pong.getSize() / 2, pong.getSize(), pong.getSize());
- drawRect(racket_left_x, racket_left_y, racket_width, racket_height);
- drawRect(racket_right_x, racket_right_y, racket_width, racket_height);
- }
- glutSwapBuffers();
- }
- void update(int value) {
- keyboard();
- updateBall();
- glutTimerFunc(interval, update, 0);
- glutPostRedisplay();
- }
- 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();
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(width, height);
- glutCreateWindow("Pong");
- glutDisplayFunc(draw);
- glutTimerFunc(interval, update, 0);
- enable2D(width, height);
- glColor3f(1.0f, 1.0f, 1.0f);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment