“你好,士兵!”
“您好,松鼠船长,先生!”
“恭喜,今天休息一天。”
“我们可以为所欲为吗?”
“是的,阿米戈,你可以整天玩你的玩具,比如推箱子,我从小最喜欢的游戏,我435关都过不去,你能帮忙吗?”
“当然,我会帮忙的,我会告诉你的,队长。”
松鼠队长从口袋里掏出一个旧的 Game Boy,启动了单色版的推箱子。但随之而来的是失望。电池电量耗尽,Game Boy 关闭。
队长静静地看着 Game Boy,然后看看 Amigo,然后又看看 Game Boy。他转身,慢慢走向自己的办公室。
“船长!我们来写我们自己的推箱子吧!我们会想出 1000 个额外的关卡,并制作一个很酷的图形界面。”
“阿米戈,你总是让我感到惊讶。联系特工 IntelliJ IDEA。如果他没有其他任务给你,并且想在他的休息日帮助你,那就继续吧。”
20
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 1)
Today we're going to write our own implementation of the game Sokoban. This is a logical puzzle game.
You can read more about it on Wikipedia. The game will consist of 3
main components (as you may have guessed, you'll need the MVC pattern).
The graphical interface will be implemented using Swing.
10
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 2)
The foundation is laid, so now let's start building. You can think of the gameplay
as the interaction of GameObject objects. We'll have several types of them:
Box, StorageLocation (the place where you need to put the box), Wall, and Player.
2.1. Add an abstract GameObject class to the model package
10
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 3)
Soon we'll be creating various game objects. It would be handy to be able
to draw them somewhere right away and see how they look. Objects will be
drawn on the game board represented by a Board object.
You've received modified code for the View class that creates a Board object and
sets up the view
20
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 4)
We already have a shared GameObject class. It's time to create classes
for specific types of game objects.
4.1. Some game objects can move (player and boxes) and some can't (walls
and storage locations).
4.1.1. Add the Movable interface to the model package.
4.1.2. The Movable interface must have a
20
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 5)
Box and Player game objects can not only
collide with other objects, but also move around the board.
5.1. Create a Player class and a Box class in the model package. Make each of them
inherit the most appropriate class.
5.2. The created classes must support the interface responsible for object
move
10
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 6)
It's time to create the StorageLocation class. This class will be responsible for the game board cells
that all the boxes must be moved to. Objects of this type must not move
across the board or collide with other game objects.
6.1. Add a StorageLocation class to the model package.
6.2. The class m
10
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 7)
Let's make the Wall class. A wall can collide with other objects, but it
can't move.
7.1. Add a Wall class to the model package.
7.2. Make the class inherit the appropriate parent class.
7.3. Implement a constructor with int x and int y parameters.
7.4. Implement the method for drawing.
Hint: you c
10
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 8)
You've created the complete collection of game objects. Let's create a class to
store these objects.
8.1. Create a GameObjects class in the model package.
8.2. Add the following:
8.2.1. Set walls, Set boxes, Set storageLocations, and Player player fields.
8.2.2. Getters
10
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 9)
Various events will occur during the game. Let's create an
EventListener interface. Every class that wants to handle events must
implement it. And classes that generate events will call
this interface's methods.
9.1. Add the EventListener interface to the controller package.
9.2. Add these void met
20
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 10)
The game will have several levels, and all of them will be stored in a text file. Now we'll
write a LevelLoader test stub. Why a test stub? We don't
need the full functionality yet. It's pretty complicated, so we'll leave it for later. For now:
10.1. Create a LevelLoader class in the model package.
20
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 11)
Let's begin fleshing out the Model class' functionality. Add the following:
11.1. A GameObjects gameObjects field. It will store our game objects.
11.2. An int currentLevel field that stores the current level. Initialize it
to 1.
11.3. A LevelLoader levelLoader field that stores the level loader.
I
20
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 12)
Let's try organizing the interaction between the view and model.
12.1. Add an update() method to the View class. It must call the repaint() method on the
board field. In other words, the update() method will update the view
(redraw the board).
12.2. Add the GameObjects getGameObjects() method to th
20
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 13)
Let's fill out the controller's functionality.
13.1. In addition to what the Controller class constructor already has, add event listeners
on the model and view. The controller must itself be a listener.
13.2. Implement the controller's methods:
13.2.1. move(Direction direction) - should call the m
20
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 14)
Let's make our game more interactive (using a keyboard
to move the player). We'll start by handling keystrokes.
14.1. Add to the Board class a nested KeyHandler class that
inherits KeyAdapter.
14.2. Overload its keyPressed() method. If the key with code VK_LEFT is pressed,
then send an event with a
40
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 15)
The time has come to implement the model's move() method, which is responsible for movement. But first,
let's implement some helper methods. Add the following methods to the Model class:
15.1. boolean checkWallCollision(CollisionObject gameObject, Direction direction). This
method checks for collis
20
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 16)
All that remains is to finish implementing the level loader.
16.1. Open the levels.txt file and carefully study the file structure. The character 'X'
represents a wall, ‘*’ represents a box, ‘.’ represents a storage location, ‘&’ represents a box in a storage location, and ‘@’ represents the player
10
任务
Java Collections, 第 10 级, 课程 15
Sokoban (part 17)
You're a superstar! You've made a great game. Now you can relax a bit and play it.
If you ever get tired of it, which is practically impossible, you could make the following
improvements:
17.1. Use images to represent the objects.
17.2. Make a level editor.
17.3. Add rankings. You could even post t