飞翔的鸟小游戏

第一步是创建项目 项目名自拟

第二步创建个包名 来规范class

再创建一个包  来存储照片

如下


package game;
import java.awt.*;
import javax.swing.*;
import javax.imageio.ImageIO;
 
public class Bird {
 
	Image image;
	int x,y;
	int width,height;
	int size;
	
	double g;
	
	double t;
	
	double v0;
	
	double speed;
	
	double s;
	
	double alpha;
	
	//֡
	Image[] images;
	
	int index;
	
	public Bird() throws Exception
	{
		image=new ImageIcon("source/0.png").getImage();
		width = image.getWidth(null);
		height = image.getHeight(null);
		x=132;
		y=280;
		size=40;
	
		g=4;
		v0=20;
		t=0.25;
		speed=v0;
		s=0;
		
		alpha=0;
		
		
		images=new Image[8];
		
		for(int i=0;i<8;i++)
		{
			images[i]=new ImageIcon("source/"+i+".png").getImage();
		}
		index=0;
		
	}
	
	
	public void fly()
	{
		index++;
		image=images[(index/12)%8];
	}
	
	
	public void step()
	{
		double v0=speed;
		
		s=v0*t+g*t*t/2;
		
		y=y-(int)s;
		
		double v=v0-g*t;
		speed =v;
		
		alpha=Math.atan(s/8);
		
	}
	
	
	public void flappy()
	{
		
		speed=v0;
	}
	
	
	public boolean hit(Ground ground)
	{
		boolean hit =y+size/2>ground.y;
		if(hit)
		{
			y=ground.y-size/2;
			alpha=Math.PI/2;
		}
		return hit;
	}
	
	
	public boolean hit(Column column)
	{
		
		if(x>column.x-column.width/2-size/2&&x<column.x+column.width/2+size/2)
		{
			if(y>column.y-column.gap/2+size/2&&y<column.y+column.gap/2-size/2) return false;
			return true;
		}
		return false;
	}
}

package game;
 
import javax.imageio.ImageIO;
import java.util.*;
 
import javax.swing.*;
 
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.awt.*;
 
public class BirdGame extends JPanel {
	
	
	Image background;
	Image startImage;
	Image overImage;
	Ground ground;//����
	Column column1,column2;
	Bird bird;
	int score;
	int state;//״̬
	//״̬����
	public static final int START=0;
	public static final int RUNNING=1;
	public static final int GAME_OVER=2;
	
	public BirdGame() throws Exception
	{
		background = new ImageIcon("./source/bg.png").getImage();
		startImage = new ImageIcon("./source/start.png").getImage();
		overImage=new ImageIcon("./source/gameover.png").getImage();
	//״̬
		ground=new Ground();
		column1=new Column(1);
		column2=new Column(2);
		bird=new Bird();
		score=0;
		state=0;
	}
	
	public void paint(Graphics g)
	{
 
		g.drawImage(background, 0, 0,null);
		
		g.drawImage(ground.image, ground.x, ground.y, null);
	
		g.drawImage(column1.image,column1.x-column1.width/2,column1.y-column1.height/2,null);
		g.drawImage(column2.image,column2.x-column2.width/2,column2.y-column2.height/2,null);
	
		Graphics2D g2=(Graphics2D) g;
		g2.rotate(-bird.alpha,bird.x,bird.y);
		g.drawImage(bird.image,bird.x-bird.width/2,bird.y-bird.height/2,null);
		g2.rotate(bird.alpha,bird.x,bird.y);
		
		Font f=new Font(Font.SANS_SERIF,Font.BOLD,40);
		g.setFont(f);
		g.drawString(""+score, 40, 60);
		g.setColor(Color.WHITE);
		g.drawString(""+score,40-3, 60-3);
		
		switch(state)
		{
		case START:
			g.drawImage(startImage, 0, 0, null);
			break;
		case GAME_OVER:
			g.drawImage(overImage, 0, 0, null);
			break;
		}
	}
	
	public void action() throws Exception
	{
		
		MouseListener l=new MouseAdapter()
		{
			public void mousePressed(MouseEvent e)
			{
				try {
					switch(state) {
					case START:
						//״̬
						state=RUNNING;
						break;
					case RUNNING:
						
						bird.flappy();
						break;
					case GAME_OVER:
						
						column1=new Column(1);
						column2=new Column(2);
						bird=new Bird();
						score=0;
						state=START;
						break;
					}
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
				}
			}
			
		};
		addMouseListener(l);
		while(true)
		{
			switch(state)
			{
			case START:
				bird.fly();
				ground.step();
				break;
			case RUNNING:
				ground.step();
				column1.step();
				column2.step();
				bird.fly();
				bird.step();
 
				score++;
				//
				if(bird.hit(ground)||bird.hit(column1)||bird.hit(column2))
				{
					state=GAME_OVER;
				}
				break;
			}	
			
			Thread.sleep(1000/60);
			repaint();
		}
	}
	
	
	
	public static void main(String[] args) throws Exception
	{
		
		JFrame frame=new JFrame();
		BirdGame game=new BirdGame();
		frame.add(game);
		frame.setSize(440,670);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		game.action();
	}
	
}
package game;
 
import java.util.*;
import java.awt.*;
 
import javax.imageio.ImageIO;
import javax.swing.*;
 
public class Column {
 
	Image image;
	
	int x,y;
	int width,height;
	
	int gap;
	
	int distance;
	Random random =new Random();
	
	
	
	public Column(int n) throws Exception
	{
		image=new ImageIcon("source/column.png").getImage();
		width=image.getWidth(null);
		height=image.getHeight(null);
		gap=144;
		distance=245;
		x=550+(n-1)*distance;
		y=random.nextInt(218)+132;
	}
	
	public void step()
	{
		x-=4;
		if(x<= -width/2)
		{
			x=distance*2-width/2;
			y=random.nextInt(218);
		}
	}
}
package game;
 
import javax.swing.*;
import java.awt.*;
 
public class Ground {
 
	Image image;
	
	int x,y;
	
	int width,height;
	
	
	public Ground() throws Exception
	{
		image =new ImageIcon("source/ground.png").getImage();
		width=image.getWidth(null);
		height=image.getHeight(null);
		x=0;
		y=500;
	}
	
	
	public void step()
	{
		x-=4;
		if(x<=-109)
		{
			x=0;
		}
	}
	
	
}

 

 

 

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的飞翔的小小游戏的代码实现,希望对你有所帮助: ```c++ #include <iostream> #include <conio.h> #include <windows.h> using namespace std; bool gameOver; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; print = true; } } if (!print) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Score:" << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { int prevX = tailX; int prevY = tailY[0]; int prev2X, prev2Y; tailX = x; tailY = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } //if (x > width || x < 0 || y > height || y < 0) //撞墙结束 // gameOver = true; if (x >= width) x = 0; else if (x < 0) x = width - 1; if (y >= height) y = 0; else if (y < 0) y = height - 1; for (int i = 0; i < nTail; i++) if (tailX[i] == x && tailY[i] == y) gameOver = true; if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(50); //sleep(10); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值