ผลต่างระหว่างรุ่นของ "Prg2/arcade5 maze"
Jittat (คุย | มีส่วนร่วม) (→Pacman) |
Jittat (คุย | มีส่วนร่วม) |
||
แถว 127: | แถว 127: | ||
=== Movement === | === Movement === | ||
+ | |||
+ | We start by adding direction constants as in the snake game at the top of <tt>models.py</tt> | ||
+ | |||
+ | {{synfile|models.py}} | ||
+ | <syntaxhighlight lang="python"> | ||
+ | DIR_UP = 1 | ||
+ | DIR_RIGHT = 2 | ||
+ | DIR_DOWN = 3 | ||
+ | DIR_LEFT = 4 | ||
+ | |||
+ | DIR_OFFSETS = { DIR_UP: (0,1), | ||
+ | DIR_RIGHT: (1,0), | ||
+ | DIR_DOWN: (0,-1), | ||
+ | DIR_LEFT: (-1,0) } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | To deal with key pressed, we first wire the <tt>on_key_press</tt> from the window to the world. | ||
+ | |||
+ | {{synfile|maze.py}} | ||
+ | <syntaxhighlight lang="python"> | ||
+ | class MazeWindow(arcade.Window): | ||
+ | # ... | ||
+ | |||
+ | def on_key_press(self, key, key_modifiers): | ||
+ | self.world.on_key_press(key, key_modifiers) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | In class World, you have to figure out how to deal with it. First, you need to import the key code constants: | ||
+ | |||
+ | {{synfile|models.py}} | ||
+ | <syntaxhighlight lang="python"> | ||
+ | import arcade.key | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Then, in <tt>on_key_press</tt>, we can call pacman's movement function based on the key pressed. | ||
+ | |||
+ | {{synfile|models.py}} | ||
+ | <syntaxhighlight lang="python"> | ||
+ | class World: | ||
+ | # ... | ||
+ | |||
+ | def on_key_press(self, key, key_modifiers): | ||
+ | if | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
{{gitcomment|After a few trials to make sure your code works, please commit.}} | {{gitcomment|After a few trials to make sure your code works, please commit.}} |
รุ่นแก้ไขเมื่อ 16:49, 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
- 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.
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
We start by adding direction constants as in the snake game at the top of models.py
DIR_UP = 1
DIR_RIGHT = 2
DIR_DOWN = 3
DIR_LEFT = 4
DIR_OFFSETS = { DIR_UP: (0,1),
DIR_RIGHT: (1,0),
DIR_DOWN: (0,-1),
DIR_LEFT: (-1,0) }
To deal with key pressed, we first wire the on_key_press 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)
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 call pacman's movement function based on the key pressed.
class World:
# ...
def on_key_press(self, key, key_modifiers):
if
********************************************************************************* ********************************* CHECK POINT 4.1 ******************************* *********************************************************************************