-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreecodecampgame.py
More file actions
63 lines (45 loc) · 2.46 KB
/
freecodecampgame.py
File metadata and controls
63 lines (45 loc) · 2.46 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
import pygame
#to initialize
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600)) #TWO BRACKETS HERE #width,height #(x,y)
#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
player_image = pygame.image.load('plane_64bit.png')#don't forget to write png here
playerX = 360
playerY = 450
playerX_change = 0
playerY_change = 0
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
screen.blit(player_image, (x, y)) #blit basically means to draw #put playerX, playerY in bracket (playerX, playerY)like this
'''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))
#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 hve 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
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT :
playerX_change = 0.1
playerX = playerX + playerX_change
playerY = playerY + playerY_change
player(playerX, playerY)
pygame.display.update() # You have to write this to upadate anything and everything in the game window