Space Invader
Space Invader
import pygame
pygame.init ()
#player
playerImg = pygame.image.load('spaceship.png')
playerX = 370
playerY = 480
playerX_change = 0
#game loop
running = True
while running:
screen.fill((0, 0, 0))
#
playerX += playerX_change
if playerX <=0 :
playerX = 0
elif playerX >= 736 :
playerX = 736
player(playerX,playerY)
pygame.display.update()
===================================space invader part
2=======================================
import pygame
import random
pygame.init ()
#player
playerImg = pygame.image.load('spaceship.png')
playerX = 370
playerY = 480
playerX_change = 0
#enemy
enemyImg = pygame.image.load('ufo.png')
enemyX = random.randint(0, 800)
enemyY = random.randint(50, 150)
enemyX_change = 0.3
enemyY_change = 40
#game loop
running = True
while running:
screen.fill((0, 0, 0))
if playerX <=0 :
playerX = 0
elif playerX >= 736 :
playerX = 736
#enemy movement
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 0.3
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.3
enemyY += enemyY_change
player(playerX,playerY)
enemy(enemyX, enemyY)
pygame.display.update()
import pygame
import random
pygame.init ()
#bakcground
background = pygame.image.load('background.png')
#player
playerImg = pygame.image.load('spaceship.png')
playerX = 370
playerY = 480
playerX_change = 0
#enemy
enemyImg = pygame.image.load('ufo.png')
enemyX = random.randint(0, 800)
enemyY = random.randint(50, 50)
enemyX_change = 4
enemyY_change = 40
#bullet
bulletImg = pygame.image.load('bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
#game loop
running = True
while running:
#rgb
screen.fill((0, 0, 0))
#backg image
screen.blit (background,(0,0))
if event.type ==pygame.KEYUP :
if event.key == pygame.K_LEFT or event.key ==pygame.K_RIGHT:
playerX_change = 0
#check bounderies
playerX += playerX_change
if playerX <=0 :
playerX = 0
elif playerX >= 736 :
playerX = 736
#enemy movement
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 4
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -4
enemyY += enemyY_change
#bullet movement
if bullet_state is "fire":
fire_bullet(playerX,bulletY)
bulletY -= bulletY_change
player(playerX,playerY)
enemy(enemyX, enemyY)
pygame.display.update()
pygame.init ()
#bakcground
background = pygame.image.load('background.png')
#player
playerImg = pygame.image.load('spaceship.png')
playerX = 370
playerY = 480
playerX_change = 0
#enemy
enemyImg = pygame.image.load('ufo.png')
enemyX = random.randint(0, 735)
enemyY = random.randint(50, 50)
enemyX_change = 4
enemyY_change = 40
#bullet
bulletImg = pygame.image.load('bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
score = 0
#game loop
running = True
while running:
#rgb
screen.fill((0, 0, 0))
#backg image
screen.blit (background,(0,0))
if event.type ==pygame.KEYUP :
if event.key == pygame.K_LEFT or event.key ==pygame.K_RIGHT:
playerX_change = 0
#check bounderies
playerX += playerX_change
if playerX <=0 :
playerX = 0
elif playerX >= 736 :
playerX = 736
#enemy movement
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 4
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -4
enemyY += enemyY_change
#bullet movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state is "fire":
fire_bullet(bulletX,bulletY)
bulletY -= bulletY_change
#collision
collision = isCollision(enemyX,enemyY,bulletX,bulletY)
if collision :
bulletY = 480
bullet_state = "ready"
score += 1
print(score)
enemyX = random.randint(0, 735)
enemyY = random.randint(50, 50)
player(playerX,playerY)
enemy(enemyX, enemyY)
pygame.display.update()