-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·131 lines (94 loc) · 3.19 KB
/
main.py
File metadata and controls
executable file
·131 lines (94 loc) · 3.19 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Basic game')
game_stat = ''
gravity = 1
difficulty = 1
player_img = pygame.image.load("jogging.png")
playerY = 286
playerX = 50
going = ''
t = 0
obstacleX = []
obstacleY = 300
obstacleX_change = 0.3
num_of_obstacle = 3
for i in range(num_of_obstacle):
obstacleX.append(random.randint(800 * (i + 1) - 20, 800 * (i + 2)))
# to check if character is already jumping
pressed = False
def player(x, y):
screen.blit(player_img, (x, y))
def obstacle(x, y):
pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(x, y, 20, 50))
def game_over_text():
over_font = pygame.font.Font('freesansbold.ttf', 64)
game_over = over_font.render('GAME OVER', True, (255, 255, 255))
screen.blit(game_over, (200, 250))
def is_collision(x1, y1, x2, y2):
if x2 - 55 <= x1 <= x2 + 1 and y1 >= y2 - 60:
return True
else:
return False
running = True
while running:
# making background
screen.fill((135, 206, 235))
pygame.draw.rect(screen, (156, 52, 0), pygame.Rect(0, 350, 800, 300))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
# checking if space bar is pressed
if event.key == pygame.K_SPACE:
if not pressed:
pressed = True
going = 'up'
# check if game is still going on
if game_stat == 'lost':
game_over_text()
else:
# Displaying and managing obstacles
for i in range(num_of_obstacle):
obstacle(obstacleX[i], 300)
obstacleX[i] -= obstacleX_change
# Re-displaying obstacles when they cross screen
if obstacleX[i] <= -20:
obstacleX[i] = random.randint(800 * (i + 1) + 20, 800 * (i + 2) - 40)
# Check for Collisions
if is_collision(playerX, playerY, obstacleX[i], obstacleY):
game_stat = 'lost'
# Assignment 3:
if not gravity:
playerY_change = (1 - 2 * obstacleX_change) / 2
if pressed and playerY > 180 and going == 'up':
playerY -= playerY_change
if playerY <= 180:
going = ''
t = 0
if pressed and playerY >= 286:
pressed = False
elif pressed and going == '':
playerY += playerY_change
else:
playerY_change = (1.2 - 2 * obstacleX_change) * t
max_speed = ((2 * (1.2 - 2 * obstacleX_change) * 100)/1000) ** (1/2)
if pressed and playerY > 180 and going == 'up':
playerY -= (max_speed - playerY_change)
if pressed and playerY <= 180:
going = ''
t = 0
if pressed and playerY > 286:
pressed = False
t = 0
elif pressed and going == '':
playerY += playerY_change
if pressed:
t += 0.001
else:
t = 0
# Displaying player
player(playerX, playerY)
pygame.display.update()