-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
83 lines (61 loc) · 1.87 KB
/
init.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
# Quick-and-dirty pygame demo of a camera that scrolls to follow
# a player. Please excuse the horrible graphics. WASD to move.
import pygame
# Actual size of the game map
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 1000
# Dimensions of the part you can see
VIEW_WIDTH = 500
VIEW_HEIGHT = 500
pygame.init()
screen = pygame.display.set_mode([VIEW_WIDTH,VIEW_HEIGHT])
clock = pygame.time.Clock()
keymap = {
'up': pygame.K_w,
'down': pygame.K_s,
'left': pygame.K_a,
'right': pygame.K_d
}
# Scale the map image to the desired dimensions; this distorts it a bit.
# Would be better to have an image of the desired size already
bg = pygame.transform.scale(
pygame.image.load('images/bg.png'), (SCREEN_WIDTH, SCREEN_HEIGHT))
# Player sprite
class Sprite(pygame.sprite.Sprite):
def __init__(self,x,y,speed):
super().__init__()
self.x = x
self.y = y
self.speed = speed
def update(self, keys):
'''Basic movement.'''
if keys[keymap['up']]:
self.y -= self.speed
if keys[keymap['down']]:
self.y += self.speed
if keys[keymap['right']]:
self.x += self.speed
if keys[keymap['left']]:
self.x -= self.speed
def render(self, surf):
''' Sprite is represented as a circle for simplicity.'''
pygame.draw.circle(surf, (255,0,0), (self.x, self.y), 5)
player = Sprite(500, 500, 10)
# A rectangle to represent the part of the map you can see
camera = pygame.Rect(0,0, VIEW_WIDTH,VIEW_HEIGHT)
camera.center = (player.x, player.y)
# Main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
camera.center = (player.x, player.y)
surf = bg.copy()
keys = pygame.key.get_pressed()
player.render(surf)
player.update(keys)
# Draw the part of the map that you can see (as defined by the camera rect)
# onto the screen
screen.blit(surf, (0,0), camera)
pygame.display.flip()
clock.tick(60)