-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.py
140 lines (110 loc) · 4.94 KB
/
sprites.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import pygame
from settings import *
from random import choice, randint
class BackGround(pygame.sprite.Sprite):
def __init__(self, groups, scale_factor):
super().__init__(groups)
bg_image = pygame.image.load("graphics/environment/simple.png").convert()
full_height = bg_image.get_height() * scale_factor
full_width = bg_image.get_width() * scale_factor
full_sized_image = pygame.transform.scale(bg_image,(full_width, full_height))
self.image = pygame.transform.scale(bg_image, ((full_width * 2), full_height))
self.image.blit(full_sized_image, (0,0))
self.image.blit(full_sized_image, (full_width, 0))
self.rect = self.image.get_rect(topleft=(0,0))
self.pos = pygame.math.Vector2(self.rect.topleft)
def update(self, dt):
self.pos.x -= 300 * dt
if self.rect.centerx <= 0:
self.pos.x = 0
self.rect.x = round(self.pos.x)
class Ground(pygame.sprite.Sprite):
def __init__(self, groups, scale_factor, position = "bottom"):
super().__init__(groups)
ground_surf = pygame.image.load("graphics/environment/ground.png").convert_alpha()
self.image = pygame.transform.scale(ground_surf, pygame.math.Vector2(ground_surf.get_size())*scale_factor)
self.sprite_type = "ground"
# self.rect = self.image.get_rect(bottomleft = (0,WINDOW_HEIGHT))
if position == 'top':
self.image = pygame.transform.flip(self.image, False, True) # Flip the image vertically
self.rect = self.image.get_rect(topleft=(0, 0))
else: # Default to bottom if anything else is specified
self.rect = self.image.get_rect(bottomleft=(0, WINDOW_HEIGHT))
self.pos = pygame.math.Vector2(self.rect.topleft)
self.mask = pygame.mask.from_surface(self.image)
def update(self, dt):
self.pos.x -= 360 * dt
if self.rect.centerx <= 0:
self.pos.x = 0
self.rect.x = round(self.pos.x)
class Plane(pygame.sprite.Sprite):
def __init__(self, groups, scale_factor):
super().__init__(groups)
#image
self.import_frames(scale_factor)
self.frame_index = 0
self.image = self.frames[self.frame_index]
#rect
self.rect = self.image.get_rect(midleft = (WINDOW_WIDTH / 20,WINDOW_HEIGHT /2))
self.pos = pygame.math.Vector2(self.rect.topleft)
#movement
self.gravity = 666
self.direction = 0
self.mask = pygame.mask.from_surface(self.image)
self.jump_sound = pygame.mixer.Sound('graphics/sounds/jump.wav')
self.jump_sound.set_volume(0.1)
def import_frames(self,scale_factor):
self.frames = []
for i in range(3):
# orange0
surf = pygame.image.load(f'graphics/plane/orange{i}.png').convert_alpha()
scaled_surface = pygame.transform.scale(surf, pygame.math.Vector2(surf.get_size())*scale_factor)
self.frames.append(scaled_surface)
def apply_gravity(self, dt):
self.direction += self.gravity * dt
self.pos.y += self.direction * dt
self.rect.y = round(self.pos.y)
def jump(self):
self.jump_sound.play()
self.direction = -400
def animate(self, dt):
self.frame_index += 13 * dt
if self.frame_index >= len(self.frames):
self.frame_index = 0
self.image = self.frames[int(self.frame_index)]
def rotate(self):
rotated_plane = pygame.transform.rotozoom(self.image,-self.direction*.05,1)
self.image = rotated_plane
self.mask = pygame.mask.from_surface(self.image)
def update(self, dt):
self.apply_gravity(dt)
self.animate(dt)
self.rotate()
class Obstacle(pygame.sprite.Sprite):
def __init__(self, groups, scale_factor):
super().__init__(groups)
orientation = choice(('up','down'))
image_choice = choice((0,1,2)) # Choose the image
surf = pygame.image.load(f'graphics/obstacles/{image_choice}.png').convert_alpha()
if image_choice == 2:
chosen_scale_factor = 0.4
else:
chosen_scale_factor = scale_factor * 2.1
self.image = pygame.transform.scale(surf,pygame.math.Vector2(surf.get_size())*chosen_scale_factor)
self.sprite_type = "obstacle"
self.passed = False
x = WINDOW_WIDTH + randint(40,100)
if orientation == 'up':
y = WINDOW_HEIGHT + randint(10,50)
self.rect = self.image.get_rect(midbottom = (x,y))
else:
y = randint(-50,-10)
self.image = pygame.transform.flip(self.image, False, True)
self.rect = self.image.get_rect(midtop = (x,y))
self.pos = pygame.math.Vector2(self.rect.topleft)
self.mask = pygame.mask.from_surface(self.image)
def update(self, dt):
self.pos.x -= 400 * dt
self.rect.x = round(self.pos.x)
if self.rect.right <= -100:
self.kill()