Cześć. Ostatnio programuję trochę więcej niż kiedykolwiek. Napisałem prostą podstawę(nawet jeszcze nie gotowa) dla gry snake 3d i chciałbym żeby mój kod wyglądał trochę bardziej profesjonalnie, przejrzyściej i był trochę bardziej ogólny, jeśli wiecie o co mi chodzi. Tak właściwie to moja pierwsza zabawa z klasami i dzieleniem kodu na pliki, wcześniej robiłem tylko małe programiki które tego nie wymagały.Nie proszę tutaj o poprawianie mojego kodu, proszę jedynie o tipsy i nawyki potrzebne do osiągnięcia czystego kodu. Oto i on:
Główna klasa:
package snakeTheGame;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.util.glu.GLU;
public class theGame {
final int width = 800;
final int height = 600;
final float cameraAngle = 35;
long lastFrame;
int fps;
long lastFPS;
Snake snake;
Grid grid;
public void start() {
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
snake = new Snake();
grid = new Grid(20, 20, 0, 0, 4f, 1, 0);
initGL();
getDelta();
lastFPS = getTime();
while (!Display.isCloseRequested()) {
int delta = getDelta();
update(delta);
renderGL();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public void poll(){
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT) || Keyboard.isKeyDown(Keyboard.KEY_D)){
grid.initRotationRight();
}
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT) || Keyboard.isKeyDown(Keyboard.KEY_A)){
grid.initRotationLeft();
}
}
public void update(int delta) {
grid.rotate();
grid.move();
snake.rotateHead(delta);
poll();
updateFPS();
}
public int getDelta() {
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
public long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void updateFPS() {
if (getTime() - lastFPS > 1000) {
Display.setTitle("FPS: " + fps);
fps = 0;
lastFPS += 1000;
}
fps++;
}
public void initGL() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0f, (float)width / (float)height, 0.1f, 200.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
}
public void renderGL() {
grid.renderGrid(cameraAngle);
snake.renderSnake(cameraAngle, grid.getSquareSize());
}
public static void main(String[] argv) {
theGame game = new theGame();
game.start();
}
}
package snakeTheGame;
import java.util.LinkedList;
import org.lwjgl.opengl.GL11;
public class Snake {
private final float headRadius = 0.85f;
private final float[] headXCoords = new float[5];
private final float[] headYCoords = new float[5];
private final float rotationSpeed = 0.35f;
private float rotation = 0;
//private LinkedList snake = new LinkedList();
public Snake(){
getPentagonCoords(headRadius);
}
private void getPentagonCoords(float radius){
float theta = 2 * (float)Math.PI / 5;
for (int i = 0; i < 5;i++){
headXCoords[i] = (float) (Math.cos(theta * (i + 1)) * radius);
headYCoords[i] = (float) (Math.sin(theta * (i + 1)) * radius);
}
}
public void rotateHead(int delta){
rotation += rotationSpeed * delta;
if (rotation > 360)
rotation -= 360;
}
public void renderSnake(float cameraAngle, float squareSize){
//http://web.njit.edu/~kevin/rgb.txt.html
GL11.glLoadIdentity();
//setup the camera
GL11.glTranslatef(-squareSize/2, -squareSize/2, -20);
GL11.glRotatef(cameraAngle, 1, 0, 0);
//passive rotation
GL11.glTranslatef(squareSize/2, 0, -1.5f);
GL11.glRotatef(rotation, 0f, 0f, 1f);
GL11.glTranslatef(0f, 0f, -1.5f);
GL11.glBegin(GL11.GL_TRIANGLES);
GL11.glColor3f(0f,0.13333333f,0.4f);//royal blue 5
GL11.glVertex3f(headXCoords[0], headYCoords[0], -0.5f);
GL11.glVertex3f(headXCoords[1], headYCoords[1], -0.5f);
GL11.glVertex3f(0, 0, -3.5f);
GL11.glColor3f(0.152941f,0.250980f,0.5450980f);//royal blue 4
GL11.glVertex3f(headXCoords[1], headYCoords[1], -0.5f);
GL11.glVertex3f(headXCoords[2], headYCoords[2], -0.5f);
GL11.glVertex3f(0, 0, -3.5f);
GL11.glColor3f(0.22745098f,0.3725490196f,0.80392156863f);//royal blue 3
GL11.glVertex3f(headXCoords[2], headYCoords[2], -0.5f);
GL11.glVertex3f(headXCoords[3], headYCoords[3], -0.5f);
GL11.glVertex3f(0, 0, -3.5f);
GL11.glColor3f(0.282352941f,0.462745098f,1f);//royal blue 1
GL11.glVertex3f(headXCoords[3], headYCoords[3], -0.5f);
GL11.glVertex3f(headXCoords[4], headYCoords[4], -0.5f);
GL11.glVertex3f(0, 0, -3.5f);
GL11.glColor3f(0.25490196f,0.4117647f,1f);//royal blue
GL11.glVertex3f(headXCoords[4], headYCoords[4], -0.5f);
GL11.glVertex3f(headXCoords[0], headYCoords[0], -0.5f);
GL11.glVertex3f(0, 0, -3.5f);
GL11.glEnd();
GL11.glColor3f(0f, 0f, 1.0f); //blue
GL11.glBegin(GL11.GL_POLYGON);
GL11.glVertex3f(headXCoords[0], headYCoords[0],-0.5f);
GL11.glVertex3f(headXCoords[1], headYCoords[1],-0.5f);
GL11.glVertex3f(headXCoords[2], headYCoords[2],-0.5f);
GL11.glVertex3f(headXCoords[3], headYCoords[3],-0.5f);
GL11.glVertex3f(headXCoords[4], headYCoords[4],-0.5f);
GL11.glEnd();
}
}
package snakeTheGame;
import org.lwjgl.opengl.GL11;
public class Grid {
private float squareX, squareZ;
private int gridSizeX, gridSizeZ;
private final float squareSize;
private final float squareBorder = 0.15f;
private float gridRotation = 0;
private final float gridRotationSpeed = 7.5f;
private float prevRotation;
private boolean rotateLeft = false, rotateRight = false;
private circularList zSpeed, xSpeed;
private final float movementSpeed = 0.0625f;
private class circularList{
private int[] values;
private int currentValue;
public circularList(int[] values, int startValue){
this.values = values;
this.currentValue = startValue;
}
public circularList(int[] values){
this.values = values;
this.currentValue = 0;
}
public int currentValue(){
return values[currentValue];
}
public void shiftRight(){
if (currentValue == values.length - 1)
currentValue = 0;
else
currentValue++;
}
public void shiftLeft(){
if (currentValue == 0)
currentValue = values.length - 1;
else
currentValue--;
}
}
private void initSpeedLists(int zDir, int xDir){
int[] array = {-1, 0, 1, 0};
zSpeed = new circularList(array, zDir);
xSpeed = new circularList(array, xDir);
}
public void move(){
//temporary 1 point
if (!((rotateRight || rotateLeft) && isCentered())){
if ((squareX > 0 && xSpeed.currentValue() == -1) || (squareX < gridSizeX - 1 && xSpeed.currentValue() == 1))
squareX += xSpeed.currentValue() * movementSpeed;
if ((squareZ < 0 && zSpeed.currentValue() == 1) || (squareZ > -gridSizeZ + 1 && zSpeed.currentValue() == -1))
squareZ += zSpeed.currentValue() * movementSpeed;
}
}
public Grid(int gridSizeX, int gridSizeZ, int spawnX, int spawnZ, float squareSize, int xDir, int zDir){
this.gridSizeX = gridSizeX;
this.gridSizeZ = gridSizeZ;
this.squareX = (float) spawnX;
this.squareZ = (float) spawnZ;
this.squareSize = squareSize;
initSpeedLists(zDir, xDir);
}
public Grid (){
this.gridSizeX = 20;
this.gridSizeZ = 20;
this.squareX = 0.0f;
this.squareZ = 0.0f;
this.squareSize = 4f;
initSpeedLists(0, 1);
}
public void renderGrid(float cameraAngle){
GL11.glLoadIdentity();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(0f, 0f, 0f, 0f);
//set up the camera
GL11.glTranslatef(0, 0, -20);
GL11.glRotatef(cameraAngle, 1f, 0f, 0f);
//rotate the grid
GL11.glRotated(gridRotation, 0, 1f, 0);
GL11.glTranslatef(-(squareX * squareSize + (squareSize/2)), 0,- (squareZ * squareSize - (squareSize/2)));
//draw the grid
GL11.glColor3f(0.5f, 0f, 0.5f);
GL11.glBegin(GL11.GL_QUADS);
for(int j = 0; j<gridSizeZ;j++){
for(int i = 0; i<gridSizeX;i++){
GL11.glVertex3f(i * squareSize + squareBorder,0,-j * squareSize - squareBorder);
GL11.glVertex3f(i * squareSize + squareBorder,0,-(j+1) * squareSize + squareBorder);
GL11.glVertex3f((i+1) * squareSize - squareBorder,0,-(j+1) * squareSize + squareBorder);
GL11.glVertex3f((i+1) * squareSize - squareBorder,0,-j * squareSize - squareBorder);
}
}
GL11.glEnd();
}
public void rotate(){
if (isCentered()){
if (rotateRight)
if (gridRotation != prevRotation + 90)
gridRotation += gridRotationSpeed;
else{
rotateRight = false;
zSpeed.shiftLeft();
xSpeed.shiftRight();
if (gridRotation == 360)
gridRotation = 0;
}
if (rotateLeft)
if (gridRotation != prevRotation - 90)
gridRotation -= gridRotationSpeed;
else{
rotateLeft = false;
zSpeed.shiftRight();
xSpeed.shiftLeft();
if (gridRotation == -360)
gridRotation = 0;
}
}
}
public void initRotationLeft(){
if (!rotateLeft && !rotateRight){
prevRotation = gridRotation;
rotateLeft = true;
}
}
public void initRotationRight() {
if (!rotateLeft && !rotateRight){
prevRotation = gridRotation;
rotateRight = true;
}
}
private boolean isCentered(){
return (squareX % 1 == 0 && squareZ % 1 == 0);
}
public float getSquareX(){
return squareX;
}
public float getSquareZ(){
return squareZ;
}
public float getSquareSize(){
return squareSize;
}
}