-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_transforming_surfaces.py
103 lines (78 loc) · 3.52 KB
/
12_transforming_surfaces.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
import pygame
from sys import exit
def display_score():
current_time = int(pygame.time.get_ticks() / 1000) - start_time
score_surf = test_font.render(f'Score: {current_time}', False, (64, 64, 64))
score_rect = score_surf.get_rect(center = (400, 50))
screen.blit(score_surf, score_rect)
return current_time
# Variables
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
clock = pygame.time.Clock()
test_font = pygame.font.Font('font/Pixeltype.ttf', 50)
game_active = False
start_time = 0
score = 0
sky_surface = pygame.image.load('graphics/Sky.png').convert()
ground_surface = pygame.image.load('graphics/ground.png').convert()
# score_surf = test_font.render('Hello World', False, (64, 64, 64))
# score_rect = score_surf.get_rect(center = (400, 50))
snail_surf = pygame.image.load('graphics/snail/snail1.png').convert_alpha()
snail_rect = snail_surf.get_rect(bottomright = (600, 300))
player_surf = pygame.image.load('graphics/player/player_walk_1.png').convert_alpha()
player_rect = player_surf.get_rect(midbottom = (80, 300))
player_gravity = 0
# Intro screen
player_stand = pygame.image.load('graphics/player/player_stand.png').convert_alpha()
player_stand = pygame.transform.scale2x(player_stand) # Agrandar el player
player_stand_rect = player_stand.get_rect(center = (400, 200))
game_name = test_font.render('Pixel Runner', False, '#74D1B9')
game_name_rect = game_name.get_rect(center = (400, 80))
game_message = test_font.render('Press space to run', False, '#74D1B9')
game_message_rect = game_message.get_rect(center = (400, 340))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if game_active:
if event.type == pygame.MOUSEBUTTONDOWN:
if player_rect.collidepoint(event.pos) and player_rect.bottom >= 300:
player_gravity = -20
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and player_rect.bottom >= 300:
player_gravity = -20
else:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
game_active = True
snail_rect.left = 800
start_time = int(pygame.time.get_ticks() / 1000)
if game_active:
screen.blit(sky_surface, (0, 0))
screen.blit(ground_surface, (0, 300))
# pygame.draw.rect(screen, '#c0e8ec', score_rect)
# screen.blit(score_surf, score_rect)
score = display_score()
snail_rect.x -= 5
if snail_rect.right <= 0: snail_rect.left = 800
screen.blit(snail_surf, snail_rect)
# Player
player_gravity += 1
player_rect.y += player_gravity
if player_rect.bottom >= 300: player_rect.bottom = 300
screen.blit(player_surf, player_rect)
# Collision
if snail_rect.colliderect(player_rect):
game_active = False
else:
screen.fill('#333333')
screen.blit(player_stand, player_stand_rect)
score_message = test_font.render(f'Your score: {score}', False, '#74D1B9')
score_message_rect = score_message.get_rect(center = (400, 330))
screen.blit(game_name, game_name_rect)
if score == 0: screen.blit(game_message, game_message_rect)
else: screen.blit(score_message, score_message_rect)
pygame.display.update()
clock.tick(60)