Created
September 18, 2021 10:14
-
-
Save shinysu/49276160240a03850009744b94dce287 to your computer and use it in GitHub Desktop.
Complete game with one enmy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pgzrun | |
WIDTH = 1000 | |
HEIGHT = 700 | |
player = Actor('playership') | |
player.bottom = HEIGHT | |
player.x = WIDTH / 2 | |
def draw(): | |
screen.blit('skyb', (0,0)) | |
player.draw() | |
pgzrun.go() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
moving the player | |
''' | |
import pgzrun | |
WIDTH = 1000 | |
HEIGHT = 700 | |
player = Actor('playership') | |
player.bottom = HEIGHT | |
player.x = WIDTH / 2 | |
def draw(): | |
screen.blit('skyb', (0,0)) | |
player.draw() | |
def update(): | |
move_player() | |
def move_player(): | |
if keyboard.LEFT: | |
player.x -= 10 | |
if keyboard.RIGHT: | |
player.x += 10 | |
pgzrun.go() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
create bullets | |
''' | |
import pgzrun | |
WIDTH = 1000 | |
HEIGHT = 700 | |
player = Actor('playership') | |
player.bottom = HEIGHT | |
player.x = WIDTH / 2 | |
bullets = [] | |
def draw(): | |
screen.blit('skyb', (0,0)) | |
player.draw() | |
for bullet in bullets: | |
bullet.draw() | |
def update(): | |
move_player() | |
def on_key_down(key): | |
if key == keys.SPACE: | |
create_new_bullet() | |
def move_player(): | |
if keyboard.LEFT: | |
player.x -= 10 | |
if keyboard.RIGHT: | |
player.x += 10 | |
def create_new_bullet(): | |
bullet = Actor('bullet') | |
bullet.x = player.x | |
bullet.y = player.y | |
bullets.append(bullet) | |
pgzrun.go() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
moving bullets | |
''' | |
import pgzrun | |
WIDTH = 1000 | |
HEIGHT = 700 | |
player = Actor('playership') | |
player.bottom = HEIGHT | |
player.x = WIDTH / 2 | |
bullets = [] | |
def draw(): | |
screen.blit('skyb', (0,0)) | |
player.draw() | |
for bullet in bullets: | |
bullet.draw() | |
def update(): | |
move_player() | |
move_bullets() | |
def on_key_down(key): | |
if key == keys.SPACE: | |
create_new_bullet() | |
def move_player(): | |
if keyboard.LEFT: | |
player.x -= 10 | |
if keyboard.RIGHT: | |
player.x += 10 | |
def move_bullets(): | |
for bullet in bullets: | |
if bullet.bottom < 0: | |
bullets.remove(bullet) | |
else: | |
bullet.y -= 10 | |
def create_new_bullet(): | |
bullet = Actor('bullet') | |
bullet.x = player.x | |
bullet.y = player.y | |
bullets.append(bullet) | |
pgzrun.go() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
create enemies | |
''' | |
import pgzrun | |
from random import randint | |
WIDTH = 1000 | |
HEIGHT = 700 | |
player = Actor('playership') | |
player.bottom = HEIGHT | |
player.x = WIDTH / 2 | |
bullets = [] | |
enemies = [] | |
def draw(): | |
screen.blit('skyb', (0,0)) | |
player.draw() | |
for bullet in bullets: | |
bullet.draw() | |
for enemy in enemies: | |
enemy.draw() | |
def update(): | |
move_player() | |
move_bullets() | |
def on_key_down(key): | |
if key == keys.SPACE: | |
create_new_bullet() | |
def move_player(): | |
if keyboard.LEFT: | |
player.x -= 10 | |
if keyboard.RIGHT: | |
player.x += 10 | |
def move_bullets(): | |
for bullet in bullets: | |
if bullet.bottom < 0: | |
bullets.remove(bullet) | |
else: | |
bullet.y -= 10 | |
def create_new_bullet(): | |
bullet = Actor('bullet') | |
bullet.x = player.x | |
bullet.y = player.y | |
bullets.append(bullet) | |
def create_new_enemy(): | |
enemy = Actor('enemygreen') | |
enemy.x = randint(100, WIDTH-100) | |
enemy.y = 10 | |
enemies.append(enemy) | |
create_new_enemy() | |
pgzrun.go() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Animate the enemies | |
''' | |
import pgzrun | |
from random import randint | |
WIDTH = 1000 | |
HEIGHT = 700 | |
player = Actor('playership') | |
player.bottom = HEIGHT | |
player.x = WIDTH / 2 | |
bullets = [] | |
enemies = [] | |
def draw(): | |
screen.blit('skyb', (0,0)) | |
player.draw() | |
for bullet in bullets: | |
bullet.draw() | |
for enemy in enemies: | |
enemy.draw() | |
def update(): | |
move_player() | |
move_bullets() | |
move_enemies() | |
def on_key_down(key): | |
if key == keys.SPACE: | |
create_new_bullet() | |
def move_player(): | |
if keyboard.LEFT: | |
player.x -= 10 | |
if keyboard.RIGHT: | |
player.x += 10 | |
def move_bullets(): | |
for bullet in bullets: | |
if bullet.bottom < 0: | |
bullets.remove(bullet) | |
else: | |
bullet.y -= 10 | |
def move_enemies(): | |
for enemy in enemies: | |
if enemy.top > HEIGHT: | |
enemies.remove(enemy) | |
create_new_enemy() | |
else: | |
enemy.y += 5 | |
def create_new_bullet(): | |
bullet = Actor('bullet') | |
bullet.x = player.x | |
bullet.y = player.y | |
bullets.append(bullet) | |
def create_new_enemy(): | |
enemy = Actor('enemygreen') | |
enemy.x = randint(100, WIDTH-100) | |
enemy.y = 10 | |
enemies.append(enemy) | |
create_new_enemy() | |
pgzrun.go() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Animate the enemies | |
''' | |
import pgzrun | |
from random import randint | |
WIDTH = 1000 | |
HEIGHT = 700 | |
player = Actor('playership') | |
player.bottom = HEIGHT | |
player.x = WIDTH / 2 | |
bullets = [] | |
enemies = [] | |
def draw(): | |
screen.blit('skyb', (0,0)) | |
player.draw() | |
for bullet in bullets: | |
bullet.draw() | |
for enemy in enemies: | |
enemy.draw() | |
def update(): | |
move_player() | |
move_bullets() | |
move_enemies() | |
def on_key_down(key): | |
if key == keys.SPACE: | |
create_new_bullet() | |
def move_player(): | |
if keyboard.LEFT: | |
player.x -= 10 | |
if keyboard.RIGHT: | |
player.x += 10 | |
def move_bullets(): | |
for bullet in bullets: | |
if bullet.bottom < 0: | |
bullets.remove(bullet) | |
else: | |
bullet.y -= 10 | |
def move_enemies(): | |
for enemy in enemies: | |
if enemy.top > HEIGHT: | |
enemies.remove(enemy) | |
create_new_enemy() | |
else: | |
enemy.y += 5 | |
def create_new_bullet(): | |
bullet = Actor('bullet') | |
bullet.x = player.x | |
bullet.y = player.y | |
bullets.append(bullet) | |
def create_new_enemy(): | |
enemy = Actor('enemygreen') | |
enemy.x = randint(100, WIDTH-100) | |
enemy.y = 10 | |
enemies.append(enemy) | |
create_new_enemy() | |
pgzrun.go() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Check bullet collision with the enemies | |
''' | |
import pgzrun | |
from random import randint | |
WIDTH = 1000 | |
HEIGHT = 700 | |
player = Actor('playership') | |
player.bottom = HEIGHT | |
player.x = WIDTH / 2 | |
bullets = [] | |
enemies = [] | |
def draw(): | |
screen.blit('skyb', (0,0)) | |
player.draw() | |
for bullet in bullets: | |
bullet.draw() | |
for enemy in enemies: | |
enemy.draw() | |
def update(): | |
move_player() | |
move_bullets() | |
move_enemies() | |
check_bullet_collision() | |
def on_key_down(key): | |
if key == keys.SPACE: | |
create_new_bullet() | |
def move_player(): | |
if keyboard.LEFT: | |
player.x -= 10 | |
if keyboard.RIGHT: | |
player.x += 10 | |
def move_bullets(): | |
for bullet in bullets: | |
if bullet.bottom < 0: | |
bullets.remove(bullet) | |
else: | |
bullet.y -= 10 | |
def move_enemies(): | |
for enemy in enemies: | |
if enemy.top > HEIGHT: | |
enemies.remove(enemy) | |
create_new_enemy() | |
else: | |
enemy.y += 5 | |
def create_new_bullet(): | |
bullet = Actor('bullet') | |
bullet.x = player.x | |
bullet.y = player.y | |
bullets.append(bullet) | |
def create_new_enemy(): | |
enemy = Actor('enemygreen') | |
enemy.x = randint(100, WIDTH-100) | |
enemy.y = 10 | |
enemies.append(enemy) | |
def check_bullet_collision(): | |
for bullet in bullets: | |
for enemy in enemies: | |
if bullet.colliderect(enemy): | |
sounds.shoot.play() | |
bullets.remove(bullet) | |
enemies.remove(enemy) | |
create_new_enemy() | |
create_new_enemy() | |
pgzrun.go() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Add scoring logic | |
''' | |
import pgzrun | |
from random import randint | |
WIDTH = 1000 | |
HEIGHT = 700 | |
player = Actor('playership') | |
player.bottom = HEIGHT | |
player.x = WIDTH / 2 | |
bullets = [] | |
enemies = [] | |
score = 0 | |
WHITE = (255, 255, 255) | |
RED = (255, 0, 0) | |
game_over = False | |
def draw(): | |
screen.blit('skyb', (0,0)) | |
player.draw() | |
for bullet in bullets: | |
bullet.draw() | |
for enemy in enemies: | |
enemy.draw() | |
screen.draw.text("Score: "+str(score), (10, 10), fontsize=25, color=WHITE) | |
if game_over: | |
screen.draw.text("GAME OVER !!!", (400, HEIGHT/2), fontsize=50, color=RED) | |
def update(): | |
if not game_over: | |
move_player() | |
move_bullets() | |
move_enemies() | |
check_bullet_collision() | |
check_player_collision() | |
def on_key_down(key): | |
if not game_over: | |
if key == keys.SPACE: | |
create_new_bullet() | |
def move_player(): | |
if keyboard.LEFT: | |
player.x -= 10 | |
if keyboard.RIGHT: | |
player.x += 10 | |
def move_bullets(): | |
for bullet in bullets: | |
if bullet.bottom < 0: | |
bullets.remove(bullet) | |
else: | |
bullet.y -= 10 | |
def move_enemies(): | |
for enemy in enemies: | |
if enemy.top > HEIGHT: | |
enemies.remove(enemy) | |
create_new_enemy() | |
else: | |
enemy.y += 5 | |
def create_new_bullet(): | |
bullet = Actor('bullet') | |
bullet.x = player.x | |
bullet.y = player.y | |
bullets.append(bullet) | |
def create_new_enemy(): | |
enemy = Actor('enemygreen') | |
enemy.x = randint(100, WIDTH-100) | |
enemy.y = 10 | |
enemies.append(enemy) | |
def check_bullet_collision(): | |
global score | |
for bullet in bullets: | |
for enemy in enemies: | |
if bullet.colliderect(enemy): | |
score += 10 | |
sounds.shoot.play() | |
bullets.remove(bullet) | |
enemies.remove(enemy) | |
create_new_enemy() | |
def check_player_collision(): | |
global game_over | |
for enemy in enemies: | |
if enemy.colliderect(player): | |
player.image = 'damageship' | |
game_over = True | |
create_new_enemy() | |
pgzrun.go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment