Java swing开发简易版跳一跳

程序运行效果
在这里插入图片描述
直接上代码

主类:

package happy;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.TimerTask;
import java.util.Timer;

import javax.swing.*;

public class win extends JFrame{
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new win();

	}
	public win(){
		open a=new open();
		this.addKeyListener(a);
		this.add(a);
		
		
		this.setTitle("跳一跳");
		this.setSize(800, 600);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
	}

}



class open extends JPanel implements KeyListener{
	private static int x=100,y=505,a=50,b=50,l,u;
	private static String f=">";
	private static boolean fx=true;
	private static boolean yu=true;
	public open(){
		
	}
	public void paint(Graphics g){
		super.paint(g);
		g.setColor(Color.black);
		g.fillRect(0, 0, 800, 800);
		g.setColor(Color.GREEN);
		g.fillRect(0, 555, 800, 10);
		g.setColor(Color.cyan);
		g.fillRect(x, y, a, b);
		
		g.setColor(Color.ORANGE);
		g.fillRect(shu.xa,shu.ya,shu.wa, shu.ha);	
		
		g.setColor(Color.BLUE);
		g.fillRect(shu.xb, shu.yb, shu.wb, shu.hb);
		
		g.setColor(Color.white);
		g.setFont(new Font("微软雅黑",Font.CENTER_BASELINE,25));
		g.drawString(f, x+18, y-5);
	}
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(KeyEvent.VK_UP==e.getKeyCode()){
			y-=5;
			System.out.println(y);
			repaint();
			}
		if(KeyEvent.VK_DOWN==e.getKeyCode()){
			y+=5;
			System.out.println(y);
			repaint();
			}
		if(KeyEvent.VK_RIGHT==e.getKeyCode()){
			fx=true;
			f=">";
			if(x<745){
			x+=5;}
			System.out.println(x);
			if(x>shu.xa+shu.wa-10&&y==shu.ya-50
			||x>shu.xb+shu.wb-10&&y==shu.yb-50){
			Timer p=new Timer();
			TimerTask o=new TimerTask(){

				@Override
				public void run() {
					// TODO Auto-generated method stub
					
					if(y>=505){p.cancel();}
					if(y<505){y+=12;}
					repaint();
				}
				
			};
			p.schedule(o, 50L,20L);
			
			}
			
			
			
			repaint();
			}
		if(KeyEvent.VK_LEFT==e.getKeyCode()){
			fx=false;
			f="<";
			if(x>0){
			x-=5;}
			System.out.println(x);
			if(x<shu.xa-40&&y==shu.ya-50
			||x<shu.xb-40&&y==shu.yb-50){
				Timer p=new Timer();
				TimerTask o=new TimerTask(){

					@Override
					public void run() {
						// TODO Auto-generated method stub
						
						if(y>=505){p.cancel();}
						if(y<505){y+=12;}
						repaint();
					}
					
				};
				p.schedule(o, 50L,20L);
			}
			
			repaint();
			}
		if(KeyEvent.VK_SPACE==e.getKeyCode()){
			if(l<16){
			a+=1;
			b-=2;
			y+=2;
			l+=1;
			repaint();
			}
			}
		
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		if(KeyEvent.VK_SPACE==e.getKeyCode()){
			yu=true;
			
			a=50;
			b=50;
			y+=2;
			y-=l*2+2;
			u=l;
			
			Timer t=new Timer();
			TimerTask s=new TimerTask(){
				@Override
				public void run() {
					// TODO Auto-generated method stub
					l-=1;
					if(l>0){y-=12;if(fx==true){if(x<740)x+=7;}if(fx==false){if(x>0)x-=7;}}
					else if(l<0){
						u-=1;
						if(u>0){y+=12;if(fx==true){if(x<740)x+=7;}if(fx==false){if(x>0)x-=7;}}
						if(u<0){t.cancel();u=0;l=0;}
						}
					else if(x>shu.xa-40&&x<shu.xa+shu.wa-10&&y<shu.ya-40){t.cancel();y=shu.ya-50;yu=false;u=0;l=0;}
					else if(x>shu.xb-40&&x<shu.xb+shu.wb-10&&y<shu.yb-40){t.cancel();y=shu.yb-50;yu=false;u=0;l=0;}
					if(yu==true&&u==0){
					Timer p=new Timer();
					TimerTask o=new TimerTask(){

						@Override
						public void run() {
							// TODO Auto-generated method stub
							if(y>=505){p.cancel();t.cancel();u=0;l=0;}
							if(y<505){y+=12;if(fx==true){if(x<740)x+=7;}if(fx==false){if(x>0)x-=7;}}
							
							repaint();
						}
						
					};
					p.schedule(o, 50L,20L);
					
					}
					
					repaint();
				}
				
			};
			t.schedule(s, 100L,15L);
			
			repaint();
			
			}
		
	}
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}

存储类:

package happy;

public class shu {
	//存储方块
	public static int xa=200;
	public static int ya=450;
	public static int wa=40;
	public static int ha=20;
	
	public static int xb=350;
	public static int yb=330;
	public static int wb=50;
	public static int hb=20;
	

}

发布了15 篇原创文章 · 获赞 6 · 访问量 2803

猜你喜欢

转载自blog.csdn.net/qq_41743240/article/details/105609583