Added all previous files
This commit is contained in:
parent
4466ee8a4c
commit
f04fb2189a
59 changed files with 64921 additions and 0 deletions
19
lab9/Makefile
Normal file
19
lab9/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Makefile for lab 9
|
||||
# "make" or "make all : creates all three executables
|
||||
# "make symbol" : creates the symbol executable
|
||||
# (similarly for "make bounce" and "make funanim")
|
||||
|
||||
all: symbol bounce funanim
|
||||
|
||||
symbol: symbol.c
|
||||
gcc symbol.c gfx.o -lX11 -lm -o symbol
|
||||
|
||||
bounce: bounce.c
|
||||
gcc bounce.c gfx.o -lX11 -o bounce
|
||||
|
||||
funanim: funanim.c
|
||||
gcc funanim.c gfx.o -lX11 -o funanim
|
||||
|
||||
clean:
|
||||
rm symbol bounce funanim
|
||||
|
||||
78
lab9/bounce.c
Normal file
78
lab9/bounce.c
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundementals of Computing
|
||||
// Assignment: Lab 9, Part 2
|
||||
// Due: 26 November 2024
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "gfx.h"
|
||||
|
||||
#define WIN_LENGTH 1200
|
||||
#define WIN_HEIGHT 1000
|
||||
#define BALL_SIZE 200
|
||||
#define REF_RATE 30 // frames/sec
|
||||
#define MAX_VEL 200 // pixels/sec
|
||||
#define MIN_VEL 50
|
||||
|
||||
int randVel();
|
||||
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
|
||||
int keyPress;
|
||||
int xVel = randVel();
|
||||
int yVel = randVel();
|
||||
int xPos = WIN_LENGTH / 2;
|
||||
int yPos = WIN_HEIGHT / 2;
|
||||
|
||||
// Open new window, set color and draw first frame
|
||||
gfx_open(WIN_LENGTH, WIN_HEIGHT, "Bounce");
|
||||
gfx_color(255, 255, 255);
|
||||
gfx_circle(xPos, yPos, BALL_SIZE / 2);
|
||||
gfx_flush();
|
||||
|
||||
while (1) {
|
||||
usleep(100000 / REF_RATE);
|
||||
xPos = xPos + (xVel / REF_RATE);
|
||||
yPos = yPos + (yVel / REF_RATE);
|
||||
|
||||
// Change the direction of the velocity vectors if ball hits wall
|
||||
if (abs(xPos) - BALL_SIZE / 2 < 0 || abs(xPos) + BALL_SIZE / 2 > WIN_LENGTH) xVel = -xVel;
|
||||
if (abs(yPos) - BALL_SIZE / 2 < 0 || abs(yPos) + BALL_SIZE / 2 > WIN_HEIGHT) yVel = -yVel;
|
||||
|
||||
// Draw the next frame
|
||||
gfx_clear();
|
||||
gfx_circle(xPos, yPos, BALL_SIZE / 2);
|
||||
gfx_flush();
|
||||
|
||||
// Check if user has pressed key
|
||||
if (gfx_event_waiting()) {
|
||||
keyPress = gfx_wait();
|
||||
// Update position and generate random velocity if left click
|
||||
if (keyPress == 1) {
|
||||
xPos = gfx_xpos();
|
||||
yPos = gfx_ypos();
|
||||
xVel = randVel();
|
||||
yVel = randVel();
|
||||
}
|
||||
// If user hit escape, exit
|
||||
else if (keyPress == 27) break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int randVel() {
|
||||
srand(time(NULL) + rand());
|
||||
int randNum = 1;
|
||||
// Generate a random number in range, if zero repeat until nonzero
|
||||
do {
|
||||
randNum = -MAX_VEL + rand() % (2 * MAX_VEL + 1);
|
||||
} while (abs(randNum) < MIN_VEL);
|
||||
return randNum;
|
||||
}
|
||||
156
lab9/funanim.c
Normal file
156
lab9/funanim.c
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundementals of Computing
|
||||
// Assignment: Lab 9, Part 3
|
||||
// Due: 26 November 2024
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "gfx.h"
|
||||
|
||||
#define WIN_LENGTH 1200
|
||||
#define WIN_HEIGHT 1000
|
||||
#define HOOP_RADIUS 75
|
||||
#define BALL_RADIUS 50
|
||||
#define HOOP_SPEED 400 // pixels/sec
|
||||
#define BALL_SPEED_MULT 300
|
||||
#define BALL_NUM 5
|
||||
#define REF_RATE 30 // frames/sec
|
||||
|
||||
void startScreen();
|
||||
void nextFrame(int*, int, int*, int*, int*);
|
||||
void drawShootingLine();
|
||||
void drawHoop(int);
|
||||
void drawBall(int, int);
|
||||
bool scored(int, int, int);
|
||||
void printStats(int, int);
|
||||
|
||||
int main() {
|
||||
gfx_open(WIN_LENGTH, WIN_HEIGHT, "Ball Toss");
|
||||
|
||||
int ballsLeft = BALL_NUM;
|
||||
int ballXPos, ballYPos;
|
||||
int ballSpeed = 0;
|
||||
int hoopXPos = 75;
|
||||
int hoopSpeed = HOOP_SPEED;
|
||||
int score = 0;
|
||||
|
||||
// Draw shooting line, hoop and ball and flush
|
||||
drawShootingLine();
|
||||
drawHoop(hoopXPos);
|
||||
gfx_flush();
|
||||
|
||||
// Call the start screen
|
||||
startScreen();
|
||||
|
||||
while (1) {
|
||||
usleep(1000000 / REF_RATE);
|
||||
|
||||
// Iterate to the next frame, performing calculations
|
||||
nextFrame(&hoopXPos, ballXPos, &ballYPos, &hoopSpeed, &ballSpeed);
|
||||
printStats(score, ballsLeft);
|
||||
gfx_flush();
|
||||
|
||||
// If scored, add to score
|
||||
if (scored(hoopXPos, ballXPos, ballYPos)) {
|
||||
score += 1100 - (100 * ballSpeed / BALL_SPEED_MULT);
|
||||
ballSpeed = 0;
|
||||
ballYPos = -100;
|
||||
}
|
||||
|
||||
// If no balls left, display score and exit
|
||||
if (ballsLeft == 0 && ballSpeed == 0) {
|
||||
char finalScore[25];
|
||||
sprintf(finalScore, "Final Score: %d", score);
|
||||
gfx_text(WIN_LENGTH / 2, WIN_HEIGHT / 2, finalScore);
|
||||
gfx_flush();
|
||||
usleep(3000000);
|
||||
break;
|
||||
}
|
||||
|
||||
// Check for user input 1-9 and esc
|
||||
if (gfx_event_waiting()) {
|
||||
int input = gfx_wait();
|
||||
if (input == 27) break;
|
||||
else if (input >= '1' && input <= '9' && ballSpeed == 0) {
|
||||
ballXPos = gfx_xpos();
|
||||
ballYPos = WIN_HEIGHT - BALL_RADIUS;
|
||||
ballSpeed = (input - '0') * BALL_SPEED_MULT;
|
||||
ballsLeft--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void startScreen() {
|
||||
// Print instructions on how to play
|
||||
gfx_color(0, 255, 0);
|
||||
gfx_text(WIN_LENGTH / 2 - 200, WIN_HEIGHT / 2 - 20, "Shoot balls by placing mouse horizontally, then hit keys 1-9. Higher number = faster ball.");
|
||||
gfx_text(WIN_LENGTH / 2 - 200, WIN_HEIGHT / 2, "If you score with a slower ball, you get more points.");
|
||||
gfx_text(WIN_LENGTH / 2 - 200, WIN_HEIGHT / 2 + 20, "Press [Enter] to begin.");
|
||||
|
||||
gfx_flush();
|
||||
|
||||
// Wait until user hits enter to continue
|
||||
int input;
|
||||
while (1) {
|
||||
input = gfx_wait();
|
||||
if (input == 13) break;
|
||||
}
|
||||
}
|
||||
|
||||
void nextFrame(int* hoopXPos, int ballXPos, int* ballYPos, int* hoopSpeed, int* ballSpeed) {
|
||||
// Update hoop x position
|
||||
*hoopXPos = *hoopXPos + (*hoopSpeed / REF_RATE);
|
||||
if (*hoopXPos + HOOP_RADIUS >= WIN_LENGTH || *hoopXPos - HOOP_RADIUS <= 0) *hoopSpeed = -(*hoopSpeed);
|
||||
|
||||
// Update ball y position
|
||||
if (ballSpeed > 0) {
|
||||
*ballYPos = *ballYPos - (*ballSpeed / REF_RATE);
|
||||
if (*ballYPos + BALL_RADIUS < 0) *ballSpeed = 0;
|
||||
}
|
||||
|
||||
// Clear screen and draw elements
|
||||
gfx_clear();
|
||||
drawShootingLine();
|
||||
drawHoop(*hoopXPos);
|
||||
if (ballSpeed > 0 && ballXPos != 0) drawBall(ballXPos, *ballYPos);
|
||||
}
|
||||
|
||||
void drawShootingLine() {
|
||||
gfx_color(255, 255, 255);
|
||||
gfx_line(50, WIN_HEIGHT - BALL_RADIUS * 2, WIN_LENGTH - 50, WIN_HEIGHT - BALL_RADIUS * 2);
|
||||
}
|
||||
|
||||
void drawHoop(int xPos) {
|
||||
gfx_color(255, 0, 0);
|
||||
gfx_circle(xPos, HOOP_RADIUS, HOOP_RADIUS);
|
||||
}
|
||||
|
||||
void drawBall(int xPos, int yPos) {
|
||||
gfx_color(255, 165, 0);
|
||||
gfx_circle(xPos, yPos, BALL_RADIUS);
|
||||
}
|
||||
|
||||
bool scored(int hoopXPos, int ballXPos, int ballYPos) {
|
||||
// Return true if ball is within hoop
|
||||
return abs(HOOP_RADIUS - ballYPos) <= HOOP_RADIUS - BALL_RADIUS &&
|
||||
abs(hoopXPos - ballXPos) <= HOOP_RADIUS - BALL_RADIUS;
|
||||
}
|
||||
|
||||
void printStats(int score, int ballsLeft) {
|
||||
char scoreString[20], ballString[20];
|
||||
sprintf(scoreString, "Score: %d", score);
|
||||
sprintf(ballString, "Balls left: %d", ballsLeft);
|
||||
|
||||
// Print current score and balls remaining
|
||||
gfx_color(0, 255, 0);
|
||||
gfx_text(WIN_LENGTH - 100, WIN_HEIGHT - 300, scoreString);
|
||||
gfx_text(WIN_LENGTH - 100, WIN_HEIGHT - 250, ballString);
|
||||
}
|
||||
51
lab9/gfx.h
Normal file
51
lab9/gfx.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
// A simple graphics library for CSE 20311
|
||||
// Last edit: Nov 15, 2024
|
||||
|
||||
#ifndef GFX_H
|
||||
#define GFX_H
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
// Open a new graphics window.
|
||||
void gfx_open( int width, int height, const char *title );
|
||||
|
||||
// Flush all previous output to the window.
|
||||
void gfx_flush();
|
||||
|
||||
// Change the current drawing color.
|
||||
void gfx_color( int red, int green, int blue );
|
||||
|
||||
// Clear the graphics window to the background color.
|
||||
void gfx_clear();
|
||||
|
||||
// Change the current background color.
|
||||
void gfx_clear_color( int red, int green, int blue );
|
||||
|
||||
// Check to see if an event is waiting.
|
||||
int gfx_event_waiting();
|
||||
|
||||
// Wait for the user to press a key or mouse button.
|
||||
char gfx_wait();
|
||||
|
||||
// Return the X and Y coordinates of the last event.
|
||||
int gfx_xpos();
|
||||
int gfx_ypos();
|
||||
|
||||
// Return the X and Y dimensions of the screen (monitor).
|
||||
int gfx_xsize();
|
||||
int gfx_ysize();
|
||||
|
||||
// Draw a point at (x,y)
|
||||
void gfx_point( int x, int y );
|
||||
|
||||
// Draw a line from (x1,y1) to (x2,y2)
|
||||
void gfx_line( int x1, int y1, int x2, int y2 );
|
||||
|
||||
// Draw a circle centered at (xc,yc) with radius r
|
||||
void gfx_circle( int xc, int yc, int r );
|
||||
|
||||
// Display a string at (x,y)
|
||||
void gfx_text( int x, int y , const char *text );
|
||||
|
||||
#endif
|
||||
|
||||
BIN
lab9/gfx.o
Normal file
BIN
lab9/gfx.o
Normal file
Binary file not shown.
108
lab9/symbol.c
Normal file
108
lab9/symbol.c
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundementals of Computing
|
||||
// Assignment: Lab 9, Part 1
|
||||
// Due: 24 November 2024
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "gfx.h"
|
||||
|
||||
#define SIZE 100
|
||||
|
||||
void poly(int x, int y, int numSides);
|
||||
void triangle(int x, int y);
|
||||
void square(int x, int y);
|
||||
|
||||
int main() {
|
||||
int inpt;
|
||||
int x, y;
|
||||
|
||||
gfx_open(600, 600, "Symbolic Typewriter");
|
||||
|
||||
|
||||
while(1) {
|
||||
inpt = gfx_wait();
|
||||
x = gfx_xpos();
|
||||
y = gfx_ypos();
|
||||
|
||||
switch (inpt) {
|
||||
// Check for letter inputs
|
||||
case 'c':
|
||||
gfx_color(255, 255, 255);
|
||||
gfx_circle(x, y, SIZE / 2);
|
||||
break;
|
||||
case 't':
|
||||
gfx_color(0, 255, 0);
|
||||
triangle(x, y);
|
||||
break;
|
||||
case 'q':
|
||||
goto end;
|
||||
break;
|
||||
default:
|
||||
// Check for number inputs
|
||||
if (isdigit(inpt)) {
|
||||
if (inpt == '1') {
|
||||
gfx_color(0, 0, 255);
|
||||
square(x, y);
|
||||
}
|
||||
else if (inpt >= '3' && inpt <= '9') {
|
||||
gfx_color(255, 0, 255);
|
||||
poly(x, y, inpt - '0');
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Check for left click and esc
|
||||
if (inpt == 1) {
|
||||
gfx_color(0, 0, 255);
|
||||
square(x, y);
|
||||
}
|
||||
else if (inpt == 27) gfx_clear();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
end: return 0;
|
||||
}
|
||||
|
||||
void poly(int x, int y, int numSides) {
|
||||
int radius = SIZE / 2;
|
||||
float angleStep = 360.0 / numSides;
|
||||
int x1 = x + radius;
|
||||
int y1 = y;
|
||||
int x2, y2;
|
||||
float angle;
|
||||
|
||||
// Draw lines based on number of sides
|
||||
for (int i = 1; i < numSides + 1; i++) {
|
||||
angle = angleStep * i;
|
||||
x2 = x + radius * cos(angle * M_PI / 180);
|
||||
y2 = y + radius * sin(angle * M_PI / 180);
|
||||
gfx_line(x1, y1, x2, y2);
|
||||
|
||||
x1 = x2;
|
||||
y1 = y2;
|
||||
}
|
||||
}
|
||||
|
||||
void square(int x, int y) {
|
||||
int halfSide = SIZE / 2;
|
||||
|
||||
// Draw the square
|
||||
gfx_line(x + halfSide, y - halfSide, x + halfSide, y + halfSide);
|
||||
gfx_line(x + halfSide, y + halfSide, x - halfSide, y + halfSide);
|
||||
gfx_line(x - halfSide, y + halfSide, x - halfSide, y - halfSide);
|
||||
gfx_line(x - halfSide, y - halfSide, x + halfSide, y - halfSide);
|
||||
}
|
||||
|
||||
void triangle(int x, int y) {
|
||||
int height = SIZE;
|
||||
int halfWidth = SIZE / 2;
|
||||
|
||||
// Draw the triangle with the top at (x, y)
|
||||
gfx_line(x - halfWidth, y + height, x + halfWidth, y + height);
|
||||
gfx_line(x - halfWidth, y + height, x, y);
|
||||
gfx_line(x + halfWidth, y + height, x, y);
|
||||
}
|
||||
Loading…
Reference in a new issue