Java蛇游戏 [英] Java snake game

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

问题描述

一开始,我必须承认那里有无数的蛇问题,尽管每个程序员,即使新手或专家以不同的方式编写他的代码,所以我还是决定再开一个案子,

Well starting off i must admit there are countless snake questions out there, though every Programmer even if newbie or expert writes his code differently so i decided to open another one case,

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class Game1 extends JFrame {

float x;
float y;
int[][] tileKati = new int[30][30];
int keyCode;
int body;
boolean Right = true;
boolean Left = false;
boolean Up = false;
boolean Down = false;
boolean Running = false;


public class AL extends KeyAdapter{

    public void keyPressed(KeyEvent e){

         keyCode = e.getKeyCode();

        if((keyCode == e.VK_LEFT) && (!Right)){
            Left=true;
            Down=false;
            Up=false;
            Running=true;
        }
        if((keyCode == e.VK_RIGHT) && (!Left)){
            Right=true;
            Down=false;
            Up=false;
            Running=true;
        }
        if((keyCode == e.VK_UP) && (!Down)){
            Up=true;
            Left=false;
            Right=false;
            Running=true;
        }
        if((keyCode == e.VK_DOWN) && (!Up)){
            Down=true;
            Left=false;
            Right=false;
            Running=true;
        }
        }

    public void keyReleased(KeyEvent e){

}
}
public Game1(){
    addKeyListener(new AL());
    setTitle("Snake");
    setSize(960,960);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    x=0;
    y=0;
    for(int x=0;x<30;x++){
        for(int y=0;y<30;y++){
        tileKati[x][y] = 0;
     }  

    }

}


public void paint(Graphics g)
{
    state();

    if(Running==true)
    {

    if(x<0)
    {

        x=30;
    }

    if(x>30)
    {
        x=0;    
    }

    if(y<0)
    {
        y=30;
    }

    if(y>30)
    {
        y=0;
    }

    }
    tileKati[(int)x][(int)y] = 1;
    for(int x=0;x<30;x++)
    {

        for(int y=0;y<30;y++)
        {

        if(tileKati[x][y] == 0)
        {
            g.setColor(Color.black);
            g.drawRect(x*32, y*32, 32, 32);
        }   
        if(tileKati[x][y]== 1)
        {
            g.setColor(Color.cyan);
            g.fillRect(x*32, y*32, 32, 32); 
        }

        }  

    }



    repaint();
}

public void move(){
    if(Right){
        x+= 0.01;

    }
    if(Left){
        x-=0.01;
    }
    if(Up){
        y-=0.01;

    }
    if(Down){
        y+=0.01;
    }}

public void state(){
    if(Running){
        move();


    }
    repaint();
}

public static void main(String[] args){
new Game1();

}
}

所以我的问题如下,当我的蛇超过3个图块(x或y)时,我想开始绘制我的图块并清除地图"的其余部分,这样我就可以使3个图块的蛇在地图上穿行那么我想我可以弄清楚其余的事了,谢谢.

So my question is as follows, i want to start painting my tiles and clearing the rest of the "map" when my snake exceed 3 tiles (x or y), so i can make a 3 tile snake running through the map then i think i can figure out the rest, thanks in advance.

不再有破损的代码.

推荐答案

为此,您可以使用StackLinkedList.现在,随着蛇头的一举一动,您将其位置(x,y)添加到列表的开头,如果列表中还有更多元素作为蛇的长度,则删除最后一个元素.

You could use a Stack or LinkedList for this. Now with every move of the snakes head you add its position (x,y) to the start of the list and remove the last element if there are more elements in the list as the snake length.

在绘画时,您首先要绘画背景,然后再列出蛇的元素.

And while painting you first paint the background and then the lists elements a the snake.

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

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