Using Java to write the Elves eat Beans Game

Source: Internet
Author: User
Tags abs gety stub

After learning the thread to do a fairy eat beans game.

Form class:

Package Www.csdn.net.zuoye;
Use the form to realize the Elves Pacman game import Java.awt.Color;
Import Java.awt.Graphics;
Import java.awt.event.KeyEvent;
Import Java.awt.event.KeyListener;

Import Java.util.Random;
Import Javax.swing.JFrame;

Import Javax.swing.JOptionPane;
	public class Gamejframe extends JFrame implements KeyListener {//define starting position private int x = = 30;
	Defines the intermediate variable private Boolean flag = true;
	private static final int dir_up = 1;
	private static final int dir_down = 2;
	private static final int dir_left = 3;

	private static final int dir_right = 4;
	Define initialization radians private int arc1 = 20;
	private int arc2 = 320;
	Define counter (let the top and bottom two mouths move through the counter) private int count = 0;
	Defines the object of the bean as a private bean bean;
	Define direction private int dir =-1;
	Generate random number private Random Random;
	Defines the score private int score=0;
	Define speed private int k=100;
		Public Gamejframe () {this.setbounds (300, 200, 200, 300);
		This.setdefaultcloseoperation (Jframe.exit_on_close);
		This.setlocationrelativeto (NULL);
This.setresizable (FALSE);		* * * 1.this.setbackground (Color.gray);
		 * This.getcontentpane (). setvisible (false);
		*/This.getcontentpane (). SetBackground (Color.pink);
						New Thread () {public void run () {while (flag) {try {Boolean temp = Checkhit ();
							if (temp) {Initbean ();
							score++;
							if (score%10==0) {k-=5;
						} thread.sleep (k);
						count++;
							if (count% 2 = 0) {//mouth open and closed in case ARC1 = 40;
						ARC2 = 280;
							else {arc1 = 20;
						ARC2 = 320;
							Switch (dir) {case Dir_up:y-= 5;
						Break
							Case Dir_down:y + + 5;
						Break
							Case DIR_LEFT:X-= 5;
						Break
							Case DIR_RIGHT:X + + 5;
						Break
						Default:break;
						} repaint ();
							if (Y > 275 | | | y <) {flag = false;
							Joptionpane.showmessagedialog (NULL, "Game over, your score is:" +score);
						Dispose (); }else if (x>176| |
x==0) {flag=false;							Joptionpane.showmessagedialog (NULL, "Game over, your score is:" +score);
						Dispose ();
					} catch (Interruptedexception e) {e.printstacktrace ();

		}}}.start ();

		Add listening This.addkeylistener (this);
		Create a Random object random = new random ();
	Initbean ();
		The public void Initbean () {///randomly generates coordinates int RX = random.nextint (5) +10;
		int ry = Random.nextint (15) +7;
		produce bean bean = new Bean (RX * BEAN.R * 2, ry * BEAN.R * 2);/coordinates must be randomly generated} @Override public void Paint (Graphics g) {
		Super.paint (g);
			G.setcolor (color.red);/Set color switch (dir) {case Dir_up:g.fillarc (x, y,, Arc1 + arc2);
		Break
			Case Dir_down:g.fillarc (x, Y, M, Arc1 + 270, ARC2);
		Break
			Case Dir_left:g.fillarc (x, Y, M, Arc1 + 180, ARC2);
		Break
			Case Dir_right:g.fillarc (x, Y, M, Arc1, ARC2);
		Break
		default://in order to be able to display, need to have a display of state G.fillarc (x, Y, M, Arc1, ARC2);
		}//Draw Bean G.setcolor (color.yellow); G.fillarc (Bean.getX (), Bean.gety (), BEAN.R * 2, BEAN.R * 2, 0, 360); G.fillarc (x, Y, M, Arc1, ARC2);//round//G.fillrect (50, 30, 20, 20);//rectangle} public static void Main (string[] Arg
		s) {Gamejframe g = new Gamejframe ();
	G.setvisible (TRUE); /** * Enter a key when executing * * @Override public void keytyped (KeyEvent e) {//TODO auto-generated a stub system.o
	Ut.println ("type"); /** * When executing * * @Override public void keypressed (KeyEvent e) {//TODO auto-generated stub/System
		. Out.println ("button"); Gets the value of the key = int key = E.getkeycode ();//Query key value (with this method to the next key plus event)//According to the top and bottom arrows change method switch (key) {Case 38:dir = dir_up
			;
		Break
			Case 40:dir = Dir_down;
		Break
			Case 37:dir = Dir_left;
		Break
			Case 39:dir = dir_right;
		Break
		Default:break; }/** * When lifting * * @Override public void keyreleased (KeyEvent e) {//TODO auto-generated stub/Sy
	Stem.out.println ("lift"); }//Define Collision Detection public Boolean checkhit () {if (BEAn!= null) {if (bean.getx () = = x) {if (Math.Abs (Bean.gety ()-y) <) {return true;//encountered}}
				if (bean.gety () = = y) {if Math.Abs (Bean.getx ()-X) < {return true;
	}} return false;
 }
}


Beans class:

Package Www.csdn.net.zuoye;

Draw Beans Public
class Bean {
	//coordinate
	private int x, y;
	The radius of the beans is public
	static int r=5;
	public int GetX () {//get horizontal coordinate return
		x;
	}
	public int GetY () {//Get ordinate return
		y;
	}
	public Bean (int x, int y) {
		this.x = x;
		This.y = y;
	}
}

Effect:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.