⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java游戏.java.txt

📁 是一个java的小球游戏 是一个java的小球游戏 是一个java的小球游戏
💻 TXT
字号:

import java.awt.*;
import java.awt.event.*;

class Board{
	int xb,yb,w,h;
	
	
	public Board(int x,int y){
		xb=x;
		yb=y;
		w=100;
		h=10;
		}
	public void leftmove(int move){
		xb=xb-move;
		if(xb<=0)
			xb = 0;
		}
	public void rightmove(int move){
		xb=xb+move;
		if(xb>=200)
			xb = 200;
		}
	}
	
class BallClass{
	int xb,yb;
	int vx,vy;
	boolean state = true;
	final int W = 5,H = 5;
	Color color;
	
	public BallClass(int x,int y){
		xb = x;
		yb = y;
		vx = -(int)(Math.random()*10 + 5);
		vy = -(int)(Math.random()*10 + 5);
		color = new Color((int)(Math.random()*255),
					(int)(Math.random()*255),(int)(Math.random()*255));
		}
	}
	
public class Ball extends Panel implements Runnable,KeyListener{
	boolean control = true;
	BallClass[] b;
	Board bd;
	static Frame frm;
	static Thread newThread;
	int time,cont = 1;
	int add,x,y,over = 1;
	int mv = 30;
	
	public Ball(){
		time = 50;
		b = new BallClass[1];
		for(int i=0;i<1;i++){
			b[i] = new BallClass(100,10);			
			}
			
		bd = new Board(100,300);
		this.addKeyListener(this);
		newThread = new Thread(this);
		newThread.start();
		}

		

		
	public void paint(Graphics g){
		this.requestFocus();
	
		for(int i=0;i<cont;i++){
			g.setColor(b[i].color);
			g.fillOval(b[i].xb,b[i].yb,20,20);
			
			}
		if(over == 0){
			Font f = new Font("Courier",Font.BOLD,28);
			g.setFont(f);
			g.setColor(Color.orange);
			g.drawString("GAME OVER",40,150);
			newThread = null;	
			control = false;	
			System.gc();
			}	
		
		g.fillRect(bd.xb,bd.yb,bd.w,bd.h);
		}
		
	public static void main(String[] args){
		frm = new Frame();
		Ball bl = new Ball();
		bl.setBackground(Color.black);
		bl.setSize(300,400);
	
		frm.setSize(300,400);
		frm.setLocation(300,100);
		//添加关闭窗口的适配器
		frm.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
			
				frm.dispose();
				}
			});
		frm.add(bl);
		frm.setVisible(true);
		}

//线程启动后执行
	public void run(){
		while(control){
			repaint();
		
			for(int i=0;i<cont;i++){
				if(b[i].xb > 280||b[i].xb < 0){
					b[i].vx = -b[i].vx;
					}
				if(b[i].yb < 0){
					b[i].vy = -b[i].vy;
					}
				if(b[i].yb > 500){
					b[i].state = false;
					}
				if((Math.abs(b[i].yb-bd.yb)<=20)&&
					((b[i].xb+20>=bd.xb)&&(b[i].xb<=bd.xb+bd.w))){
					b[i].vy = -b[i].vy;	
				add++;
				System.out.println(add);
				switch(add){
					case 3:
						time = 100;
						break;
					case 5:
						time = 50;
						mv = 40;
						break;
					case 20:
						time = 20;
						mv = 50;
						break;
					default:break;
					}
					if(add == 2){
							cont = 4;
							x = b[0].xb;
							y = b[0].yb-20;
							b = new BallClass[4];
							for(int j=0;j<4;j++){
								b[j] = new BallClass(x,y);			
								}
						}
					}
			
				
				b[i].xb+=b[i].vx;
				b[i].yb+=b[i].vy;
			}
			over = 0;
			for(int i=0;i<cont;i++){
				if(b[i].state){
					over = 1;
				
					break;
					}
				}
			
		try{
			Thread.sleep(time);
			}catch(InterruptedException e){}
			}
		}
	
	public void keyTyped(KeyEvent e){}
	
	public void keyPressed(KeyEvent e){
		int key;
		key=e.getKeyCode();
		if(key==KeyEvent.VK_LEFT){
			bd.leftmove(mv);
			}
		if(key==KeyEvent.VK_RIGHT){
			bd.rightmove(mv);
			}
		}
	
	public void keyReleased(KeyEvent e){}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -