使用Java创建Snake [英] Creating Snake using Java

查看:181
本文介绍了使用Java创建Snake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定使用Java重新创建Snake,但我有点陷入困境。目前,我有一个方块,用户可以使用箭头键在屏幕上移动。当您按下LEFT一次时,方块开始使用计时器向左移动。您不需要按住键或继续按下它;当您按下任何其他设置的键(右,上,下)时,它会改变方向。我的目标是使用ArrayList来保存构成蛇的方块。目前,我创建了一个只包含一个Snake对象的ArrayList,如果我将第二个Snake对象添加到列表中并将其添加到框架(第一个),则只能看到一个Snake对象以及要移动的键它不起作用。我正在寻找一些关于如何在这个项目上取得进展的想法 - 请不要给我完整的答案,因为我想自己弄清楚它的大部分内容;我只需要一些方向。在此先感谢 - 代码如下。

I decided to re-create Snake using Java, but I'm sort of stuck. At the moment, I have a square that the user can move around the screen using the arrow keys. When you press LEFT once, the square begins to move left using a timer.. You don't need to hold down the key or keep pressing it; it changes direction when you press any of the other keys that are set(Right, Up, Down). My goal is to use an ArrayList to hold the squares that make up the snake. At the moment, I've created an ArrayList with just one Snake object inside, if I add a second Snake object to the list and add it to the frame(with the first), only one Snake object is visible and the keys to move it don't function. I'm looking for some ideas as to how I can progress with this project - please don't give me the full answer, because I'd like to figure out most of it on my own; I just need some direction. Thanks in advance - code is below.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Snake extends JPanel implements KeyListener, ActionListener
{
int x = 0, y = 0, velx = 0, vely = 0;
Timer t = new Timer(250, this);


public Snake(int num1, int num2)
{
    t.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(true);
    x = num1;
    y = num2;
}
public void paintComponent(Graphics g)
{   
    super.paintComponent(g);

    g.setColor(Color.blue);
    g.fillRect(x, y, 20, 20);
}
public void actionPerformed(ActionEvent e)
{
    repaint();
    x += velx;
    y += vely;
}
public void up()
{
    vely = -20;
    velx = 0;
}
public void down()
{
    vely = 20;
    velx = 0;
}
public void left()
{
    vely = 0;
    velx = -20;
}
public void right()
{
    vely = 0;
    velx = 20;
}
public void keyPressed(KeyEvent e)
{
    int code = e.getKeyCode();

    if(code == KeyEvent.VK_UP)
        up();
    else if(code == KeyEvent.VK_DOWN)
        down();
    else if(code == KeyEvent.VK_RIGHT)
        right();
    else if(code == KeyEvent.VK_LEFT)
        left();

}
public void keyReleased(KeyEvent e)
{

}
public void keyTyped(KeyEvent e)
{

}
}
//Driver Class
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class UserClass
{
private static JFrame frame = new JFrame("Snake");
private static ArrayList<Snake> mySnake = new ArrayList<Snake>();

public static void main(String[] args)
{
    frame.setSize(500,500);
    frame.setBackground(Color.black);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    mySnake.add(new Snake(20,20));
    frame.add(mySnake.get(0));
}
}

PS当我在Eclipse中放入相同的精确代码时我的Mac,我的框架的背景是黑色的,但在Windows上它是浅灰色的......任何人都知道为什么?谢谢。

P.S When I put this same exact code in Eclipse on my Mac, the background of my frame is black, but on Windows it's light gray... Anyone know why? Thanks.

推荐答案

要回答你的后记,请设置背景颜色或用其他颜色制作JPanel。

To answer your postscript, set a background color or make a JPanel behind everything else with a painted color.

要进一步完成项目,请考虑将其作为MVC框架中的练习。现在发生的事情是你的模型和视图被链接在同一个类中 - 这使得很难遵循一切背后的逻辑。

To progress with the project, consider making it an exercise in the MVC framework. What is happening now is that your Model and View are linked in the same class - this makes it hard to follow the logic behind everything.

你能做的就是先分开你的Snake与视图有关 - 使用 ArrayList< Integer []>制作一条蛇segmentLocations 或其他东西,用于表示您定义的板上每个段位置的(x,y) - 您可以使用绝对坐标,也可以创建任意网格并更改为绝对坐标你的观点(这更好地代表了MVC关系)。 Snake还应该有一个蛇面朝向的方向 - 我会使用枚举方向{N,S,E,W} ,但你有选择,因为你也可以有一个表示方向的整数,或者其他各种方式。

What you can do is first separate your Snake from anything to do with the view - make a snake with an ArrayList<Integer[]> segmentLocations or something, to represent the (x,y) of each of the segment locations on a board that you define - you can use absolute coordinates or you can make an arbitrary grid and change to absolute coordinates in your View (this typifies the MVC relationship better). Snake should also have a field for the direction the snake is facing - I would use an enum Direction {N, S, E, W}, but you have options on that, as you could also have an integer representing direction, or a variety of other ways.

你的Snake也有办法更新自己 - move(),改变位置所有细分的基于初始细分的当前方向,并导致所有其他细分跟随之前的细分移动(如果你考虑它几分钟,这很容易)。

Your Snake would then also have ways to update itself - move(), shifting the location of all of the segments based on the current direction for the initial segment and causing all of the other segments to follow the movement of the one before it (this is pretty easy if you consider it for a couple of minutes).

您的视图可能是一个带有GridLayout的JFrame,其中包含JPanels,用于轮询您的Snake实例并查看该位置是否存在某个段,如果是,则绘制它或其他众多选项。

Your view could be a JFrame with a GridLayout consisting of JPanels which poll your Snake instance and see if there is a segment in the location and if so, draw it, or a multitude of other options.

您的控制器将是KeyAdapter,当按下箭头键时,它会向Snake发送更新信息。

Your controller would be the KeyAdapter that sends the updates in direction to your Snake when the arrow keys are pressed.

小提示,让您的生活更轻松:添加时一个新的细分,只需将它放在蛇的最后一段。下次移动时,新段将位于同一位置,其余的Snake应相应移动。

Small hint, to make your life easier: when you add a new segment, just have it at the location of the last segment of the Snake. The next time it moves, the new segment will be in the same location, and the rest of the Snake should have move accordingly.

这篇关于使用Java创建Snake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆