Skip to content

Commit a22ab7d

Browse files
committed
pygame
1 parent da931df commit a22ab7d

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

pygame/camera - 2/main.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python3
2+
3+
# date: 2020.01.16
4+
#
5+
6+
import pygame
7+
import random
8+
9+
# --- constants ---
10+
11+
RED = (255, 0, 0)
12+
GREEN = (0, 255, 0)
13+
BLUE = (0, 0, 255)
14+
COLORS = (RED, GREEN, BLUE)
15+
16+
TILE_SIZE = 50
17+
18+
# --- main ---
19+
20+
pygame.init()
21+
22+
# - window -
23+
screen = pygame.display.set_mode((800,600))
24+
screen_rect = screen.get_rect()
25+
26+
# - map - (with random rectangles)
27+
fullmap = pygame.Surface((2000, 1000))
28+
#for y in range(TILE_SIZE, 1000-TILE_SIZE, TILE_SIZE):
29+
# for x in range(TILE_SIZE, 2000-TILE_SIZE, TILE_SIZE):
30+
for y in range(0, 1000, TILE_SIZE):
31+
for x in range(0, 2000, TILE_SIZE):
32+
pygame.draw.rect(fullmap, random.choice(COLORS), (x, y, TILE_SIZE, TILE_SIZE))
33+
fullmap_rect = fullmap.get_rect()
34+
35+
# - icon(s) which not move -
36+
icon = pygame.Surface((100, 100))
37+
icon.fill((255,255,255))
38+
icon_rect = icon.get_rect()
39+
icon_rect.right = screen_rect.right - 10
40+
icon_rect.bottom = screen_rect.bottom - 10
41+
42+
# - camera -
43+
# `camera` is not `Surface` but only `Rect` with value/offset which I uses to cut map
44+
camera = screen.get_rect()
45+
46+
# --- loop ---
47+
running = True
48+
while running:
49+
50+
# - events -
51+
52+
for event in pygame.event.get():
53+
if event.type == pygame.QUIT:
54+
running = False
55+
if event.type == pygame.KEYDOWN:
56+
if event.key == pygame.K_ESCAPE:
57+
running = False
58+
59+
# - updates (without draws) -
60+
61+
keys = pygame.key.get_pressed()
62+
if keys[pygame.K_LEFT]:
63+
camera.x -= 5
64+
if camera.left < fullmap_rect.left:
65+
camera.left = fullmap_rect.left
66+
if keys[pygame.K_RIGHT]:
67+
camera.x += 5
68+
if camera.right > fullmap_rect.right:
69+
camera.right = fullmap_rect.right
70+
if keys[pygame.K_UP]:
71+
camera.y -= 5
72+
if camera.top < fullmap_rect.top:
73+
camera.top = fullmap_rect.top
74+
if keys[pygame.K_DOWN]:
75+
camera.y += 5
76+
if camera.bottom > fullmap_rect.bottom:
77+
camera.bottom = fullmap_rect.bottom
78+
79+
# - draws (without updates) -
80+
81+
# moving map - using `camera` to copy part of `fullmap` to `screen`
82+
screen.blit(fullmap, (0,0), camera)
83+
84+
# static icon(s)
85+
screen.blit(icon, icon_rect)
86+
87+
pygame.display.update()
88+
89+
# --- end ---
90+
pygame.quit()

0 commit comments

Comments
 (0)