-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_pygame_space_war.py
69 lines (54 loc) · 1.54 KB
/
07_pygame_space_war.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
import pygame
import random
pygame.init()
# Main Screen
screen = pygame.display.set_mode((500,600))
# Title and Icon
pygame.display.set_caption(".::SPACE WAR::.")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
# Player
playerImg = pygame.image.load('player.png')
playerX = 225
playerY = 450
playerX_change = 0
# enemy
enemyType = random.randint(1,4)
if enemyType == 1 :
enemyImg = pygame.image.load('enemy1.png')
elif enemyType == 2 :
enemyImg = pygame.image.load('enemy2.png')
elif enemyType == 3 :
enemyImg = pygame.image.load('enemy3.png')
elif enemyType == 4 :
enemyImg = pygame.image.load('enemy4.png')
enemyX = random.randint(100,400)
enemyY = random.randint(50,150)
enemyX_change = 0
def player(x,y):
screen.blit(playerImg,(x,y))
def enemy(x,y):
screen.blit(enemyImg ,(x,y))
# Game Frames
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
playerX_change = 0.2
if event.key == pygame.K_LEFT:
playerX_change = -0.2
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
screen.fill((0,128,128))
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >=436 :
playerX = 436
player(playerX,playerY)
enemy(enemyX,enemyY)
pygame.display.update()