Java 在我正在创建的蛇游戏中,如何增加为创建蛇而绘制的圆圈之间的间距?
我在java上创建了一个蛇游戏,每当蛇吃东西的时候,我就设法让它长大。但是我画的椭圆的间距太近了,我想增加它们之间的间距,我应该这样做吗 我尝试更改在pts方法中向ArrayList添加值的位置的值Java 在我正在创建的蛇游戏中,如何增加为创建蛇而绘制的圆圈之间的间距?,java,arraylist,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 jav
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)
);
}
}
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.color.ColorSpace;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.KeyEvent;
导入java.awt.event.KeyListener;
导入java.awt.image.buffereImage;
导入java.util.ArrayList;
导入java.util.Random;
导入javax.swing.AbstractAction;
导入javax.swing.JComponent;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.KeyStroke;
导入javax.swing.Timer;
公共类GUI扩展JPanel实现ActionListener{
//创建变量
定时器t;
静态int x=60;
静态int y=20;
静态智力得分=0;
布尔movexr;
布尔型movexl;
布尔moveyd;
布尔型moveyu;
布尔fa;
布尔值lv1=true,lv2=false;
静态int rx=(int)(Math.random()*650+10);
静态int-ry=(int)(Math.random()*520+10);
Snake lvv1=新Snake();
//变量结束//
公共图形用户界面(){
初始化组件();
t=新定时器(30,此);
t、 start();
lvv1.xs.add(x);
lvv1.ys.add(y);
}
公共组件(图形g){
超级组件(g);
//你所有的画都放在这里
如果(lv1==true){
lvl1(g);
如果(分数>=10){
lv2=真;
}
}
if(lv2==true){
lv1=假;
lvl2(g);
}
lvv1.me(g);
g、 clearRect(x,y,20,20);
////你的画结束了
}
公共图书馆临时秘书处(图g){
g、 圆角(rx,ry,10,10);
if(lvv1.xs.get)(0)