gpt4 book ai didi

c++ - 将图像添加到 Pong 游戏

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:20:29 26 4
gpt4 key购买 nike

第一次发帖:)

我正在编写 Pong 游戏的代码,但碰壁了。我的代码有效,但我想稍微花点时间。我有 3 张图像要使用(每个桨一张,球一张)。由于代码是现在编写的,我只有长方形的桨和一个正方形的球。

这是我的代码:

// 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;
}

同样,如果有人能帮我弄清楚如何为游戏窗口添加背景,那就太棒了:)

最佳答案

关于c++ - 将图像添加到 Pong 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27034126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com