A dinosaur parkour game made after learning java

little dinosaur run

The project is an example from the book and then did the programming implementation myself. The difficulty of creation and related technical points are shared below.
The project is composed of some basic operations of java, such as the concept of thread, realize animation effect,
learn to use AWT to draw game screen, Rectangle class to realize collision detection, learn to use javax.sound package to realize sound playback.

1. Threads
are a key part of the threading project, because in the process of realizing dinosaur running, it is actually repeating and redrawing pictures, so the control of this process must use the concept of threads, and only through threads can better Control related processes. For example, to realize the appearance of dinosaurs running, in fact, three dinosaur pictures are continuously drawn, which visually achieves the effect of running. Under the FreshThead class is the refresh frame thread class in the game, which inherits from the Thread thread class, and loops infinitely in the main method of the thread, redrawing the game panel every time after a certain period of time, that is, the repaint() method. Before each redraw, the instructions input by the user will be executed first, so that the picture drawn each time is different, and the effect of animation is achieved by switching pictures in a very short time.

2. The main interface of the AWT drawing
game is the core of the game. First, a frame of a JFrame is created, and then a background image is drawn on it. The background image is actually a picture to play in turn, define two identical photos, and then define the starting point. Position, move a little bit to the left each time, and then connect a picture behind it, then detect the current picture to the end and automatically clear it, and then draw it immediately after him. For example, the panel width is 800, then the starting position of the first picture is 0, and the starting position of the second picture is 800. Each time it moves to the left by 5, it is subtracted by 5, so that when the coordinates of the first picture are -800 , the whole picture has left the panel, and then reassign it to the coordinate of 800, so as to realize the scrolling effect of the background.
There is also the class of dinosaurs and the class of obstacles, which uses the principle of image overlay, first draw the background picture, and then draw the dinosaurs and obstacles, then the interface of the game is realized. The effect of the movement is actually the constant change of the x-coordinate, and the jump of the dinosaur is the constant change of the y-coordinate.

public BackgroundImage() {
    
    
    	  try {
    
    
			image1=ImageIO.read(new File("image/99.jpg"));
			image2=ImageIO.read(new File("image/99.jpg"));
		} catch (Exception e) {
    
    
			e.printStackTrace();
		}
    	  image=new BufferedImage(800, 400,BufferedImage.TYPE_INT_RGB);
    	  g2=image.createGraphics();                            //获取主图片绘图对象
    	  x1=0;
    	  x2=800;
    	  g2.drawImage(image1,x1,0,null);
      }
      public void roll() {
    
    
    	  x1-=SPEED;                                     //第一幅图片左移
    	  x2-=SPEED;                                     //第二幅图片左移
    	  if(x1<=-800) {
    
    
    		  x1=800;
    	  }
    	  if(x2<=-800) {
    
    
    		  x2=800;
    	  }
    	  g2.drawImage(image1, x1, 0,null);
    	  g2.drawImage(image2, x2, 0,null);
      }

3. Rectangle collision detection

if(o.getBounds().intersects(golden.getFootBounds())||o.getBounds().intersects(golden.getHeadBounds())) {
    
    
    				 Sound.hit();                              
    				 gameOver();
    			 }

o is the border of the obstacle, and golden is the border of the dinosaur. If it collides, play the hit sound, and then execute the gameover; the game logic is established.

4. The javax.sound package implements sound playback
, which should be the most complicated part for me, and because of the limitation of the java format, only a few types of music can be used, such as wav and MP3, etc., and the music cutting is also very complicated. , the background material is also difficult to find (students with relevant information can find me to improve this project together).
The principle is

byte[] auBuffer=new byte[1024*128];                  //创建128k缓冲区
		do {
    
    
			AudioInputStream audioinputstream=null;          //创建音频输入流对象
			SourceDataLine auline=null;                      //混频器源数据行
			try {
    
    
				//从音乐文件中获取音频输入流
				audioinputstream=AudioSystem.getAudioInputStream(soundFile);
				AudioFormat format=audioinputstream.getFormat();       //获取音频格式
				//按照源数据行类型和指定音频格式创建数据行对象
				DataLine.Info info=new DataLine.Info(SourceDataLine.class, format);
				//利用音频系统类获得与指定line.info对象中匹配的行对象
				auline=(SourceDataLine)AudioSystem.getLine(info);
				auline.open(format);                             //按照指定格式打开源数据行
				auline.start();                                  //源数据行开启读写活动
				int byteCount=0;                                 //记录音频输入流读出的字节数 
				while(byteCount!=-1) {
    
                               //如果音频输入流中读取的字节数不为-1
					byteCount=audioinputstream.read(auBuffer, 0, auBuffer.length);//从音频数据流中读取128k的数据
					if(byteCount>=0) {
    
                                                //如果读出有效数据
						auline.write(auBuffer, 0, byteCount);                     //将有效数据写入数据行中
					}
				}
			} catch (Exception e) {
    
    
			e.printStackTrace();
			}finally {
    
    
				try {
    
    
					auline.flush();
				} catch (Exception e2) {
    
    
					System.out.println("666");
				}
				auline.drain();
				auline.close();
			}
		} while (circulate);                //根据循环标志判断是否循环播放
		

Then play the corresponding music by judging and so on!

5. Screenshots of the game running
insert image description here
insert image description here

insert image description here
The folder cannot be uploaded, and friends who need files or are interested can privately message me.

I am a college student, and I am a rookie in my study. I welcome the guidance of the big guy, and welcome everyone to like and follow!

Guess you like

Origin blog.csdn.net/m0_48781656/article/details/108928754#comments_20968974