Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created December 19, 2020 15:37
Show Gist options
  • Save shinysu/34f7571a020cd2d4fce7962baa3978cd to your computer and use it in GitHub Desktop.
Save shinysu/34f7571a020cd2d4fce7962baa3978cd to your computer and use it in GitHub Desktop.
Space shooter
import pgzrun
HEIGHT = 800
WIDTH = 1000
BLACK = (0, 0, 0)
ship = Actor("playership")
ship.x = WIDTH // 2
ship.bottom = HEIGHT
def draw():
screen.fill(BLACK)
ship.draw()
pgzrun.go()
'''
move the ship left and right based on keyboard keys
'''
import pgzrun
HEIGHT = 800
WIDTH = 1000
BLACK = (0, 0, 0)
ship = Actor("playership")
ship.x = WIDTH // 2
ship.bottom = HEIGHT
def draw():
screen.fill(BLACK)
ship.draw()
def update():
move_ship()
def move_ship():
if keyboard.left:
ship.x -= 5
if keyboard.right:
ship.x += 5
pgzrun.go()
'''
create a bullet and animate it
'''
import pgzrun
HEIGHT = 800
WIDTH = 1000
BLACK = (0, 0, 0)
ship = Actor("playership")
ship.x = WIDTH // 2
ship.bottom = HEIGHT
bullet = Actor("bullet")
bullet.x = ship.x
bullet.y = ship.top
def draw():
screen.fill(BLACK)
ship.draw()
bullet.draw()
def update():
move_ship()
move_bullet()
def move_ship():
if keyboard.left:
ship.x -= 5
if keyboard.right:
ship.x += 5
def move_bullet():
bullet.y -= 10
pgzrun.go()
'''
create a group of bullets
'''
import pgzrun
HEIGHT = 800
WIDTH = 1000
BLACK = (0, 0, 0)
ship = Actor("playership")
ship.x = WIDTH // 2
ship.bottom = HEIGHT
bullets = []
def draw():
screen.fill(BLACK)
ship.draw()
for bullet in bullets:
bullet.draw()
def update():
move_ship()
move_bullet()
def on_key_down(key):
if key == keys.SPACE:
create_new_bullet()
def move_ship():
if keyboard.left:
ship.x -= 5
if keyboard.right:
ship.x += 5
def move_bullet():
for bullet in bullets:
if bullet.y < 0:
bullets.remove(bullet)
else:
bullet.y -= 10
def create_new_bullet():
bullet = Actor("bullet")
bullet.x = ship.x
bullet.y = ship.top
bullets.append(bullet)
pgzrun.go()
'''
create enemies
'''
import pgzrun
from random import randint
HEIGHT = 800
WIDTH = 1000
BLACK = (0, 0, 0)
ship = Actor("playership")
ship.x = WIDTH // 2
ship.bottom = HEIGHT
bullets = []
enemies = []
def draw():
screen.fill(BLACK)
ship.draw()
for bullet in bullets:
bullet.draw()
for enemy in enemies:
enemy.draw()
def update():
move_ship()
move_bullet()
move_enemies()
def on_key_down(key):
if key == keys.SPACE:
create_new_bullet()
def move_ship():
if keyboard.left:
ship.x -= 5
if keyboard.right:
ship.x += 5
def move_enemies():
for enemy in enemies:
if enemy.bottom > HEIGHT:
enemies.remove(enemy)
create_new_enemy()
else:
enemy.y += 10
def move_bullet():
for bullet in bullets:
if bullet.y < 0:
bullets.remove(bullet)
else:
bullet.y -= 10
def create_new_bullet():
bullet = Actor("bullet")
bullet.x = ship.x
bullet.y = ship.top
bullets.append(bullet)
def create_new_enemy():
enemy = Actor("enemygreen")
enemy.x = randint(50, WIDTH - 50)
enemy.y = 50
enemies.append(enemy)
create_new_enemy()
pgzrun.go()
'''
Check if bullet collides with enemy or if enemy collides with player
'''
import pgzrun
from random import randint
HEIGHT = 800
WIDTH = 1000
BLACK = (0, 0, 0)
ship = Actor("playership")
ship.x = WIDTH // 2
ship.bottom = HEIGHT
bullets = []
enemies = []
def draw():
screen.fill(BLACK)
ship.draw()
for bullet in bullets:
bullet.draw()
for enemy in enemies:
enemy.draw()
def update():
move_ship()
move_bullet()
move_enemies()
check_bullet_collision()
def on_key_down(key):
if key == keys.SPACE:
create_new_bullet()
def move_ship():
if keyboard.left:
ship.x -= 5
if keyboard.right:
ship.x += 5
def move_enemies():
for enemy in enemies:
if enemy.bottom > HEIGHT:
enemies.remove(enemy)
create_new_enemy()
else:
enemy.y += 10
def move_bullet():
for bullet in bullets:
if bullet.y < 0:
bullets.remove(bullet)
else:
bullet.y -= 10
def create_new_bullet():
bullet = Actor("bullet")
bullet.x = ship.x
bullet.y = ship.top
bullets.append(bullet)
def create_new_enemy():
enemy = Actor("enemygreen")
enemy.x = randint(50, WIDTH - 50)
enemy.y = 50
enemies.append(enemy)
def check_bullet_collision():
for enemy in enemies:
for bullet in bullets:
if enemy.colliderect(bullet):
bullets.remove(bullet)
enemies.remove(enemy)
sounds.shoot.play()
create_new_enemy()
create_new_enemy()
pgzrun.go()
'''
Check collision
'''
import pgzrun
from random import randint
HEIGHT = 800
WIDTH = 1000
BLACK = (0, 0, 0)
ship = Actor("playership")
ship.x = WIDTH // 2
ship.bottom = HEIGHT
bullets = []
enemies = []
is_player_dead = False
def draw():
screen.fill(BLACK)
ship.draw()
for bullet in bullets:
bullet.draw()
for enemy in enemies:
enemy.draw()
def update():
if not is_player_dead:
move_ship()
move_bullet()
move_enemies()
check_bullet_collision()
check_player_collision()
def on_key_down(key):
if not is_player_dead:
if key == keys.SPACE:
create_new_bullet()
def move_ship():
if keyboard.left:
ship.x -= 5
if keyboard.right:
ship.x += 5
def move_enemies():
for enemy in enemies:
if enemy.bottom > HEIGHT:
enemies.remove(enemy)
create_new_enemy()
else:
enemy.y += 5
def move_bullet():
for bullet in bullets:
if bullet.y < 0:
bullets.remove(bullet)
else:
bullet.y -= 10
def create_new_bullet():
bullet = Actor("bullet")
bullet.x = ship.x
bullet.y = ship.top
bullets.append(bullet)
def create_new_enemy():
enemy = Actor("enemygreen")
enemy.x = randint(50, WIDTH - 50)
enemy.y = 50
enemies.append(enemy)
def check_bullet_collision(): # check if any bullet collides with any of the enemies
for enemy in enemies:
for bullet in bullets:
if enemy.colliderect(bullet):
if bullet in bullets:
bullets.remove(bullet)
if enemy in enemies:
enemies.remove(enemy)
sounds.shoot.play() #play sound
create_new_enemy()
def check_player_collision(): # check if any enemy collides with player
global is_player_dead
for enemy in enemies:
if enemy.colliderect(ship):
if enemy in enemies:
enemies.remove(enemy)
ship.image = "damageship"
is_player_dead = True
create_new_enemy()
pgzrun.go()
'''
Create multiple enemies
'''
import pgzrun
from random import randint
HEIGHT = 800
WIDTH = 1000
BLACK = (0, 0, 0)
ship = Actor("playership")
ship.x = WIDTH // 2
ship.bottom = HEIGHT
bullets = []
enemies = []
is_player_dead = False
def draw():
screen.fill(BLACK)
ship.draw()
for bullet in bullets:
bullet.draw()
for enemy in enemies:
enemy.draw()
def update():
if not is_player_dead:
move_ship()
move_bullet()
move_enemies()
check_bullet_collision()
check_player_collision()
def on_key_down(key):
if not is_player_dead:
if key == keys.SPACE:
create_new_bullet()
def move_ship():
if keyboard.left:
ship.x -= 5
if keyboard.right:
ship.x += 5
def move_enemies():
for enemy in enemies:
if enemy.bottom > HEIGHT:
enemies.remove(enemy)
#create_new_enemy()
else:
enemy.y += 5
def move_bullet():
for bullet in bullets:
if bullet.y < 0:
bullets.remove(bullet)
else:
bullet.y -= 10
def create_new_bullet():
bullet = Actor("bullet")
bullet.x = ship.x
bullet.y = ship.top
bullets.append(bullet)
def create_new_enemy():
for i in range(8):
for j in range(4):
enemy = Actor("smallenemy")
enemy.left = 70 * i
enemy.top = 70 * j
enemies.append(enemy)
def check_bullet_collision(): # check if any bullet collides with any of the enemies
for enemy in enemies:
for bullet in bullets:
if enemy.colliderect(bullet):
if bullet in bullets:
bullets.remove(bullet)
if enemy in enemies:
enemies.remove(enemy)
sounds.shoot.play() #play sound
if len(enemies) == 0:
create_enemy()
def check_player_collision(): # check if any enemy collides with player
global is_player_dead
for enemy in enemies:
if enemy.colliderect(ship):
if enemy in enemies:
enemies.remove(enemy)
ship.image = "damageship"
is_player_dead = True
create_new_enemy()
pgzrun.go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment