Implementation of Java mini game Snake (1)

Coded in the front row: Teacher Zhao Shanshan’s lecture notes and summary of ideas&& Teacher Zhao Shanshan loves it (manual heart comparison)
Snake, Tetris, Sokoban... were the regular games on push-button mobile phones in the past.
Write it well: #ignorefoodishuahua

Next we will implement the greedy snake. First, let’s review JavaGUI.

JavaGUI interface

JFrame : Window class, defines a window
Method: setTittle(String): Set a title for the window
setBounds(int x, int y, int width, int height): x, y represent the window coordinates (the coordinates of the upper left corner of the window), pay attention to the reference The coordinate system is not a coordinate system in conventional mathematics. The upper left corner is the origin, the origin is horizontally to the right as the x-axis, and the origin is vertically downward as the y-axis. width and height represent the width and height of the window respectively.
setDefaultCloseOperation(int): Set the window closing method. Common parameters: WindowConstants.EXIT_ON_CLOSE means closing the window and ending the program.
setResizable(boolean): Set whether the window size is adjustable.
setVisible(boolean): Set whether the window is visible. Generally put at the end of the program.

Snake picture packaging

Java is an object-oriented language that encapsulates images and makes them easy to use.
Pictures prepared in advance:
Create an images package (folder) and copy the pictures to the package.
Image encapsulation: Get the path of each image and encapsulate it as ImageIcon to facilitate drawing the image with a brush later.

public class Images {
    
    

	 /* 现在是面向对象的语言,面向对象的思维,,将图片封装为一个
	 * 对象,这样在开发过程中才能操控这个对象 */
	//将图片所在的路径封装为一个对象
	public static URL bodyURL=Images.class.getResource("/images/body.jpg");
	//将这个图片封装为程序中的一个对象
	public static ImageIcon bodyImg=new ImageIcon(bodyURL);
	
	//将图片所在的路径封装为一个对象
		public static URL downURL=Images.class.getResource("/images/down.jpg");
		//将这个图片封装为程序中的一个对象
		public static ImageIcon downImg=new ImageIcon(downURL);
		
		//将图片所在的路径封装为一个对象
		public static URL foodURL=Images.class.getResource("/images/food.jpg");
		//将这个图片封装为程序中的一个对象
		public static ImageIcon foodImg=new ImageIcon(foodURL);
		
		//将图片所在的路径封装为一个对象
		public static URL headerURL=Images.class.getResource("/images/header.jpg");
		//将这个图片封装为程序中的一个对象
		public static ImageIcon headerImg=new ImageIcon(headerURL);
		
		//将图片所在的路径封装为一个对象
		public static URL leftURL=Images.class.getResource("/images/left.jpg");
		//将这个图片封装为程序中的一个对象
		public static ImageIcon leftImg=new ImageIcon(leftURL);
		
		//将图片所在的路径封装为一个对象
		public static URL rightURL=Images.class.getResource("/images/right.jpg");
		//将这个图片封装为程序中的一个对象
		public static ImageIcon rightImg=new ImageIcon(rightURL);
		
		//将图片所在的路径封装为一个对象
		public static URL upURL=Images.class.getResource("/images/up.jpg");
		//将这个图片封装为程序中的一个对象
		public static ImageIcon upImg=new ImageIcon(upURL);
		

}

Draw a window: used to place the panel, and then we add elements to the panel; it is also the program entrance.
Code:

public class StartGame {
    
    

	public static void main(String[] args) {
    
    
		
		JFrame jf = new JFrame();
		//设置一个标题
		jf.setTitle("贪吃不是一种错,偷吃才是,手动狗头");
		//设置窗体弹出的坐标
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		jf.setBounds((width-800)/2, (height-800)/2, 800, 800);
		//设置关闭方式,关闭窗口的同时,程序要随之关闭
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		//设置窗体大小不可调节
		jf.setResizable(false);
		//默认状态下窗体是隐藏状态
		jf.setVisible(true);
		
		
		
	}

}

Then create it as this Y child:

Guess you like

Origin blog.csdn.net/qq_52605986/article/details/117188069