Prg2/arcade6 dotrun
- This is part of the course Programming 2.
In this tutorial we will sketch rough steps and new techniques for building the Dot Run game.
เนื้อหา
Steps
These are the steps we will take to implement the game.
- Shows Dot
- Dot moves to the right
- Dot moves and jump on the floor
- Viewport
- Shows platforms
- Jumps on top of platforms
- Recycles platforms
- Shows and collects coins
- Shows score
New techniques
Viewport
Drawing primitives
Player's states for jumping off and onto a platform
Let's get started
Create a new project and set up a Git repository
Create a directory to keep the project. Create a git repository there.
We will start with a very simple dot moving game.
File: dotrun.py
import arcade
from models import World
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
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()
class DotRunWindow(arcade.Window):
def __init__(self, width, height):
super().__init__(width, height)
arcade.set_background_color(arcade.color.GRAY)
self.world = World(SCREEN_WIDTH, SCREEN_HEIGHT)
self.dot_sprite = ModelSprite('images/dot.png',
model=self.world.dot)
def animate(self, delta):
self.world.animate(delta)
def on_draw(self):
arcade.start_render()
self.dot_sprite.draw()
if __name__ == '__main__':
window = DotRunWindow(SCREEN_WIDTH, SCREEN_HEIGHT)
arcade.run()
File: models.py
GRAVITY = -1
MAX_VX = 10
ACCX = 1
class Model:
def __init__(self, world, x, y, angle):
self.world = world
self.x = x
self.y = y
self.angle = 0
class Dot(Model):
def __init__(self, world, x, y):
super().__init__(world, x, y, 0)
self.vx = 0
self.vy = 0
def animate(self, delta):
if self.vx < MAX_VX:
self.vx += ACCX
self.x += self.vx
class World:
def __init__(self, width, height):
self.width = width
self.height = height
self.dot = Dot(self, 0, 100)
def animate(self, delta):
self.dot.animate(delta)
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.