-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (79 loc) · 2.77 KB
/
Copy pathmain.py
File metadata and controls
107 lines (79 loc) · 2.77 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
from typing import Any
import pygame
from sys import exit
pygame.init()
#variables
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner sample')
clock = pygame.time.Clock() #Helps to set the ceiling frame rate
font = pygame.font.Font(None, 50)
game_active = True
sky = pygame.image.load('graphics/Sky.png')
ground = pygame.image.load('graphics/ground.png')
text = font.render('Runner', True, 'Black')
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load('graphics/Player/player_walk_1.png').convert_alpha()
self.rect = self.image.get_rect(midbottom = (80, 300))
self.gravity = 0
def player_input(self):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.gravity = -20
def apply_gravity(self):
self.gravity += 1
self.rect.y += self.gravity
def update(self):
self.player_input()
self.apply_gravity()
class Snail(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load('graphics/snail/snail1.png').convert_alpha()
self.rect = self.image.get_rect(bottomright = (600, 300))
def respawn(self):
if self.rect.right <= 0:
self.rect.left = 800
def update(self):
self.rect.x -= 6
self.respawn()
snail = pygame.sprite.GroupSingle()
snail.add(Snail())
player = pygame.sprite.GroupSingle()
player.add(Player())
def collision():
if pygame.sprite.spritecollide(player.sprite,snail,False):
snail.empty()
return False
else:
return True
while True:
for event in pygame.event.get(): #allows the user to exit the game
if event.type == pygame.QUIT:
pygame.quit()
exit() #breaks the while loop, prevents conflict with pygame.init
if game_active:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player_gravity = -20
else:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
game_active = True
if game_active:
#get screens onto respectinve cords from top left
screen.blit(sky, (0, 0))
screen.blit(ground,(0, 300))
screen.blit(text, (320, 50))
snail.draw(screen)
snail.update()
player.draw(screen)
player.update()
game_active = collision()
else:
game_active = False
screen.fill((94,129,162))
gameover = font.render('Game Over - Press space to run', True, "Green")
screen.blit(gameover, (220, 50))
pygame.display.update()
clock.tick(60) #ceiling frame rate