Prg2/arcade6 dotrun
รุ่นแก้ไขเมื่อ 05:33, 21 กุมภาพันธ์ 2562 โดย Jittat (คุย | มีส่วนร่วม) (→Create a new project and set up a Git repository)
- 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 update(self, delta):
self.world.update(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 update(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 update(self, delta):
self.dot.update(delta)
Try to run the game to see if there is a dot moving. Then, commit the code.