241 lines
8.0 KiB
C
241 lines
8.0 KiB
C
#include <gint/display.h>
|
|
#include <gint/keyboard.h>
|
|
#include "3dline.h"
|
|
#include "conf.h"
|
|
|
|
void draw_arena()
|
|
{
|
|
dddline(X_WIDTH, Y_HEIGHT, Z_DEPTH, X_WIDTH, Y_HEIGHT, -Z_DEPTH, C_WHITE);
|
|
dddline(-X_WIDTH, Y_HEIGHT, Z_DEPTH, -X_WIDTH, Y_HEIGHT, -Z_DEPTH, C_WHITE);
|
|
dddline(-X_WIDTH, -Y_HEIGHT, Z_DEPTH, -X_WIDTH, -Y_HEIGHT, -Z_DEPTH, C_WHITE);
|
|
dddline(X_WIDTH, -Y_HEIGHT, Z_DEPTH, X_WIDTH, -Y_HEIGHT, -Z_DEPTH, C_WHITE);
|
|
|
|
dddline(X_WIDTH, Y_HEIGHT, Z_DEPTH, -X_WIDTH, Y_HEIGHT, Z_DEPTH, C_WHITE);
|
|
dddline(X_WIDTH, -Y_HEIGHT, Z_DEPTH, -X_WIDTH, -Y_HEIGHT, Z_DEPTH, C_WHITE);
|
|
dddline(-X_WIDTH, Y_HEIGHT, Z_DEPTH, -X_WIDTH, -Y_HEIGHT, Z_DEPTH, C_WHITE);
|
|
dddline(X_WIDTH, Y_HEIGHT, Z_DEPTH, X_WIDTH, -Y_HEIGHT, Z_DEPTH, C_WHITE);
|
|
|
|
dddline(X_WIDTH, Y_HEIGHT, -Z_DEPTH, -X_WIDTH, Y_HEIGHT, -Z_DEPTH, C_WHITE);
|
|
dddline(X_WIDTH, -Y_HEIGHT, -Z_DEPTH, -X_WIDTH, -Y_HEIGHT, -Z_DEPTH, C_WHITE);
|
|
dddline(-X_WIDTH, Y_HEIGHT, -Z_DEPTH, -X_WIDTH, -Y_HEIGHT, -Z_DEPTH, C_WHITE);
|
|
dddline(X_WIDTH, Y_HEIGHT, -Z_DEPTH, X_WIDTH, -Y_HEIGHT, -Z_DEPTH, C_WHITE);
|
|
}
|
|
|
|
void draw_ball(const float ball_x, const float ball_y, const float ball_z)
|
|
{
|
|
int x, y, z;
|
|
|
|
x = (int) ball_x;
|
|
y = (int) ball_y;
|
|
z = (int) ball_z;
|
|
|
|
dddline(x + BALL_SIZE, y, z + BALL_SIZE, x - BALL_SIZE, y, z + BALL_SIZE, C_GREEN);
|
|
dddline(x + BALL_SIZE, y, z - BALL_SIZE, x - BALL_SIZE, y, z - BALL_SIZE, C_GREEN);
|
|
dddline(x + BALL_SIZE, y, z + BALL_SIZE, x + BALL_SIZE, y, z - BALL_SIZE, C_GREEN);
|
|
dddline(x - BALL_SIZE, y, z + BALL_SIZE, x - BALL_SIZE, y, z - BALL_SIZE, C_GREEN);
|
|
|
|
dddline(x + BALL_SIZE, y, z + BALL_SIZE, x, y + BALL_SIZE, z, C_GREEN);
|
|
dddline(x + BALL_SIZE, y, z - BALL_SIZE, x, y + BALL_SIZE, z, C_GREEN);
|
|
dddline(x - BALL_SIZE, y, z + BALL_SIZE, x, y + BALL_SIZE, z, C_GREEN);
|
|
dddline(x - BALL_SIZE, y, z - BALL_SIZE, x, y + BALL_SIZE, z, C_GREEN);
|
|
|
|
dddline(x + BALL_SIZE, y, z + BALL_SIZE, x, y - BALL_SIZE, z, C_GREEN);
|
|
dddline(x + BALL_SIZE, y, z - BALL_SIZE, x, y - BALL_SIZE, z, C_GREEN);
|
|
dddline(x - BALL_SIZE, y, z + BALL_SIZE, x, y - BALL_SIZE, z, C_GREEN);
|
|
dddline(x - BALL_SIZE, y, z - BALL_SIZE, x, y - BALL_SIZE, z, C_GREEN);
|
|
}
|
|
|
|
void draw_crap(const float ball_x, const float ball_y, const float ball_z)
|
|
{
|
|
int x, y, z;
|
|
|
|
x = (int) ball_x;
|
|
y = (int) ball_y;
|
|
z = (int) ball_z;
|
|
|
|
dddline(x + BALL_SIZE, Y_HEIGHT, z, x - BALL_SIZE, Y_HEIGHT, z, C_GREEN);
|
|
dddline(x + BALL_SIZE, -Y_HEIGHT, z, x - BALL_SIZE, -Y_HEIGHT, z, C_GREEN);
|
|
dddline(X_WIDTH, y + BALL_SIZE, z, X_WIDTH, y - BALL_SIZE, z, C_GREEN);
|
|
dddline(-X_WIDTH, y + BALL_SIZE, z, -X_WIDTH, y - BALL_SIZE, z, C_GREEN);
|
|
}
|
|
|
|
|
|
void draw_paddle(const int x, const int y, const int z, const int color)
|
|
{
|
|
dddline(x + PADDLE_WIDTH, y + PADDLE_HEIGHT, z, x - PADDLE_WIDTH, y + PADDLE_HEIGHT, z, color);
|
|
dddline(x + PADDLE_WIDTH, y - PADDLE_HEIGHT, z, x - PADDLE_WIDTH, y - PADDLE_HEIGHT, z, color);
|
|
dddline(x + PADDLE_WIDTH, y + PADDLE_HEIGHT, z, x + PADDLE_WIDTH, y - PADDLE_HEIGHT, z, color);
|
|
dddline(x - PADDLE_WIDTH, y + PADDLE_HEIGHT, z, x - PADDLE_WIDTH, y - PADDLE_HEIGHT, z, color);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
key_event_t ev;
|
|
bool run = true;
|
|
bool ball = false;
|
|
float ball_x = 0, ball_y = 0, ball_z = 0;
|
|
float ball_xm = 0, ball_ym = 0, ball_zm = 0;
|
|
int ball_to = 1;
|
|
int paddle_x = 0;
|
|
int paddle_y = 0;
|
|
int score = 0;
|
|
int enemy_x = 0;
|
|
int enemy_y = 0;
|
|
int enemy_score = 0;
|
|
int speed = 1;
|
|
|
|
dclear(C_BLACK);
|
|
draw_ball(0, 1, -2.3 * Z_DEPTH);
|
|
dtext(140, 75, C_WHITE, "3D PONG");
|
|
dupdate();
|
|
ev = getkey();
|
|
if(ev.key == KEY_EXIT ||ev.key ==KEY_MENU) run = false;
|
|
|
|
while(run)
|
|
{
|
|
dclear(C_BLACK);
|
|
dprint(0, 0, C_WHITE, "Player 1: %d", score);
|
|
dprint(0, 15, C_WHITE, "Player 2: %d", enemy_score);
|
|
draw_arena();
|
|
if(ball)
|
|
{
|
|
if(enemy_x > ball_x)
|
|
{
|
|
enemy_x-=speed;
|
|
if(enemy_x < -X_WIDTH + PADDLE_WIDTH) enemy_x = -X_WIDTH + PADDLE_WIDTH;
|
|
}
|
|
if(enemy_x < ball_x)
|
|
{
|
|
enemy_x+=speed;
|
|
if(enemy_x > X_WIDTH - PADDLE_WIDTH) enemy_x = X_WIDTH - PADDLE_WIDTH;
|
|
}
|
|
if(enemy_y > ball_y)
|
|
{
|
|
enemy_y-=speed;
|
|
if(enemy_y < -Y_HEIGHT + PADDLE_HEIGHT) enemy_y = -Y_HEIGHT + PADDLE_HEIGHT;
|
|
}
|
|
if(enemy_y < ball_y)
|
|
{
|
|
enemy_y+=speed;
|
|
if(enemy_y > Y_HEIGHT - PADDLE_HEIGHT) enemy_y = Y_HEIGHT - PADDLE_HEIGHT;
|
|
}
|
|
}else if(ball_to == 2){
|
|
ball = true;
|
|
ball_x = enemy_x;
|
|
ball_y = enemy_y;
|
|
ball_z = Z_DEPTH;
|
|
ball_xm = (float) enemy_x / ANGLE_DIVIDE;
|
|
ball_ym = (float) enemy_y / ANGLE_DIVIDE;
|
|
ball_zm = -3;
|
|
}
|
|
draw_paddle(enemy_x, enemy_y, Z_DEPTH, C_BLUE);
|
|
|
|
if(ball)
|
|
{
|
|
ball_x += ball_xm;
|
|
ball_y += ball_ym;
|
|
ball_z += ball_zm;
|
|
if(ball_x > X_WIDTH - BALL_SIZE)
|
|
{
|
|
ball_x = X_WIDTH - BALL_SIZE;
|
|
ball_xm *= -1;
|
|
}else if(ball_x < -X_WIDTH + BALL_SIZE)
|
|
{
|
|
ball_x = -X_WIDTH + BALL_SIZE;
|
|
ball_xm *= -1;
|
|
}
|
|
|
|
if(ball_y > Y_HEIGHT - BALL_SIZE)
|
|
{
|
|
ball_y = Y_HEIGHT - BALL_SIZE;
|
|
ball_ym *= -1;
|
|
}else if(ball_y < -Y_HEIGHT + BALL_SIZE)
|
|
{
|
|
ball_y = -Y_HEIGHT + BALL_SIZE;
|
|
ball_ym *= -1;
|
|
}
|
|
|
|
if(ball_z < -Z_DEPTH + BALL_SIZE)
|
|
{
|
|
if(ball_x + BALL_SIZE >= paddle_x - PADDLE_WIDTH &&
|
|
ball_x - BALL_SIZE <= paddle_x + PADDLE_WIDTH &&
|
|
ball_y + BALL_SIZE >= paddle_y - PADDLE_HEIGHT &&
|
|
ball_y - BALL_SIZE <= paddle_y + PADDLE_HEIGHT)
|
|
{
|
|
ball_z = -Z_DEPTH + BALL_SIZE;
|
|
ball_zm *= -1;
|
|
ball_xm = (-(float) paddle_x + ball_x)/ (float) ANGLE_DIVIDE;
|
|
ball_ym = (-(float) paddle_y + ball_y)/ (float) ANGLE_DIVIDE;
|
|
}else{
|
|
ball = false;
|
|
enemy_score++;
|
|
ball_to = 2;
|
|
}
|
|
}else if(ball_z > Z_DEPTH - BALL_SIZE)
|
|
{
|
|
if(ball_x + BALL_SIZE >= enemy_x - PADDLE_WIDTH &&
|
|
ball_x - BALL_SIZE <= enemy_x + PADDLE_WIDTH &&
|
|
ball_y + BALL_SIZE >= enemy_y - PADDLE_HEIGHT &&
|
|
ball_y - BALL_SIZE <= enemy_y + PADDLE_HEIGHT)
|
|
{
|
|
ball_z = Z_DEPTH - BALL_SIZE;
|
|
ball_zm *= -1;
|
|
ball_xm = (-(float) enemy_x + ball_x)/ (float) ANGLE_DIVIDE;
|
|
ball_ym = (-(float) enemy_y + ball_y)/ (float) ANGLE_DIVIDE;
|
|
}else{
|
|
ball = false;
|
|
score++;
|
|
ball_to = 1;
|
|
}
|
|
}
|
|
draw_ball(ball_x, ball_y, ball_z);
|
|
draw_crap(ball_x, ball_y, ball_z);
|
|
}
|
|
|
|
while((ev = pollevent()).type != KEYEV_NONE)
|
|
{
|
|
if(ev.type != KEYEV_DOWN) continue;
|
|
if(ev.key == KEY_EXIT)
|
|
{
|
|
run = 0;
|
|
break;
|
|
}else if(ev.key == KEY_SHIFT && !ball && ball_to == 1){
|
|
ball = true;
|
|
ball_x = paddle_x;
|
|
ball_y = paddle_y;
|
|
ball_z = -Z_DEPTH;
|
|
ball_xm = (float) paddle_x / ANGLE_DIVIDE;
|
|
ball_ym = (float) paddle_y / ANGLE_DIVIDE;
|
|
ball_zm = 3;
|
|
}
|
|
}
|
|
if(keydown(KEY_LEFT))
|
|
{
|
|
paddle_x-=speed;
|
|
if(paddle_x < -X_WIDTH + PADDLE_WIDTH) paddle_x = -X_WIDTH + PADDLE_WIDTH;
|
|
}
|
|
if(keydown(KEY_RIGHT))
|
|
{
|
|
paddle_x+=speed;
|
|
if(paddle_x > X_WIDTH - PADDLE_WIDTH) paddle_x = X_WIDTH - PADDLE_WIDTH;
|
|
}
|
|
if(keydown(KEY_UP))
|
|
{
|
|
paddle_y-=speed;
|
|
if(paddle_y < -Y_HEIGHT + PADDLE_HEIGHT) paddle_y = -Y_HEIGHT + PADDLE_HEIGHT;
|
|
}
|
|
if(keydown(KEY_DOWN))
|
|
{
|
|
paddle_y+=speed;
|
|
if(paddle_y > Y_HEIGHT - PADDLE_HEIGHT) paddle_y = Y_HEIGHT - PADDLE_HEIGHT;
|
|
}
|
|
|
|
draw_paddle(paddle_x, paddle_y, -Z_DEPTH, C_RED);
|
|
speed -= 1;
|
|
if(speed==0) speed = 2;
|
|
dupdate();
|
|
}
|
|
return 1;
|
|
}
|
|
|