-
Notifications
You must be signed in to change notification settings - Fork 75
/
eight_dir_move.py
executable file
·133 lines (110 loc) · 3.62 KB
/
eight_dir_move.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#! /usr/bin/env python
"""
This script implements a basic sprite that can move in all 8 directions.
-Written by Sean J. McKiernan 'Mekire'
"""
import os
import sys
import pygame as pg
CAPTION = "Move me with the Arrow Keys"
SCREEN_SIZE = (500, 500)
TRANSPARENT = (0, 0, 0, 0)
# This global constant serves as a very useful convenience for me.
DIRECT_DICT = {pg.K_LEFT : (-1, 0),
pg.K_RIGHT : ( 1, 0),
pg.K_UP : ( 0,-1),
pg.K_DOWN : ( 0, 1)}
class Player(object):
"""
This class will represent our user controlled character.
"""
SIZE = (100, 100)
def __init__(self, pos, speed):
"""
The pos argument is a tuple for the center of the player (x,y);
speed is given in pixels/frame.
"""
self.rect = pg.Rect((0,0), Player.SIZE)
self.rect.center = pos
self.speed = speed
self.image = self.make_image()
def make_image(self):
"""
Creates our hero (a red circle with a black outline).
"""
image = pg.Surface(self.rect.size).convert_alpha()
image.fill(TRANSPARENT)
image_rect = image.get_rect()
pg.draw.ellipse(image, pg.Color("black"), image_rect)
pg.draw.ellipse(image, pg.Color("red"), image_rect.inflate(-12, -12))
return image
def update(self, keys, screen_rect):
"""
Updates our player appropriately every frame.
"""
for key in DIRECT_DICT:
if keys[key]:
self.rect.x += DIRECT_DICT[key][0]*self.speed
self.rect.y += DIRECT_DICT[key][1]*self.speed
self.rect.clamp_ip(screen_rect) # Keep player on screen.
def draw(self, surface):
"""
Blit image to the target surface.
"""
surface.blit(self.image, self.rect)
class App(object):
"""
A class to manage our event, game loop, and overall program flow.
"""
def __init__(self):
"""
Get a reference to the display surface; set up required attributes;
and create a Player instance.
"""
self.screen = pg.display.get_surface()
self.screen_rect = self.screen.get_rect()
self.clock = pg.time.Clock()
self.fps = 60
self.done = False
self.keys = pg.key.get_pressed()
self.player = Player(self.screen_rect.center, 5)
def event_loop(self):
"""
One event loop. Never cut your game off from the event loop.
Your OS may decide your program has hung if the event queue is not
accessed for a prolonged period of time.
"""
for event in pg.event.get():
if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
self.done = True
elif event.type in (pg.KEYUP, pg.KEYDOWN):
self.keys = pg.key.get_pressed()
def render(self):
"""
Perform all necessary drawing and update the screen.
"""
self.screen.fill(pg.Color("white"))
self.player.draw(self.screen)
pg.display.update()
def main_loop(self):
"""
One game loop. Simple and clean.
"""
while not self.done:
self.event_loop()
self.player.update(self.keys, self.screen_rect)
self.render()
self.clock.tick(self.fps)
def main():
"""
Prepare our environment, create a display, and start the program.
"""
os.environ['SDL_VIDEO_CENTERED'] = '1'
pg.init()
pg.display.set_caption(CAPTION)
pg.display.set_mode(SCREEN_SIZE)
App().main_loop()
pg.quit()
sys.exit()
if __name__ == "__main__":
main()