-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaceimpact.py
119 lines (90 loc) · 2.99 KB
/
spaceimpact.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
import pygame
import time
from random import randrange, randint
import random
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pygame')
black = (0, 0, 0)
white = (255, 255, 255)
gold = (255,215,0)
car_width = 73
global my_enemies
clock = pygame.time.Clock()
carImg = pygame.image.load('resources/racecar.png')
enemyImg = pygame.image.load('resources/ship.png')
def car(x, y):
gameDisplay.blit(carImg, (x, y))
def enemy(enemyX, enemyY):
gameDisplay.blit(enemyImg, (enemyX, enemyY))
def text_objects(text, font):
textSurface = font.render(text, True, gold)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('You Crashed')
def game_loop():
x = (display_width/2)
y = (display_height-90)
enemyX = display_width/2
enemyY = -5
crashed = False
moving_right, moving_left, moving_down = True, False, True
x_change = 0
my_enemies = []
for i in range(5):
e_x = random.randrange(0, display_width)
e_y = random.randrange(-300, -200)
my_enemies.append([e_x, e_y])
while crashed == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
pygame.quit()
quit()
#Movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
car(x, y)
for i in range(len(my_enemies)):
a = my_enemies[i][0]
b = my_enemies[i][1]
gameDisplay.blit(enemyImg, (a, b))
#moving_enemy
if b < 0:
my_enemies[i][1] += 1
if moving_right == True:
my_enemies[i][0] += 1
if my_enemies[i][0] > (display_width-100):
my_enemies[i][1] += 100
moving_right = False
moving_left = True
if moving_left == True:
my_enemies[i][0] -= 1
if my_enemies[i][0] < 0:
my_enemies[i][1] += 100
moving_right = True
moving_left = False
if x > (display_width-car_width) or x < 0:
crash()
print(my_enemies)
pygame.display.update()
clock.tick(100)
game_loop()