貪吃蛇游戲 望采納
成都創(chuàng)新互聯(lián)主打移動(dòng)網(wǎng)站、成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)絡(luò)推廣、網(wǎng)站維護(hù)、申請(qǐng)域名、等互聯(lián)網(wǎng)信息服務(wù),為各行業(yè)提供服務(wù)。在技術(shù)實(shí)力的保障下,我們?yōu)榭蛻舫兄Z穩(wěn)定,放心的服務(wù),根據(jù)網(wǎng)站的內(nèi)容與功能再?zèng)Q定采用什么樣的設(shè)計(jì)。最后,要實(shí)現(xiàn)符合網(wǎng)站需求的內(nèi)容、功能與設(shè)計(jì),我們還會(huì)規(guī)劃穩(wěn)定安全的技術(shù)方案做保障。
import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Snake extends JFrame implements KeyListener{
int Count=0;
Button[][] grid = new Button[20][20];
ArrayListPoint snake_list=new ArrayListPoint();
Point bean=new Point(-1,-1);//保存隨機(jī)豆子【坐標(biāo)】
int Direction = 1; //方向標(biāo)志 1:上 2:下 3:左 4:右
//構(gòu)造方法
public Snake()
{
//窗體初始化
this.setBounds(400,300,390,395);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout f=new GridLayout(20,20);
this.getContentPane().setBackground(Color.gray);
this.setLayout(f);
//初始化20*20個(gè)按鈕
for(int i=0;i20;i++)
for(int j=0;j20;j++)
{
grid[i][j]=new Button();
this.add(grid[i][j]);
grid[i][j].setVisible(false);
grid[i][j].addKeyListener(this);
grid[i][j].setBackground(Color.blue);
}
//蛇體初始化
grid[10][10].setVisible(true);
grid[11][10].setVisible(true);
grid[12][10].setVisible(true);
grid[13][10].setVisible(true);
grid[14][10].setVisible(true);
//在動(dòng)態(tài)數(shù)組中保存蛇體按鈕坐標(biāo)【行列】信息
snake_list.add(new Point(10,10));
snake_list.add(new Point(11,10));
snake_list.add(new Point(12,10));
snake_list.add(new Point(13,10));
snake_list.add(new Point(14,10));
this.rand_bean();
this.setTitle("總分:0");
this.setVisible(true);
}
//該方法隨機(jī)一個(gè)豆子,且不在蛇體上,并使豆子可見(jiàn)
public void rand_bean(){
Random rd=new Random();
do{
bean.x=rd.nextInt(20);//行
bean.y=rd.nextInt(20);//列
}while(snake_list.contains(bean));
grid[bean.x][bean.y].setVisible(true);
grid[bean.x][bean.y].setBackground(Color.red);
}
//判斷擬增蛇頭是否與自身有碰撞
public boolean is_cross(Point p){
boolean Flag=false;
for(int i=0;isnake_list.size();i++){
if(p.equals(snake_list.get(i) )){
Flag=true;break;
}
}
return Flag;
}
//判斷蛇即將前進(jìn)位置是否有豆子,有返回true,無(wú)返回false
public boolean isHaveBean(){
boolean Flag=false;
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
Point p=null;
if(Direction==1)p=new Point(x-1,y);
if(Direction==2)p=new Point(x+1,y);
if(Direction==3)p=new Point(x,y-1);
if(Direction==4)p=new Point(x,y+1);
if(bean.equals(p))Flag=true;
return Flag;
}
//前進(jìn)一格
public void snake_move(){
if(isHaveBean()==true){//////////////有豆子吃
Point p=new Point(bean.x,bean.y);//【很重要,保證吃掉的是豆子的復(fù)制對(duì)象】
snake_list.add(0,p); //吃豆子
grid[p.x][p.y].setBackground(Color.blue);
this.Count++;
this.setTitle("總分:"+Count);
this.rand_bean(); //再產(chǎn)生一個(gè)豆子
}else{///////////////////無(wú)豆子吃
//取原蛇頭坐標(biāo)
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
//根據(jù)蛇頭坐標(biāo)推算出擬新增蛇頭坐標(biāo)
Point p=null;
if(Direction==1)p=new Point(x-1,y);//計(jì)算出向上的新坐標(biāo)
if(Direction==2)p=new Point(x+1,y);//計(jì)算出向下的新坐標(biāo)
if(Direction==3)p=new Point(x,y-1);//計(jì)算出向左的新坐標(biāo)
if(Direction==4)p=new Point(x,y+1);//計(jì)算出向右的新坐標(biāo)
//若擬新增蛇頭碰壁,或纏繞則游戲結(jié)束
if(p.x0||p.x19|| p.y0||p.y19||is_cross(p)==true){
JOptionPane.showMessageDialog(null, "游戲結(jié)束!");
System.exit(0);
}
//向蛇體增加新的蛇頭坐標(biāo),并使新蛇頭可見(jiàn)
snake_list.add(0,p);
grid[p.x][p.y].setVisible(true);
//刪除原蛇尾坐標(biāo),使蛇尾不可見(jiàn)
int x1=snake_list.get(snake_list.size()-1).x;
int y1=snake_list.get(snake_list.size()-1).y;
grid[x1][y1].setVisible(false);
snake_list.remove(snake_list.size()-1);
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP Direction!=2) Direction=1;
if(e.getKeyCode()==KeyEvent.VK_DOWN Direction!=1) Direction=2;
if(e.getKeyCode()==KeyEvent.VK_LEFT Direction!=4) Direction=3;
if(e.getKeyCode()==KeyEvent.VK_RIGHT Direction!=3) Direction=4;
}
@Override
public void keyReleased(KeyEvent e) { }
@Override
public void keyTyped(KeyEvent e) { }
public static void main(String[] args) throws InterruptedException {
Snake win=new Snake();
while(true){
win.snake_move();
Thread.sleep(300);
}
}
}
視具體代碼情況而定,視是否引用外部包而定,視個(gè)人編程書(shū)寫(xiě)習(xí)慣而定。
具體的來(lái)說(shuō),Python可以將任意長(zhǎng)的代碼寫(xiě)在一行上(其實(shí)好像java也可以這么干)。
所以行數(shù)說(shuō)明不了什么問(wèn)題。
平均來(lái)看,Java要打100行的代碼,Python大約需要50行代碼左右。
另外Python在某些問(wèn)題上,處理比Java要更消耗資源,不過(guò)Python用了很多多線程優(yōu)化,所以說(shuō)起來(lái),單機(jī)的運(yùn)行速度不相上下,但在服務(wù)器上運(yùn)行就能看出來(lái)Java是有明顯優(yōu)勢(shì)的。
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Painter extends JFrame{
/**
*
*/
private static final long serialVersionUID = 8160427604782702376L;
CanvasPanel canvas = new CanvasPanel();;
public Painter() {
super("Star");
this.add(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Painter();
}
}
class CanvasPanel extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -4642528854538741028L;
private JButton[] btn = new JButton[4];
private String[] btn_name = {"+", "-", "R", "L"};
private int center_x = 200, center_y = 200, radius = 100, degree = 0;
public CanvasPanel() {
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for(int i = 0; i 4; i++) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160 + i * 60, 425, 50, 50);
btn[i].addActionListener(this);
this.add(btn[i]);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i 5; i++) {
g.drawLine( (int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i))),
(int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i + 144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i + 144))));
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == "+") {
if(radius 200)
radius += 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius 0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "R") {
degree = (degree + 2) % 360;
repaint();
} else if(e.getActionCommand() == "L") {
degree = (degree - 2) % 360;
repaint();
}
}
}
網(wǎng)頁(yè)標(biāo)題:java一百行代碼開(kāi)發(fā) 第一行代碼JAVA
本文鏈接:http://www.rwnh.cn/article48/hiihep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、商城網(wǎng)站、網(wǎng)站策劃、定制網(wǎng)站、微信小程序、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)