-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalcAll.py
More file actions
74 lines (58 loc) · 2.12 KB
/
Copy pathCalcAll.py
File metadata and controls
74 lines (58 loc) · 2.12 KB
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
import pygame
from random import randint
from math import cos
# Preparing method for splitting sprite sheet into correct parts
def prepare_sheet(directory, width, height):
slices = []
sheet = pygame.image.load(directory).convert_alpha()
for i in range(sheet.get_width() // width):
image = pygame.Surface((width, height + 8), pygame.SRCALPHA)
image.blit(sheet, (0, 0), (i * width, 0, (i + 1) * width, height))
slices.append(image)
return slices
# Preparing method for setting up grass, based on screen dimensions
def prepare_grass(width, height, density=10):
grass = []
for w in range(width // density):
for h in range(height // density):
sprite = randint(0, 11)
position = w * density - randint(2, 8) + 5, h * density - randint(2, 8) + 5
step = int(position[0] + 0.3 * position[1])
blade = {'sprite': sprite,
'position': position,
'step': step}
grass.append(blade)
return grass
# Initialising game
pygame.init()
X, Y = 500, 500
screen = pygame.display.set_mode((X, Y), pygame.RESIZABLE)
clock = pygame.time.Clock()
pygame.display.set_caption('Moving Grass')
# Preparing Grass
Grass = prepare_grass(X, Y)
# Unpacking sprites
sprites = prepare_sheet('Grass.png', 5, 10)
# Main loop
while True:
# Checking events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.VIDEORESIZE:
X, Y = pygame.display.get_surface().get_size()
Grass = prepare_grass(X, Y)
screen.fill((47, 131, 39))
# Drawing each blade of grass
for blade in Grass:
blade['step'] += 2
angle = 45 * (cos(blade['step'] / 40) - 0.4)
image = sprites[blade['sprite']]
image = pygame.transform.rotate(image, angle)
image = pygame.transform.scale_by(image, (4, 4))
x, y = tuple(blade['position'])
x, y = x - 0.5 * image.get_width(), y - 0.5 * image.get_height()
screen.blit(image, (x, y))
pygame.display.flip()
clock.tick(60)