如何在我正在创建的蛇游戏中增加为绘制蛇而绘制的圆圈之间的空间?

时间:2019-05-19 17:04:30

标签: java arraylist

我在Java上创建了一个蛇游戏,每当它吃东西时,我就设法使它长出来。但是然后我绘制的椭圆形的空间太近了,我想增加它们之间的间距吗?

我尝试在pts方法中将值添加到ArrayList的地方更改值。

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import java.util.Random;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class GUI extends JPanel implements ActionListener  {

//Create variables
Timer t;
static int x= 60;
static int y= 20;
static int score = 0;
boolean movexr;
boolean movexl;
boolean moveyd;
boolean moveyu;
boolean fa;
boolean lv1 = true,lv2= false;
static int rx= (int) (Math.random()*650+10);
static int ry= (int) (Math.random()*520+10);
Snake lvv1 = new Snake();

//end of variables//


public GUI() {
    initComponents();
    t = new Timer(30,this);
    t.start();
    lvv1.xs.add(x);
    lvv1.ys.add(y);



} 

public void paintComponent(Graphics g){
    super.paintComponent(g);
    //all your drawing will go in here
    if(lv1==true) {
        lvl1(g);
        if(score >=10) {
            lv2=true;
        }
    }
    if(lv2==true) {
        lv1=false;
        lvl2(g);
    }
    lvv1.me(g);
    g.clearRect(x, y, 20, 20);


    ////end of your drawing
} 

public  void pts(Graphics g) {
    g.fillOval(rx, ry, 10, 10);
    if(lvv1.xs.get(0)<rx+10&&lvv1.ys.get(0)<ry+10&&rx<lvv1.xs.get(0)+20&&ry<lvv1.ys.get(0)+20) {
        rx= (int) (Math.random()*650+10);
        ry= (int) (Math.random()*520+10);
        score+=1;
        lvv1.xs.add((lvv1.xs.size()-1)+20);
        lvv1.ys.add((lvv1.ys.size()-1)+20);

    }
    for(int i=lvv1.xs.size()-1; i>0; i--) {
        lvv1.xs.set(i, lvv1.xs.get(i-1));
        lvv1.ys.set(i, lvv1.ys.get(i-1));
    }
}

public  void fail(Graphics g) {
    //g.setColor(Color.white);
    g.fillRect(10, 10, 700, 550);
}

public void lvl1(Graphics g) {
    pts(g);
    if (lvv1.xs.get(0)>700) {
        lvv1.xs.set(0, -10);
    }
    if (lvv1.xs.get(0)<-10) {
        lvv1.xs.set(0,700);
    }
    if (lvv1.ys.get(0)>560) {
        lvv1.ys.set(0,-10);
    }
    if (lvv1.ys.get(0)<-10) {
        lvv1.ys.set(0,560);
    }
}

public void lvl2(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 700, 550);
    g.setColor(Color.black);
    g.fillRect(0, 0, 10, 550);
    g.fillRect(0, 0, 700, 10);
    g.fillRect(700, 0, 10, 560);
    g.fillRect(0, 550, 700, 10);
    pts(g);
    if(lvv1.xs.get(0)<=10||lvv1.xs.get(0)>=680||lvv1.ys.get(0)<=0||lvv1.ys.get(0)>530) {
        fa=true;
    }
    if(fa==true) {
        fail(g);
        movexl=false;
        moveyu=false;
        movexr=false;
        moveyd=false;
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    if (movexr == true) {
        lvv1.xs.set(0,lvv1.xs.get(0)+4);
        movexl=false;
    }
    if (moveyd== true) {
        lvv1.ys.set(0,lvv1.ys.get(0)+4);
        moveyu=false;
    }
    if(moveyu==true) {
        lvv1.ys.set(0,lvv1.ys.get(0)-4);
        moveyd=false;
    }
    if(movexl==true) {
        lvv1.xs.set(0,lvv1.xs.get(0)-4);
        movexr=false;
    }


    // x < x2 + w2 && y < y2 + h2 && x2 < x + w && y2 < y + h      //the rule

    repaint();
}

public void keyPressed(KeyEvent e) {

System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode()));
    int key=e.getKeyCode();
    if(key==KeyEvent.VK_A||key==KeyEvent.VK_LEFT) {
        movexl=true;
        moveyu=false;
        moveyd=false;
    }
    if(key==KeyEvent.VK_W||key==KeyEvent.VK_UP) {
        moveyu=true;
        movexl=false;
        movexr=false;

    }
    if(key==KeyEvent.VK_D||key==KeyEvent.VK_RIGHT) {
        movexr=true;
        moveyu=false;
        moveyd=false;
    }
    if(key==KeyEvent.VK_S||key==KeyEvent.VK_DOWN) {
        moveyd=true;
        movexl=false;
        movexr=false;
    }
    if(key==KeyEvent.VK_ENTER) {
        //reset();
    }

}
public void reset() {
    y= 20;
    score = 0;
    movexr=false;
    movexl=false;
    moveyd=false;
    moveyu=false;
    fa=false;
    lv1 = true;
    lv2= false;
    rx= (int) (Math.random()*650+10);
    ry= (int) (Math.random()*520+10);
    lvv1=new Snake();
}
public void keyReleased(KeyEvent e) {

}

private void initComponents() {
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            );
    layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            );

    }
}

当蛇长大时,椭圆形彼此重叠,我希望椭圆形靠近但不能重叠

0 个答案:

没有答案