将图像添加到乒乓球游戏
Adding Images to Pong game
第一篇文章在这里:)
我正在编写一个乒乓球游戏,碰壁了。我的代码有效,但我想把它花哨一点。我想使用3张图片(每个球拍一张,球一张)。由于代码是现在编写的,我只有用于球拍的矩形和一个用于球的正方形。
这是我的代码:
// pongGame.cpp
#include "stdafx.h"
#include <string>
#include <Windows.h>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <math.h>
#include <glgl.h>
#include <glglu.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;
}
同样,如果有人能帮我弄清楚如何为游戏窗口添加背景,那也很棒:)
将纹理放在四边形上。
灵感:http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 乒乓球游戏窃听 - 可能指向类的指针
- 我的 kbhit() 不在 Mac 上工作,制作乒乓球游戏
- C++创建乒乓球游戏时出错
- 虽然循环失控的乒乓球游戏
- 将图像添加到乒乓球游戏
- 为什么对手的球拍在我的多人乒乓球游戏中滞后?