This is my first post, I just started coding with java, and I'd like to try to make tetris on it. My java skill are quite limited, and I was wondering how to make my pieces move while they fall down.
This is what i coded so far, I know it's a little bit clunky, but be gentle pls, I just started! And sorry for my broken English, I'm still learning that as well.
import java.util.Scanner;
public class Tetris{
public static void main(String[] args){
int[][] matrix = new int[20][10];
boolean err=false,newGame = true;
String sino = "";//sino are yes and no in italian :) (si = yes) (no = no ofc)
Scanner t = new Scanner(System.in);
printMatrix(matrix);
do{
game(matrix);
do{
err=false;
sino="";
sino = t.nextLine();
if(sino.equalsIgnoreCase("no"))
newGame=false;
else{
if(!sino.equalsIgnoreCase("si")){
err=true;
System.out.println("errore");
}
}
}while(err);
}while(newGame);
}
public static void game(int[][] matrix){
}
Here I will first casually choose the piece, and then make it fall down from the top, and have the possibility to make it rotate and/or move left and right.
I still have to code all the single pieces, which will be casually chosen everytime a piece falls down.
public static void printMatrix(int[][] matrix){
for(int i=0;i<20;i++){
System.out.print("| ");
for(int j=0;j<10;j++){
if(matrix[i][j]==0)
System.out.print(" ");
else{
if(matrix[i][j]==1)
System.out.print("X ");
else
System.out.print("O ");
}
}
System.out.println("|");
}
System.out.println(" - - - - - - - - - - ");
}
public static int[][] newMatrix(int[][] matrix){
for(int i=0;i<20;i++)
for(int j=0;j<10;j++)
matrix[i][j]= 0;
return matrix;
}
}
I know this code is not complete at all, I was just wondering how to have an imput from my keyboard with the arrow keys to make the piece move while it falls, and to make it rotate (maybe with the R key).