-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPygame.py
121 lines (98 loc) · 2.57 KB
/
Pygame.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
import pygame, random
pygame.init()
#CONSTANTS
SCREEN_WIDTH = 500 #1820
SCREEN_HEIGHT = 500 #980
GRAVITY = 1
SPEED = 10
ACCELERATION = 5
DEACCELERATION = 2
#VARIABLES
xVelocity = 0
yVelocity = 0
colorChange = False
red = 0
blue = 0
green = 0
canJump = True
#SETUP
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
player = pygame.Rect((300, 250, 50, 50))
rec2 = pygame.Rect((400, SCREEN_HEIGHT - 10, 50, 50))
rec3 = pygame.Rect((0, SCREEN_HEIGHT - 400, 10, 400))
#INTITIALIZATION
run = True
while run:
#Drawing rectangles and screen fill
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (red, blue, green), player)
pygame.draw.rect(screen, (red, blue, green), rec2)
pygame.draw.rect(screen, (red, blue, green), rec3)
if xVelocity < 0:
xVelocity += DEACCELERATION
elif xVelocity > 0:
xVelocity -= DEACCELERATION
#Key events
key = pygame.key.get_pressed()
if key[pygame.K_a] and player.left > 0:
xVelocity -= ACCELERATION
if key[pygame.K_d] and player.right < SCREEN_WIDTH:
xVelocity += ACCELERATION
if key[pygame.K_w] and canJump:
yVelocity = -25
canJump = False
if xVelocity > SPEED:
xVelocity = SPEED
elif xVelocity < -SPEED:
xVelocity = -SPEED
yVelocity += GRAVITY
if key[pygame.K_a] and player.left == 0:
xVelocity = 0
yVelocity = 0
canJump = True
elif key[pygame.K_d] and player.right == SCREEN_WIDTH:
xVelocity = 0
yVelocity = 0
canJump = True
#Ceiling
if player.top + yVelocity < 0:
player.top = 0
yVelocity = 0
canJump = True
#Floor
if player.bottom != SCREEN_HEIGHT:
colorChange = True
if player.bottom + yVelocity > SCREEN_HEIGHT:
player.bottom = SCREEN_HEIGHT
yVelocity = 0
if player.bottom == SCREEN_HEIGHT and colorChange == True:
red = random.randint(0, 255)
blue = random.randint(0, 255)
green = random.randint(0, 255)
colorChange = False
canJump = True
#Trampoline
if pygame.Rect.colliderect(player, rec2):
yVelocity = -30
#Slide
if pygame.Rect.colliderect(player, rec3) and player.bottom != SCREEN_HEIGHT:
yVelocity = 1
CanJump = True
#Wall Collision Check
if player.left + xVelocity < 0:
player.left = 0
xVelocity = 0
elif player.right + xVelocity > SCREEN_WIDTH:
player.right = SCREEN_WIDTH
xVelocity = 0
#Movement
player.move_ip(xVelocity, yVelocity)
#Game quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#FPS and display refresh
clock.tick(60)
pygame.display.update()
pygame.quit()