-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
116 lines (84 loc) · 3.96 KB
/
tempCodeRunnerFile.py
File metadata and controls
116 lines (84 loc) · 3.96 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
import pygame
import random
# to initialize
pygame.init()
# BACKGROUND
background = pygame.image.load('background.png')
# create the screen # TWO BRACKETS HERE #width,height #(x,y)
screen = pygame.display.set_mode((800, 600))
# TITEL AND ICON
pygame.display.set_caption('Space Warriors') # use 32*32 for icons
icon = pygame.image.load('spaceship_icon_32bits.png')
pygame.display.set_icon(icon)
# PLAYER , coordinates of player
# don't forget to write png here
player_image = pygame.image.load('plane_64bit.png')
playerX = 360
playerY = 450
playerX_change = 0
playerY_change = 0
# ADDING ALIEN
alien_image = pygame.image.load('aliens.png') # don't forget to write png here
alienX = random.randint(0,736)
alienY = random.randint(10,100)
alienX_change = 0.2
alienY_change = 40
def player(x, y): # playerX has become x here and playerY has become y here ... and this x and y will go down ... uss din tu bhai toda confuse ho gya tha fir defination in python search kr k dekha tah yaad aaya
# blit basically means to draw #put playerX, playerY in bracket (playerX, playerY)like this
screen.blit(player_image, (x, y))
def alien(x, y):
screen.blit(alien_image, (x, y))
#'''place icon in the same folder where freecodecampgame.py is ... (don't put this inside some new folder )'''
# GAME LOOP
running = True
while running: # this loop exits when closed button is pressed
# RED GREEN BLUE -- these values goes from zero to 255
screen.fill((0, 0, 0))
# BACKGROUND IMAGE ADDING
screen.blit(background, (0,0))
# '''playerX = playerX + 0.1 #adding 0.1 in x so image will move RHS'''
# because screen comes first so we have written screen first ... then over it below things/images will get added so written below this
# pass #this much will hang the window ... program is in while loop and never ends so now you have to add QUIT
# anyting happening inside game window is an event even pressing of close button is also an event in pygame
# we have to make sure we exit this loop when closed button is pressed
# so make variable running = True
for event in pygame.event.get():
# this will manage all the events happening inside our game widow
# if we press red cross then the while loop should exit and we can do that by making while False
if event.type == pygame.QUIT: # MAKE SURE YOU WRITE QUIT IN CAPITAL LETTERS
running = False
break
if event.type == pygame.KEYDOWN: # key down means pressing the key and key up means releasing the key
# print('A key is pressed')
if event.key == pygame.K_LEFT:
#print('LEFT arrow is pressed')
playerX_change = -0.6
if event.key == pygame.K_RIGHT:
# print('RIGHT arrow is pressed')
playerX_change = 0.6
if event.type == pygame.KEYUP: # releasing the key ... which was either right or left
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0 # speed of spaceship AFTER RELESING THE KEY #if you don't write this or pass and leave it empty then you will get error since conditional statements need something REMEMBER HAHA
# print('keystroke has been released')
# PLAYER
playerX = playerX + playerX_change
# playerY = playerY + playerY_change
# ADDING BOUNDARIES FOR OUR SPACESHIP
if playerX <= 0:
playerX = 0
elif playerX >= 736: # 800-64=736
playerX = 736
# ALIEN
# alienX = alienX + alienX_change
# ADDING BOUNDARIES FOR OUR ALIEN
if alienX <= 0:
alienX_change = 0.2
alienY = alienY + alienY_change
elif alienX >= 736: # 800-64=736
alienX_change = -0.2
alienY = alienY + alienY_change
alienX = alienX + alienX_change
player(playerX, playerY)
alien(alienX, alienY)
# You have to write this to upadate anything and everything in the game window
pygame.display.update()