Skip to content

Commit 816e3fe

Browse files
committed
pygame
1 parent 8b7ae06 commit 816e3fe

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
author: https://blog.furas.pl
3+
4+
date: 2020.07.16
5+
6+
link: https://stackoverflow.com/questions/62940130/how-to-implement-a-scroll-view-in-pygame/
7+
8+
---
9+
10+
Code moves images when mouse is moved (without clicking button)
11+
12+
`camera_rect` keeps `offset` used to move/positioning image.
13+
14+
Event `MOUSEMOTION` and `event.rel` is used to change value in `camera_rect`.
15+
16+
It was created for image `800x600` and window `400x300` and it moves full image
17+
but for different sizes it may need different calculations -
18+
or it may need to calculate mouse position as percentage
19+
and use this percentage to calculate image position.
Loading
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
# author: https://blog.furas.pl
3+
# date: 2020.07.16
4+
# link: https://stackoverflow.com/questions/62940130/how-to-implement-a-scroll-view-in-pygame/
5+
6+
import pygame
7+
8+
# === CONSTANS === (UPPER_CASE names)
9+
10+
#BLACK = ( 0, 0, 0)
11+
#WHITE = (255, 255, 255)
12+
13+
#RED = (255, 0, 0)
14+
#GREEN = ( 0, 255, 0)
15+
#BLUE = ( 0, 0, 255)
16+
17+
SCREEN_WIDTH = 400
18+
SCREEN_HEIGHT = 300
19+
20+
# === CLASSES === (CamelCase names)
21+
22+
# empty
23+
24+
# === FUNCTIONS === (lower_case names)
25+
26+
# empty
27+
28+
# === MAIN === (lower_case names)
29+
30+
# --- (global) variables ---
31+
32+
# --- init ---
33+
34+
pygame.init()
35+
36+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
37+
screen_rect = screen.get_rect()
38+
39+
# --- objects ---
40+
41+
image = pygame.image.load('image-800x600.jpg')
42+
image_rect = image.get_rect()
43+
44+
# --- camera / offset ---
45+
46+
camera_rect = screen_rect.copy()
47+
48+
# --- mainloop ---
49+
50+
#button_pressed = False
51+
52+
clock = pygame.time.Clock()
53+
is_running = True
54+
55+
while is_running:
56+
57+
# --- events ---
58+
59+
for event in pygame.event.get():
60+
61+
# --- global events ---
62+
63+
if event.type == pygame.QUIT:
64+
is_running = False
65+
66+
elif event.type == pygame.KEYDOWN:
67+
if event.key == pygame.K_ESCAPE:
68+
is_running = False
69+
70+
#elif event.type == pygame.MOUSEBUTTONDOWN:
71+
# button_pressed = True
72+
#elif event.type == pygame.MOUSEBUTTONUP:
73+
# button_pressed = False
74+
75+
elif event.type == pygame.MOUSEMOTION:
76+
#if button_pressed:
77+
78+
camera_rect.x += event.rel[0]
79+
# check left/right limit
80+
if camera_rect.left < image_rect.left:
81+
camera_rect.left = image_rect.left
82+
if camera_rect.right > image_rect.right:
83+
camera_rect.right = image_rect.right
84+
85+
camera_rect.y += event.rel[1]
86+
# check up/down limit
87+
if camera_rect.top < image_rect.top:
88+
camera_rect.top = image_rect.top
89+
if camera_rect.bottom > image_rect.bottom:
90+
camera_rect.bottom = image_rect.bottom
91+
92+
# --- objects events ---
93+
94+
# --- updates ---
95+
96+
97+
# --- draws ---
98+
99+
#screen.fill(BLACK)
100+
101+
# draw image
102+
screen.blit(image, (-camera_rect.x, -camera_rect.y))
103+
#screen.blit(image, camera_rect)
104+
105+
pygame.display.update()
106+
107+
# --- FPS ---
108+
109+
clock.tick(25)
110+
111+
# --- the end ---
112+
113+
pygame.quit()
114+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
# author: https://blog.furas.pl
3+
# date: 2020.07.16
4+
# link: https://stackoverflow.com/questions/62940130/how-to-implement-a-scroll-view-in-pygame/
5+
6+
import pygame
7+
8+
# === CONSTANS === (UPPER_CASE names)
9+
10+
#BLACK = ( 0, 0, 0)
11+
#WHITE = (255, 255, 255)
12+
13+
#RED = (255, 0, 0)
14+
#GREEN = ( 0, 255, 0)
15+
#BLUE = ( 0, 0, 255)
16+
17+
SCREEN_WIDTH = 400
18+
SCREEN_HEIGHT = 600
19+
20+
# === CLASSES === (CamelCase names)
21+
22+
# empty
23+
24+
# === FUNCTIONS === (lower_case names)
25+
26+
# empty
27+
28+
# === MAIN === (lower_case names)
29+
30+
# --- (global) variables ---
31+
32+
# --- init ---
33+
34+
pygame.init()
35+
36+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
37+
screen_rect = screen.get_rect()
38+
39+
# --- objects ---
40+
41+
image = pygame.image.load('image-800x600.jpg')
42+
image_rect = image.get_rect()
43+
44+
# --- camera / offset ---
45+
46+
camera_rect = screen_rect.copy()
47+
48+
percentage_x = 0
49+
percentage_y = 0
50+
51+
# --- mainloop ---
52+
53+
#button_pressed = False
54+
55+
clock = pygame.time.Clock()
56+
is_running = True
57+
58+
while is_running:
59+
60+
# --- events ---
61+
62+
for event in pygame.event.get():
63+
64+
# --- global events ---
65+
66+
if event.type == pygame.QUIT:
67+
is_running = False
68+
69+
elif event.type == pygame.KEYDOWN:
70+
if event.key == pygame.K_ESCAPE:
71+
is_running = False
72+
73+
elif event.type == pygame.MOUSEMOTION:
74+
percentage_x = event.pos[0] / screen_rect.width
75+
camera_rect.x = percentage_x * (image_rect.width - screen_rect.width)
76+
77+
#percentage_y = event.pos[1] / screen_rect.height
78+
#camera_rect.y = percentage_y * (image_rect.height - screen_rect.height)
79+
80+
# --- objects events ---
81+
82+
# empty
83+
84+
# --- updates ---
85+
86+
# empty
87+
88+
# --- draws ---
89+
90+
#screen.fill(BLACK)
91+
92+
# draw image
93+
screen.blit(image, (-camera_rect.x, -camera_rect.y))
94+
#screen.blit(image, camera_rect)
95+
96+
pygame.display.update()
97+
98+
# --- FPS ---
99+
100+
clock.tick(25)
101+
102+
# --- the end ---
103+
104+
pygame.quit()
105+

0 commit comments

Comments
 (0)