ผลต่างระหว่างรุ่นของ "Prg2/arcade5 maze"
Jittat (คุย | มีส่วนร่วม) |
Jittat (คุย | มีส่วนร่วม) (→Faster) |
||
แถว 238: | แถว 238: | ||
********************************************************************************* | ********************************************************************************* | ||
− | ********************************* CHECK POINT | + | ********************************* CHECK POINT 5.1 ******************************* |
********************************************************************************* | ********************************************************************************* | ||
รุ่นแก้ไขเมื่อ 22:15, 28 กุมภาพันธ์ 2562
- This is part of the course Programming 2, the material is originally from 01219245/cocos2d/Maze from 01219245.
เนื้อหา
Rough steps
We will follow these steps to implement a pacman-like game.
- Shows and moves pacman
- Shows maze
- Moves pacman in the maze, while respecting walls.
The Pacman and its movements
Create a new project and set up a Git repository
We will start with an empty game template. Put the following code in our main program maze.py.
import arcade
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class MazeWindow(arcade.Window):
def __init__(self, width, height):
super().__init__(width, height)
arcade.set_background_color(arcade.color.WHITE)
def on_draw(self):
arcade.start_render()
def main():
window = MazeWindow(SCREEN_WIDTH, SCREEN_HEIGHT)
arcade.set_window(window)
arcade.run()
if __name__ == '__main__':
main()
Try to run the game to see if an empty white window appears. Then, create a git repository at the project directory and commit the code.
Creating the player model and the sprite
In this step, we shall create a sprite for the player, and show it in the middle of the screen.
Use a graphic editor to create an image for our player. The image should be of size 40 pixels x 40 pixels. Save the image as images/dot.png and try to make it look cute.
We will continue our model/window code structure. So let's create a dot model (called Player) and World in models.py as in our previous projects. Note that currently the Player do nothing in update
class Pacman:
def __init__(self, world, x, y):
self.world = world
self.x = x
self.y = y
def update(self, delta):
pass
class World:
def __init__(self, width, height):
self.width = width
self.height = height
self.pacman = Pacman(self, width // 2, height // 2)
def update(self, delta):
self.pacman.update(delta)
We will import these classes into our main program.
from models import World, Pacman
We will show the pacman. Download sprite images from https://theory.cpe.ku.ac.th/~jittat/courses/ooplab/pacman/. Save pacman.png in directory images.
As in the previous projects, we then use ModelSprite to display the sprite. Add the class in maze.py.
class ModelSprite(arcade.Sprite):
def __init__(self, *args, **kwargs):
self.model = kwargs.pop('model', None)
super().__init__(*args, **kwargs)
def sync_with_model(self):
if self.model:
self.set_position(self.model.x, self.model.y)
def draw(self):
self.sync_with_model()
super().draw()
Then update MazeWindow to include the world and create ModelSprite accordingly.
class MazeWindow(arcade.Window):
def __init__(self, width, height):
super().__init__(width, height)
arcade.set_background_color(arcade.color.WHITE)
self.world = World(SCREEN_WIDTH, SCREEN_HEIGHT)
self.pacman_sprite = ModelSprite('images/pacman.png',
model=self.world.pacman)
def update(self, delta):
self.world.update(delta)
def on_draw(self):
arcade.start_render()
self.pacman_sprite.draw()
Try to run the game. You should see your sprite in the middle of the screen.
Movement
Models
We start by adding direction constants as in the snake game at the top of models.py
DIR_STILL = 0
DIR_UP = 1
DIR_RIGHT = 2
DIR_DOWN = 3
DIR_LEFT = 4
DIR_OFFSETS = { DIR_STILL: (0,0),
DIR_UP: (0,1),
DIR_RIGHT: (1,0),
DIR_DOWN: (0,-1),
DIR_LEFT: (-1,0) }
We add attribute direction to Pacman (initialized to DIR_STILL). We add method move to deal with Pacman's movements based on its direction. We call move in update.
class Pacman:
def __init__(self, world, x, y):
# ...
self.direction = DIR_STILL
def move(self, direction):
self.x += DIR_OFFSETS[direction][0]
self.y += DIR_OFFSETS[direction][1]
def update(self, delta):
self.move(self.direction)
Events
In this game, we need to deal with key pressed and key released (because we want pacman to stop if we release the keys).
To deal with key events, we first wire the on_key_press and on_key_release from the window to the world.
class MazeWindow(arcade.Window):
# ...
def on_key_press(self, key, key_modifiers):
self.world.on_key_press(key, key_modifiers)
def on_key_release(self, key, key_modifiers):
self.world.on_key_release(key, key_modifiers)
In class World, you have to figure out how to deal with it. First, you need to import the key code constants:
import arcade.key
Then, in on_key_press, we can update the pacman's direction based on the key pressed. When a key is released, we let Pacman stops.
class World:
# ...
def on_key_press(self, key, key_modifiers):
if key == arcade.key.UP:
self.pacman.direction = DIR_UP
if key == arcade.key.DOWN:
self.pacman.direction = DIR_DOWN
if key == arcade.key.LEFT:
self.pacman.direction = DIR_LEFT
if key == arcade.key.RIGHT:
self.pacman.direction = DIR_RIGHT
def on_key_release(self, key, key_modifiers):
self.pacman.direction = DIR_STILL
Try to run the code. Try play with key release handling by trying to press new arrow key after the previous one without releasing the key, then try to release the first key and see how the program behaves. Explain why.
Faster
The pacman moves fairly slowly. Let's add a default speed, by adding another constants in models.py.
MOVEMENT_SPEED = 5
And update Pacman.move to use this constant.
class Pacman:
# ...
def move(self, direction):
self.x += MOVEMENT_SPEED * DIR_OFFSETS[direction][0]
self.y += MOVEMENT_SPEED * DIR_OFFSETS[direction][1]
********************************************************************************* ********************************* CHECK POINT 5.1 ******************************* *********************************************************************************