Prg2/arcade5 maze

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
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
  • blah

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.

File: 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.

Gitmark.png Create your git repository and make the first commit.

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

File: models.py
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.

File: maze.py
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.

File: 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.

File: maze.py
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.

Gitmark.png Commit your work.

Movement

We start by adding direction constants as in the snake game at the top of models.py

File: 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) }

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.

File: maze.py
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:

File: models.py
import arcade.key

Then, in on_key_press, we can call pacman's movement function based on the key pressed.

File: models.py
class World:
    # ... 

    def on_key_press(self, key, key_modifiers):
        if



Gitmark.png After a few trials to make sure your code works, please commit.
*********************************************************************************
********************************* CHECK POINT 4.1 *******************************
*********************************************************************************