-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicPygame1.py
More file actions
114 lines (88 loc) · 2.81 KB
/
BasicPygame1.py
File metadata and controls
114 lines (88 loc) · 2.81 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
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
import pygame
import random
import time
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Juego de Plataforma: Desafíos de Código')
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
clock = pygame.time.Clock()
font = pygame.font.SysFont('Arial', 24)
player_width = 50
player_height = 60
player_x = 100
player_y = 500
player_vel = 5
player_jump = 10
player_is_jumping = False
player_jump_count = 10
ground_height = 50
platforms = [(0, SCREEN_HEIGHT - ground_height, SCREEN_WIDTH, ground_height)]
def desafio_programacion():
print("¡Desafío de código!")
print("Resuelve el siguiente reto para continuar:")
print("¿Cuál es el resultado de 5 + 3?")
respuesta = input("Escribe tu respuesta: ")
if respuesta == "8":
return True
else:
return False
player = pygame.Rect(player_x, player_y, player_width, player_height)
def draw_player(player):
pygame.draw.rect(screen, GREEN, player)
def move_player(keys, player):
if keys[pygame.K_LEFT]:
player.x -= player_vel
if keys[pygame.K_RIGHT]:
player.x += player_vel
if not player_is_jumping:
if keys[pygame.K_SPACE]:
jump(player)
def jump(player):
global player_is_jumping, player_jump_count
player_is_jumping = True
player.y -= player_jump_count * 2
def apply_gravity(player):
global player_is_jumping, player_jump_count
if player_is_jumping:
if player_jump_count >= -10:
neg = 1
if player_jump_count < 0:
neg = -1
player.y -= (player_jump_count ** 2) * 0.4 * neg
player_jump_count -= 1
else:
player_is_jumping = False
player_jump_count = 10
def draw_platforms():
for platform in platforms:
pygame.draw.rect(screen, BLACK, pygame.Rect(platform))
def game_loop():
global player_x, player_y
run = True
while run:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
move_player(keys, player)
apply_gravity(player)
draw_platforms()
for platform in platforms:
if player.colliderect(pygame.Rect(platform)):
player.y = platform[1] - player.height
player_is_jumping = False
player_jump_count = 10
draw_player(player)
if player.x > 500 and player.x < 600:
if desafio_programacion():
platforms.append((600, SCREEN_HEIGHT - ground_height, SCREEN_WIDTH, ground_height))
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()