here is what I have…moving down is ok, but moving up is jerky…could someone help me with this code?
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class F extends JFrame {
private boolean[] controls = new boolean[5];
private int[][] grid = new int[7][13];
private Color[] colors={Color.yellow,Color.blue,Color.green,Color.red};
int ic=0;
boolean movingDown;
boolean movingUp;
boolean movingLeft;
boolean movingRight;
int facing;
// center position of the grid (screen x, screen y)
//int cx,cy;
// player position in the grid cy range:0..6 cx range:0..12
int gx,gy;
//
private int x;
private int y;
private double xvel=0.1,yvel=-0.3,gravity=0.0019;
private F() {
super("F");
setSize(800, 600);
setResizable(false);
show();
enableEvents(56);
createBufferStrategy(2);
BufferStrategy strategy = getBufferStrategy();
init();
long lastLoopTime = System.currentTimeMillis();
while (true) {
int delta = (int) (System.currentTimeMillis() - lastLoopTime);
logic(delta);
lastLoopTime = System.currentTimeMillis();
draw((Graphics2D) strategy.getDrawGraphics());
strategy.show();
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void init() {
restartGame();
facing=0;
}
private void restartGame() {
gx=6;
gy=0;
x = 100 + (gx * 50) - 25;
y = 120 + (gy * 60) - 45;
initLevel();
}
private void initLevel() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 13; j++) {
grid[i][j] = (j >= 6 - i && (j <= 2 * i + 6 - i) ? 1 : 0);
if (i != 0
&& j >= 6 - i
&& (j <= 2 * i + 6 - i)
&& ((j % 2 == 0 && i % 2 != 0) || (j % 2 != 0 && i % 2 == 0))) {
grid[i][j] = -1;
}
}
}
}
private void logic(int delta) {
if(controls[3] && !movingDown && !movingUp && !movingLeft && !movingRight) {
movingDown=true;
if(facing==0 || facing==1) {
gx--;
gy++;
xvel=-0.1;
yvel=-0.3;
facing=0;
}
gravity=0.0019;
} else if(controls[2] && !movingUp && !movingDown && !movingLeft && !movingRight) {
movingUp=true;
if(facing==0 || facing==1) {
gx++;
gy--;
xvel=-0.1;
yvel=0.3;
}
gravity=-0.0019;//0.00059;
} else if(controls[0] && !movingLeft && !movingDown && !movingUp && !movingRight) {
movingLeft=true;
if(facing==1 || facing==0) {
gx--;
gy--;
xvel=0.1;
yvel=0.3;
}
gravity=-0.0019;//0.00059;
} else if(controls[1] && !movingRight && !movingDown && !movingUp && !movingLeft) {
movingRight=true;
if(facing==0 || facing==1) {
xvel=0.1;
yvel=-0.3;
gx++;
gy++;
facing=1;
}
gravity=0.0019;
}
if(movingUp || movingLeft) {
x-=xvel*delta;
y-=yvel*delta;
yvel-=gravity*delta;
if(y<120 + (gy * 60) - 45){
y=120 + (gy * 60) - 45;
x=100 + (gx * 50) - 25;
movingUp=false;
movingLeft=false;
grid[gy][gx]++;
}
} else if(movingRight || movingDown) {
x+=xvel*delta;
y+=yvel*delta;
yvel+=gravity*delta;
if(y>120 + (gy * 60) -45){
y=120 + (gy * 60) - 45;
x=100 + (gx * 50) - 25;
movingRight=false;
movingDown=false;
grid[gy][gx]++;
}
}
}
private void draw(Graphics2D g) {
g.setColor(Color.black);
g.fillRect(0, 0, 800, 600);
this.renderField(g);
this.drawFuzzer(g);
g.setColor(Color.red);
for(int i=50; i < 800; i+=50) {
g.drawLine(i,120,i,560);
}
for(int i=120; i < 600; i+=20) {
g.drawLine(50,i,750,i);
}
g.setColor(Color.white);
g.drawLine(50,135,750,135);
}
private void drawFuzzer(Graphics2D g) {
g.setColor(Color.blue);
g.fillOval(x,y,50,65);
g.setColor(Color.gray);
g.fillOval(x,y,2,2);
g.fillOval(x,y+30,2,2);
}
private void renderField(Graphics2D g) {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 13; j++) {
if (grid[i][j] > 0) {
drawCube(g, i, j);
}
}
}
}
private void drawCube(Graphics2D g, int i, int j) {
int x = 100 + (j * 50);
int y = 120 + (i * 60);
g.setColor(colors[0]);
int[] x3 = { x, x-50, x, x+50, x };
int[] y3 = { y+40, y+20, y, y+20, y+40 };
g.fillPolygon(x3, y3, 5);
g.setColor(Color.green);
g.fillOval(x-3,y+20-3,6,6);
g.setColor(Color.white);
int[] x1 = { x, x, x-50, x-50, x };
int[] y1 = { y+40, y+80, y+60, y+20, y+40 };
g.fillPolygon(x1, y1, 5);
g.setColor(Color.lightGray);
int[] x2 = { x, x, x+50, x+50, x };
int[] y2 = { y+40, y+80, y+60, y+20, y+40 };
g.fillPolygon(x2, y2, 5);
}
protected void processKeyEvent(KeyEvent e) {
int[] keys = new int[] { KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,
KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_SPACE };
for (int i = 0; i < keys.length; i++) {
if (e.getKeyCode() == keys[i]) {
controls[i] = e.getID() == KeyEvent.KEY_PRESSED;
}
}
if (e.getKeyCode() == 27) {
System.exit(0);
}
}
public static void main(String argv[]) {
new F();
}
}