Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

example project for building pong on mac in C/OpenGL with Plandex

Dane Schneiderbe7cccdd202c
diff --git a/test/pong/.gitignore b/test/pong/.gitignore
new file mode 100644
index 00000000..15309787
--- /dev/null
+++ b/test/pong/.gitignore
@@ -0,0 +1 @@
+*.o
\ No newline at end of file
diff --git a/test/pong/Makefile b/test/pong/Makefile
new file mode 100644
index 00000000..3599c708
--- /dev/null
+++ b/test/pong/Makefile
@@ -0,0 +1,14 @@
+CC = gcc
+CFLAGS = -Wall -g
+LDFLAGS = -framework OpenGL -framework GLUT
+
+SRCS = main.c paddle.c ball.c render.c
+OBJS = $(SRCS:.c=.o)
+
+all: pong
+
+pong: $(OBJS)
+ $(CC) $(CFLAGS) -o pong $(OBJS) $(LDFLAGS)
+
+clean:
+ rm -f pong $(OBJS)
diff --git a/test/pong/README.md b/test/pong/README.md
new file mode 100644
index 00000000..5fc95e13
--- /dev/null
+++ b/test/pong/README.md
@@ -0,0 +1,27 @@
+# Building Pong in C/OpenGL with Plandex
+
+This directory is a result of running `plandex tell` with the `prompt.txt` file in the root of the repository, then using Plandex to work through a number of compiler errors.
+
+[Here's a video of the whole process.](https://www.youtube.com/watch?v=0ULjQx25S_Y)
+
+## Building and running
+
+The project was designed to be run on a mac.
+
+First install dependencies with:
+
+```bash
+./install_dependencies.sh
+```
+
+Then build the project with:
+
+```bash
+make
+```
+
+And run it with:
+
+```bash
+./pong
+```
\ No newline at end of file
diff --git a/test/pong/ball.c b/test/pong/ball.c
new file mode 100644
index 00000000..3864953a
--- /dev/null
+++ b/test/pong/ball.c
@@ -0,0 +1,54 @@
+#include <GLUT/glut.h>
+#include <OpenGL/gl.h>
+#include "ball.h"
+#include "paddle.h"
+
+extern float playerPaddleY; // Declare the external playerPaddleY variable
+extern float computerPaddleY; // Declare the external computerPaddleY variable
+
+float ballX = 400.0f;
+float ballY = 300.0f;
+float ballSpeedX = 4.0f;
+float ballSpeedY = 4.0f;
+
+void initBall() {
+ // Initialize ball position and speed
+ ballX = 400.0f;
+ ballY = 300.0f;
+ ballSpeedX = 4.0f;
+ ballSpeedY = 4.0f;
+}
+
+void updateBall() {
+ // Update ball position
+ ballX += ballSpeedX;
+ ballY += ballSpeedY;
+
+ // Check for collision with top and bottom walls
+ if (ballY + 10.0f > 600.0f || ballY - 10.0f < 0.0f) {
+ ballSpeedY = -ballSpeedY;
+ }
+
+ // Check for collision with player paddle
+ if (ballX - 10.0f < 70.0f && ballY > playerPaddleY - 50.0f && ballY < playerPaddleY + 50.0f) {
+ ballSpeedX = -ballSpeedX;
+ }
+
+ // Check for collision with computer paddle
+ if (ballX + 10.0f > 730.0f && ballY > computerPaddleY - 50.0f && ballY < computerPaddleY + 50.0f) {
+ ballSpeedX = -ballSpeedX;
+ }
+
+ // Check for scoring (ball goes out of bounds)
+ if (ballX + 10.0f > 800.0f || ballX - 10.0f < 0.0f) {
+ initBall(); // Reset ball position and speed
+ }
+}
+
+
+
+float getBallY() {
+ return ballY;
+}
+
+
diff --git a/test/pong/ball.h b/test/pong/ball.h
new file mode 100644
index 00000000..f5000748
--- /dev/null
+++ b/test/pong/ball.h
@@ -0,0 +1,9 @@
+#ifndef BALL_H
+#define BALL_H
+
+void initBall();
+void updateBall();
+void renderBall();
+float getBallY();
+
+#endif
diff --git a/test/pong/install_dependencies.sh b/test/pong/install_dependencies.sh
new file mode 100644
index 00000000..0d94dc86
--- /dev/null
+++ b/test/pong/install_dependencies.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+# Check for Homebrew and install if not found
+if ! command -v brew &> /dev/null
+then
+ echo "Homebrew not found. Installing Homebrew..."
+ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
+fi
+
+# Install necessary dependencies
+echo "Installing necessary dependencies..."
+brew install freeglut
+
+echo "Dependencies installed successfully."
diff --git a/test/pong/main.c b/test/pong/main.c
new file mode 100644
index 00000000..463b8aa0
--- /dev/null
+++ b/test/pong/main.c
@@ -0,0 +1,71 @@
+#include <GLUT/glut.h>
+#include <OpenGL/gl.h>
+#include <stdbool.h>
+#include "paddle.h"
+#include "ball.h"
+#include "render.h"
+
+bool keyStates[256] = { false }; // Array to hold the state of keys
+bool specialKeyStates[256] = { false }; // Array to hold the state of special keys
+
+void keyPressed(unsigned char key, int x, int y) {
+ keyStates[key] = true; // Set the state of the current key to pressed
+}
+
+void keyReleased(unsigned char key, int x, int y) {
+ keyStates[key] = false; // Set the state of the current key to released
+}
+
+void specialKeyPressed(int key, int x, int y) {
+ specialKeyStates[key] = true; // Set the state of the current special key to pressed
+}
+
+void specialKeyReleased(int key, int x, int y) {
+ specialKeyStates[key] = false; // Set the state of the current special key to released
+}
+
+void init() {
+ // Initialize game objects
+ initPaddle();
+ initBall();
+}
+
+void display() {
+ glClear(GL_COLOR_BUFFER_BIT);
+ renderPaddle();
+ renderBall();
+ glutSwapBuffers();
+}
+
+void update(int value) {
+ // Update game objects
+ updatePaddle();
+ updateBall();
+ glutPostRedisplay();
+ glutTimerFunc(16, update, 0); // 60 FPS
+}
+
+int main(int argc, char** argv) {
+ glutInit(&argc, argv);
+ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
+ glutInitWindowSize(800, 600);
+ glutCreateWindow("Pong Game");
+
+ // Set up the OpenGL context
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluOrtho2D(0.0, 800.0, 0.0, 600.0);
+ glMatrixMode(GL_MODELVIEW);
+
+ init();
+
+ glutDisplayFunc(display);
+ glutKeyboardFunc(keyPressed);
+ glutKeyboardUpFunc(keyReleased);
+ glutSpecialFunc(specialKeyPressed);
+ glutSpecialUpFunc(specialKeyReleased);
+ glutTimerFunc(16, update, 0);
+
+ glutMainLoop();
+ return 0;
+}
\ No newline at end of file
diff --git a/test/pong/paddle.c b/test/pong/paddle.c
new file mode 100644
index 00000000..c428f3e9
--- /dev/null
+++ b/test/pong/paddle.c
@@ -0,0 +1,52 @@
+#include <GLUT/glut.h>
+#include <OpenGL/gl.h>
+#include <stdbool.h>
+#include "paddle.h"
+#include "ball.h"
+
+extern bool keyStates[256]; // Declare the external keyStates array
+extern bool specialKeyStates[256]; // Declare the external specialKeyStates array
+
+float playerPaddleY = 300.0f;
+float computerPaddleY = 300.0f;
+float playerPaddleSpeed = 5.0f;
+float computerPaddleSpeed = 3.0f;
+
+void initPaddle() {
+ // Initialize paddle positions
+ playerPaddleY = 300.0f;
+ computerPaddleY = 300.0f;
+}
+
+void updatePaddle() {
+ // Update player paddle position based on keyboard input
+ if (specialKeyStates[GLUT_KEY_UP]) {
+ playerPaddleY += playerPaddleSpeed;
+ }
+ if (specialKeyStates[GLUT_KEY_DOWN]) {
+ playerPaddleY -= playerPaddleSpeed;
+ }
+
+ // Ensure the paddle stays within the window bounds
+ if (playerPaddleY + 50.0f > 600.0f) {
+ playerPaddleY = 550.0f;
+ }
+ if (playerPaddleY - 50.0f < 0.0f) {
+ playerPaddleY = 50.0f;
+ }
+
+ // Update computer paddle position based on ball position
+ if (getBallY() > computerPaddleY) {
+ computerPaddleY += computerPaddleSpeed;
+ } else if (getBallY() < computerPaddleY) {
+ computerPaddleY -= computerPaddleSpeed;
+ }
+
+ // Ensure the paddle stays within the window bounds
+ if (computerPaddleY + 50.0f > 600.0f) {
+ computerPaddleY = 550.0f;
+ }
+ if (computerPaddleY - 50.0f < 0.0f) {
+ computerPaddleY = 50.0f;
+ }
+}
\ No newline at end of file
diff --git a/test/pong/paddle.h b/test/pong/paddle.h
new file mode 100644
index 00000000..275c855a
--- /dev/null
+++ b/test/pong/paddle.h
@@ -0,0 +1,8 @@
+#ifndef PADDLE_H
+#define PADDLE_H
+
+void initPaddle();
+void updatePaddle();
+void renderPaddle();
+
+#endif
diff --git a/test/pong/pong b/test/pong/pong
new file mode 100755
index 00000000..5996e888
Binary files /dev/null and b/test/pong/pong differ
diff --git a/test/pong/prompt.txt b/test/pong/prompt.txt
new file mode 100644
index 00000000..4264adf6
--- /dev/null
+++ b/test/pong/prompt.txt
@@ -0,0 +1,7 @@
+Let's create a basic pong game using C and OpenGL. For now let's make it 1-player pong with the computer as the opponent. Create a basic AI for the computer that will move the paddle up and down based on the ball's position.
+
+Create all the necessary functionality for the project so that it will be ready to compile and run.
+
+Include a script that will install any necessary dependencies that are missing on Mac OSC. Prefer homebrew where possible for package management.
+
+Also include a Makefile that will compile the project.
diff --git a/test/pong/render.c b/test/pong/render.c
new file mode 100644
index 00000000..1f75debf
--- /dev/null
+++ b/test/pong/render.c
@@ -0,0 +1,21 @@
+#include <GLUT/glut.h>
+#include <OpenGL/gl.h>
+#include "paddle.h"
+#include "ball.h"
+
+extern float playerPaddleY; // Declare the external playerPaddleY variable
+extern float computerPaddleY; // Declare the external computerPaddleY variable
+extern float ballX; // Declare the external ballX variable
+extern float ballY; // Declare the external ballY variable
+
+void renderPaddle() {
+ // Render player paddle
+ glRectf(50.0f, playerPaddleY - 50.0f, 70.0f, playerPaddleY + 50.0f);
+ // Render computer paddle
+ glRectf(730.0f, computerPaddleY - 50.0f, 750.0f, computerPaddleY + 50.0f);
+}
+
+void renderBall() {
+ // Render the ball
+ glRectf(ballX - 10.0f, ballY - 10.0f, ballX + 10.0f, ballY + 10.0f);
+}
\ No newline at end of file
diff --git a/test/pong/render.h b/test/pong/render.h
new file mode 100644
index 00000000..c80ae0d2
--- /dev/null
+++ b/test/pong/render.h
@@ -0,0 +1,7 @@
+#ifndef RENDER_H
+#define RENDER_H
+
+void renderPaddle();
+void renderBall();
+
+#endif
diff --git a/test/test_prompts/pong.txt b/test/test_prompts/pong.txt
index 70d216d4..4264adf6 100644
--- a/test/test_prompts/pong.txt
+++ b/test/test_prompts/pong.txt
@@ -1,2 +1,7 @@
-let's create a basic pong game using C and OpenGL. create all the necessary functionality
- the project so that it will be ready to compile and run. create the whole thing in as few files as you can.
+Let's create a basic pong game using C and OpenGL. For now let's make it 1-player pong with the computer as the opponent. Create a basic AI for the computer that will move the paddle up and down based on the ball's position.
+
+Create all the necessary functionality for the project so that it will be ready to compile and run.
+
+Include a script that will install any necessary dependencies that are missing on Mac OSC. Prefer homebrew where possible for package management.
+
+Also include a Makefile that will compile the project.