A little experience of writing small games in java

In order to consolidate the foundation of java, I wrote a small game of Thunder fighter, using the knowledge of swing and javaSE thread, and later learned Spring and Mybatis, so I wrote a server based on TCP to manage some basic data of players Afterwards, this time I will share some of my thoughts about writing this game.
First put the picture first
Insert picture description here
This is the picture in the game

There are too many login pages, the main page and so on, but they are not released. To briefly introduce, the gameplay is similar to the Thunder fighter. There are 4 skills in the mouse or keyboard control. After the skills are released, there is a shrinkage. The first picture is to play a soldier, and the second picture is to play a boss. After the end, the result is uploaded to the server.

1. I have written two blogs about the problem of flickering on the screen
https://blog.csdn.net/D1124615130/article/details/104436930

https://blog.csdn.net/D1124615130/article/details/104439034

2. Too much game calculation results in screen freeze.
My method is to open two threads, one dedicated to drawing and the other dedicated to data processing. The refresh frequency of the data processing thread is preferably greater than the drawing frequency.

3. Objects in the container may appear when the Thunder Fighter game is played for a long time. When the traversal is too large, the data will be stuck, and the expired objects should be cleaned up in time (such as out of the screen range, being killed by the player, etc.)

while(!enemys.isEmpty()&&(enemys.peek()==null||!enemys.peek().isAlive()))
			enemys.poll();

Enemies are ConcurrentLinkedQueue objects. Of course, the container must be thread-safe. The
above code is called before data processing, and some outdated data will be cleaned up first.

4. Suspend , my method:
This is a drawing thread.
Lock is a class I wrote in singleton mode. There is a semaphore, a boolean type suspend, and an Object type lock.
Semaphore is used to apply for access to boolean type suspend, and then use lock to call wait

class DrawThread extends Thread{
		@Override
		public void run() {
			while(true) {
				try {
					Lock.getSemaphore().acquire();
					if(Lock.isSuspend()) {
						Lock.getSemaphore().release();
						Object lock=Lock.getLock();
						synchronized (lock) {
							lock.wait();
						}
						requestFocus();
					}else
						Lock.getSemaphore().release();
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				repaint();
				try {
					sleep(Data.REFALSH_INTERVAL);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
	}

To wake up the sleeping thread, I did this:

try {
		Lock.getSemaphore().acquire();
		Lock.setSuspend(false);
		Lock.getSemaphore().release();
	} catch (InterruptedException e1) {
		e1.printStackTrace();
	}
	Object lock=Lock.getLock();
	synchronized (lock) {
		lock.notifyAll();
	}

After quite a few trials, there was no problem. I also feel that this code is very troublesome. If you have a better way, please give me a comment. Thanks.

Published 21 original articles · liked 0 · visits 721

Guess you like

Origin blog.csdn.net/D1124615130/article/details/104559665