Skip to content

Commit 87a9264

Browse files
committed
rozne
1 parent 63002d5 commit 87a9264

8 files changed

Lines changed: 568 additions & 0 deletions

File tree

pygame/vector2/example-1.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python3
2+
3+
import pygame
4+
5+
# --- constants ---
6+
7+
WIDTH = 1200
8+
HEIGHT = 1200
9+
10+
FPS = 25
11+
12+
BLACK = (0, 0, 0)
13+
WHITE = (250, 250, 250)
14+
15+
BULLET_SPEED = 5
16+
17+
# --- classes ---
18+
19+
#empty
20+
21+
# --- functions ---
22+
23+
#empty
24+
25+
# --- main ---
26+
27+
# - init -
28+
29+
pygame.init()
30+
31+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
32+
33+
# - objects -
34+
35+
bullets = []
36+
37+
# center of screen as Vector2
38+
#center = pygame.math.Vector2(WIDTH//2, HEIGHT//2)
39+
center = pygame.math.Vector2(screen.get_rect().center)
40+
41+
# - mainloop -
42+
43+
clock = pygame.time.Clock()
44+
45+
running = True
46+
47+
while running:
48+
screen.fill(BLACK)
49+
50+
for event in pygame.event.get():
51+
if event.type == pygame.QUIT:
52+
running = False
53+
54+
elif event.type == pygame.KEYDOWN:
55+
if event.key == pygame.K_ESCAPE:
56+
running = False
57+
58+
elif event.type== pygame.MOUSEBUTTONDOWN:
59+
if event.button == 1:
60+
61+
# get vector from center to mouse position
62+
vector = event.pos - center
63+
# `center` is `Vector2` so `vector` will be `Vector2` too
64+
#print(type(vector))
65+
66+
# normalize
67+
normal = vector.normalize()
68+
69+
# create speed vector
70+
speed = normal * BULLET_SPEED
71+
72+
# move object (first move 15 times bigger then next moves )
73+
pos = center + (speed * 15)
74+
75+
previous = pygame.math.Vector2(center)
76+
77+
#pygame.draw.line(screen, (255,150,100), (pos.x, pos.y), (center.x, center.y))
78+
pygame.draw.line(screen, (255,150,100), pos, previous)
79+
80+
# remeber position and speed as one object
81+
bullets.append( [pos, speed, previous] )
82+
83+
# - draws -
84+
85+
for pos, speed, previous in bullets:
86+
previous.x = pos.x
87+
previous.y = pos.y
88+
89+
# move
90+
pos += speed
91+
92+
# draw
93+
#pygame.draw.line(screen, (255,150,100), pos, previous)
94+
pygame.draw.rect(screen, WHITE, (pos.x, pos.y, 2, 2))
95+
96+
pygame.display.update()
97+
98+
# - speed -
99+
100+
clock.tick(FPS)
101+
102+
# - end -
103+
104+
pygame.quit()
105+
quit()
106+

pygame/vector2/example-1b.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env python3
2+
3+
import pygame
4+
5+
# --- constants ---
6+
7+
WIDTH = 800
8+
HEIGHT = 800
9+
10+
FPS = 25
11+
12+
BLACK = (0, 0, 0)
13+
WHITE = (250, 250, 250)
14+
15+
BULLET_SPEED = 5
16+
17+
# --- classes ---
18+
19+
#empty
20+
21+
# --- functions ---
22+
23+
#empty
24+
25+
# --- main ---
26+
27+
# - init -
28+
29+
pygame.init()
30+
31+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
32+
screen_rect = screen.get_rect()
33+
34+
# - objects -
35+
36+
bullets = []
37+
38+
# center of screen as Vector2
39+
#center = pygame.math.Vector2(WIDTH//2, HEIGHT//2)
40+
center = pygame.math.Vector2(screen.get_rect().center)
41+
42+
shot = False
43+
44+
# - mainloop -
45+
46+
clock = pygame.time.Clock()
47+
48+
running = True
49+
50+
while running:
51+
52+
for event in pygame.event.get():
53+
if event.type == pygame.QUIT:
54+
running = False
55+
56+
elif event.type == pygame.KEYDOWN:
57+
if event.key == pygame.K_ESCAPE:
58+
running = False
59+
60+
elif event.type== pygame.MOUSEBUTTONDOWN:
61+
if event.button == 1:
62+
shot = event.pos
63+
64+
elif event.type== pygame.MOUSEBUTTONUP:
65+
if event.button == 1:
66+
shot = None
67+
68+
elif event.type== pygame.MOUSEMOTION:
69+
if shot:
70+
shot = event.pos
71+
72+
73+
# create new bullet
74+
if shot:
75+
# get vector from center to mouse position
76+
vector = shot - center
77+
# `center` is `Vector2` so `vector` will be `Vector2` too
78+
#print(type(vector))
79+
80+
# normalize
81+
normal = vector.normalize()
82+
83+
# create speed vector
84+
speed = normal * BULLET_SPEED
85+
86+
# move object (first move 15 times bigger then next moves )
87+
pos = center# + (speed * 15)
88+
pos = pygame.math.Vector2(center)
89+
90+
previous = pygame.math.Vector2(center)
91+
92+
#pygame.draw.line(screen, (255,150,100), (pos.x, pos.y), (center.x, center.y))
93+
pygame.draw.line(screen, (255,150,100), pos, previous)
94+
95+
# remeber position and speed as one object
96+
bullets.append( [pos, speed, previous] )
97+
98+
# - updates (without draws) -
99+
100+
new_bullets = []
101+
102+
for pos, speed, previous in bullets:
103+
#previous.x = pos.x
104+
#previous.y = pos.y
105+
106+
# move
107+
pos += speed
108+
109+
if screen_rect.collidepoint(pos):
110+
new_bullets.append( [pos, speed, previous] )
111+
112+
bullets = new_bullets
113+
114+
# - draws -
115+
116+
screen.fill(BLACK)
117+
118+
for pos, speed, previous in bullets:
119+
# draw
120+
#pygame.draw.line(screen, (255,150,100), pos, previous)
121+
pygame.draw.rect(screen, WHITE, (pos.x, pos.y, 2, 2))
122+
123+
pygame.display.update()
124+
125+
# - speed -
126+
127+
clock.tick(FPS)
128+
129+
# - end -
130+
131+
pygame.quit()
132+
quit()
133+

pygame/vector2/example-2.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
with line which shows way
5+
'''
6+
7+
import pygame
8+
9+
# --- constants ---
10+
11+
WIDTH = 800
12+
HEIGHT = 800
13+
14+
FPS = 25
15+
16+
BLACK = (0, 0, 0)
17+
WHITE = (255, 255, 255)
18+
RED = (255, 0, 0)
19+
GREEN = (0, 255, 0)
20+
BLUE = (0, 0, 255)
21+
22+
BULLET_SPEED = 15
23+
24+
# --- classes ---
25+
26+
#empty
27+
28+
# --- functions ---
29+
30+
#empty
31+
32+
# --- main ---
33+
34+
# - init -
35+
36+
pygame.init()
37+
38+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
39+
screen_rect = screen.get_rect()
40+
41+
# - objects -
42+
43+
bullets = []
44+
45+
# center of screen as Vector2
46+
#center = pygame.math.Vector2(WIDTH//2, HEIGHT//2)
47+
center = pygame.math.Vector2(screen_rect.center)
48+
49+
# - mainloop -
50+
51+
clock = pygame.time.Clock()
52+
53+
while True:
54+
55+
for event in pygame.event.get():
56+
if event.type == pygame.QUIT:
57+
pygame.quit()
58+
quit()
59+
60+
if event.type== pygame.MOUSEBUTTONDOWN:
61+
if event.button == 1:
62+
63+
# get vector from center to mouse position
64+
vector = event.pos - center
65+
# `center` is `Vector2` so `vector` will be `Vector2` too
66+
#print(type(vector))
67+
68+
# normalize
69+
normal = vector.normalize()
70+
# create speed vector
71+
speed = normal * BULLET_SPEED
72+
73+
# move object (first move 5 times bigger then next moves )
74+
#pos = center + (speed * 15)
75+
#pos = center + speed
76+
pos = pygame.math.Vector2(center)
77+
78+
previous = pygame.math.Vector2(center)
79+
80+
# remeber position and speed as one object
81+
bullets.append( [pos, speed, previous] )
82+
83+
# - updates (without draws)-
84+
85+
existing_bullets = []
86+
87+
for pos, speed, previous in bullets:
88+
# copy from `pos` to `previous`
89+
# can't do `previous = pos`
90+
previous.x = pos.x
91+
previous.y = pos.y
92+
93+
# move
94+
pos += speed
95+
96+
# check if bullet still on screen
97+
if screen_rect.collidepoint(pos):
98+
existing_bullets.append( [pos, speed, previous] )
99+
100+
bullets = existing_bullets
101+
102+
# - draws (without updates)-
103+
104+
screen.fill(BLACK)
105+
106+
for pos, speed, previous in bullets:
107+
# draw
108+
pygame.draw.line(screen, GREEN, pos, previous)
109+
pygame.draw.rect(screen, WHITE, (pos.x-3, pos.y-3, 6, 6))
110+
111+
pygame.display.update()
112+
113+
# - speed -
114+
115+
clock.tick(FPS)
116+
117+
# - end -
118+
119+

requests/ungzip-data/example-3.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
3+
import urllib.request
4+
import io
5+
import gzip
6+
7+
url = 'http://httpbin.org/gzip'
8+
9+
r = urllib.request.urlopen(url)
10+
11+
bytes_compressed = r.read()
12+
13+
mem_file = io.BytesIO(bytes_compressed)
14+
15+
gzip_file = gzip.GzipFile(fileobj=mem_file)
16+
17+
bytes_uncompressed = gzip_file.read()
18+
19+
print(bytes_uncompressed.decode('utf-8'))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
It is example how `after` and `mainloop` can work.

0 commit comments

Comments
 (0)