-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwitch-demo.py
103 lines (75 loc) · 2.8 KB
/
witch-demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from enum import Enum
import sys
import time
import pygame
from anim10 import Animation, Grid, Frame
from helpers import get_delta_time, process_events
from pygameframe import PyGameFrame
SCREEN_SIZE = (800, 600)
def display_text(canvas, message, x, y):
font = pygame.font.SysFont('Courier New', 12)
if len(message) > 0:
canvas.blit(font.render(message, 1, (255, 255, 255)), (x, y))
class MoveDirection(Enum):
left = 0
right = 1
up = 2
down = 3
def main(argv):
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('anim10.py - witch')
canvas = pygame.Surface((400, 300))
player_x = canvas.get_width() // 2 - 16
player_y = canvas.get_height() // 2 - 16
player_speed = 100
image = pygame.image.load('media/witch.png')
grid = Grid(PyGameFrame, 32, 32, 384, 256)
move = {
MoveDirection.down: Animation(grid('1-3', 1, 2, 1), 0.15),
MoveDirection.up: Animation(grid('1-3', 4, 2, 4), 0.15),
MoveDirection.left: Animation(grid('1-3', 2, 2, 2), 0.15),
MoveDirection.right: Animation(grid('1-3', 3, 2, 3), 0.15),
}
last_direction = MoveDirection.down
terminated = False
while not terminated:
terminated = process_events()
dt = get_delta_time()
keyboard_state = pygame.key.get_pressed()
moving = False
if keyboard_state[pygame.K_UP]:
last_direction = MoveDirection.up
moving = True
if keyboard_state[pygame.K_DOWN]:
last_direction = MoveDirection.down
moving = True
if keyboard_state[pygame.K_LEFT]:
last_direction = MoveDirection.left
moving = True
if keyboard_state[pygame.K_RIGHT]:
last_direction = MoveDirection.right
moving = True
if moving:
move[last_direction].update(dt)
if last_direction == MoveDirection.down:
player_y += player_speed * dt
elif last_direction == MoveDirection.up:
player_y -= player_speed * dt
elif last_direction == MoveDirection.left:
player_x -= player_speed * dt
elif last_direction == MoveDirection.right:
player_x += player_speed * dt
screen.fill((0, 0, 0, 255))
canvas.fill((0, 0, 0, 255))
move[last_direction].draw(canvas, image, player_x, player_y)
display_text(canvas, 'Press up/down/left/right arrows', 10, 10)
display_text(canvas, 'to control the witch.', 10, 30)
output = pygame.transform.scale(canvas, SCREEN_SIZE)
screen.blit(output, (0, 0))
pygame.display.update()
time.sleep(0.001)
if __name__ == '__main__':
sys.exit(main(sys.argv))