Skip to content

Commit

Permalink
Add new examples for pygame zero
Browse files Browse the repository at this point in the history
  • Loading branch information
JeongJun-Lee committed Nov 14, 2024
1 parent 26c1b5b commit 4e728d2
Show file tree
Hide file tree
Showing 27 changed files with 465 additions and 1 deletion.
43 changes: 43 additions & 0 deletions mu/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,39 @@
"top.png",
"bottom.png",
"background.png",
"background1.png",
"background2.png",
"ball.png",
"bar.png",
"block.png",
"enemy_bullet.png",
"enemy1_1.png",
"enemy1_2.png",
"explosion1.png",
"explosion2.png",
"grass.png",
"player_bullet.png",
"player.png",
"tank_blue.png",
"tank_dark.png",
"tank_green.png",
"tank_red.png",
"tank_sand.png",
"wall.png",
]
EXAMPLE_PGZ_SOUNDS = [
"sfx_exp_medium12.wav",
"sfx_sounds_interaction25.wav",
]
EXAMPLE_PGZ_MUSIC = [
"main_theme.mp3",
]
EXAMPLE_PGZ = [
"flappybird.py",
"flappybird_neosoco.py",
"battle_city.py",
"breakout.py",
"tweenbee.py",
]
EXAMPLE_NEOPIA = [
"01-01_KobiBot.py",
Expand Down Expand Up @@ -1007,6 +1036,20 @@ def setup(self, modes):
shutil.copy(
path(sfx, "pygamezero/"), os.path.join(example_pgz_path + 'images', sfx)
)
if not os.path.exists(example_pgz_path + 'sounds'):
logger.debug("Creating directory: {}".format('sounds'))
os.makedirs(example_pgz_path + 'sounds')
for sfx in EXAMPLE_PGZ_SOUNDS:
shutil.copy(
path(sfx, "pygamezero/"), os.path.join(example_pgz_path + 'sounds', sfx)
)
if not os.path.exists(example_pgz_path + 'music'):
logger.debug("Creating directory: {}".format('music'))
os.makedirs(example_pgz_path + 'music')
for sfx in EXAMPLE_PGZ_MUSIC:
shutil.copy(
path(sfx, "pygamezero/"), os.path.join(example_pgz_path + 'music', sfx)
)
# Neopia examples
if not os.path.exists(example_neopia_path):
logger.debug("Creating directory: {}".format(example_neopia_path))
Expand Down
Binary file added mu/resources/pygamezero/background1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/background2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
190 changes: 190 additions & 0 deletions mu/resources/pygamezero/battle_city.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
from pgzhelper import *
import random

WIDTH = 800
HEIGHT = 600

bullets = []
bullet_delay_cnt = 0
BULLET_DELAY = 50
enemy_bullets = []
explosions = []
winner = ''

tank = Actor("tank_blue", (400, 575))
tank.angle = 90

ENEMY_MOVE_DELAY = 20
MAX_ENEMIES = 3
enemies = []
for i in range(MAX_ENEMIES):
enemy = Actor("tank_red")
enemy.angle = 270
enemy.x = (i + 1) * WIDTH / (MAX_ENEMIES + 1)
enemy.y = 25
enemy.move_cnt = 0
enemies.append(enemy)

walls = []
WALL_SIZE = 50
# 50x50 pixel sized walls
for x in range(int(WIDTH / WALL_SIZE)):
# Substract 2 to blank both first and last row
for y in range(int(HEIGHT / WALL_SIZE - 2)):
# Randomly leave blank without wall
if random.randint(0, 100) < 50:
wall = Actor("wall", anchor=("left", "top"))
wall.x = x * WALL_SIZE
wall.y = y * WALL_SIZE + WALL_SIZE # Add WALL_SIZE to blank first row
walls.append(wall)


def move_player(player):
# Save the original position of the tank
original_x = player.x
original_y = player.y

if player == tank: # My tank
if keyboard.right:
player.angle = 0
player.x += 2
elif keyboard.left:
player.angle = 180
player.x -= 2
elif keyboard.up:
player.angle = 90
player.y -= 2
elif keyboard.down:
player.angle = 270
player.y += 2
else: # Enemy
if player.angle == 0:
player.x += 2
elif player.angle == 90:
player.y -= 2
elif player.angle == 180:
player.x -= 2
elif player.angle == 270:
player.y += 2

# Return player to original position if colliding with wall
if player.collidelist(walls) != -1:
player.x = original_x
player.y = original_y

# Don't drive off the screen!
if player.left < 0 or player.right > WIDTH \
or player.top < 0 or player.bottom > HEIGHT:
player.x = original_x
player.y = original_y


def fire_bullets(player, bullets):
if player == tank:
bullet = Actor("bulletblue2")
else:
bullet = Actor("bulletred2")

bullet.angle = player.angle
bullet.pos = player.pos
bullets.append(bullet)


def collide_bullets(bullets):
global winner

for bullet in bullets:
if bullet.angle == 0:
bullet.x += 5
elif bullet.angle == 90:
bullet.y -= 5
elif bullet.angle == 180:
bullet.x -= 5
elif bullet.angle == 270:
bullet.y += 5

# Walls
wall_index = bullet.collidelist(walls)
if wall_index != -1:
del walls[wall_index]
enemies.remove(enemies[hit])
bullets.remove(bullet)

# Out of screen
if bullet.x < 0 or bullet.x > 800 \
or bullet.y < 0 or bullet.y > 600:
bullets.remove(bullet)

# Enemies
if bullets != enemy_bullets:
enemy_index = bullet.collidelist(enemies)
if enemy_index != -1:
bullets.remove(bullet)
explosion = Actor("explosion3")
explosion.pos = enemies[enemy_index].pos
explosion.images = ["explosion3", "explosion4"]
explosion.fps = 8
explosion.duration = 15
explosions.append(explosion)
del enemies[enemy_index]
if len(enemies) == 0:
winner = "You"
else:
if bullet.colliderect(tank):
winner = "Enemy"

# Animate explosion
for explosion in explosions:
explosion.animate()
explosion.duration -= 1
if explosion.duration == 0:
explosions.remove(explosion)


def draw():
screen.blit('grass', (0, 0))
tank.draw()
for enemy in enemies:
enemy.draw()
for wall in walls:
wall.draw()
for bullet in bullets:
bullet.draw()
for bullet in enemy_bullets:
bullet.draw()
for explosion in explosions:
explosion.draw()

if winner:
screen.draw.text(winner + " Win!", \
midbottom=(WIDTH / 2, HEIGHT / 2), fontsize=100)


def update():
global bullet_delay_cnt, enemy_move_cnt

# This part is for my tank
if winner == '':
move_player(tank)
if bullet_delay_cnt == 0: # Re-launch possible after the delay ends
if keyboard.space:
sounds.sfx_exp_medium12.play()
fire_bullets(tank, bullets)
bullet_delay_cnt = BULLET_DELAY
else:
bullet_delay_cnt -= 1
collide_bullets(bullets)

# This part is for the enemies
for enemy in enemies:
choice = random.randint(0, 2)
if enemy.move_cnt > 0: # Move tank
enemy.move_cnt -= 1
move_player(enemy)
elif choice == 0: # Init movement delay
enemy.move_cnt = ENEMY_MOVE_DELAY
elif choice == 1: # Turn directions
enemy.angle = random.randint(0, 3) * 90
else: # Fire canon shot
fire_bullets(enemy, enemy_bullets)
collide_bullets(enemy_bullets)
Binary file added mu/resources/pygamezero/block.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions mu/resources/pygamezero/breakout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from pgzhelper import *

TITLE = 'Breakout'
WIDTH = 800
HEIGHT = 600

GAP_FROM_SCREEN = 50
ball = Actor('ball', (WIDTH / 2, HEIGHT / 2))
ball.radius = ball.width / 2
bar = Actor('bar', (WIDTH / 2, HEIGHT - GAP_FROM_SCREEN))

# Create 4 x 8 block dummy
blocks = []
for block_row in range(4):
for block_col in range(8):
block = Actor(
'block',
(block_col * 100, block_row * 32 + GAP_FROM_SCREEN),
anchor=('left', 'top')
)
blocks.append(block)

# Set velocity of ball
vx = 5
vy = -5


def draw():
screen.blit('background', (0, 0))
ball.draw()
bar.draw()
for block in blocks:
block.draw()

def update():
global vx, vy

# Limit the movement of bar in the window
if bar.left < 0:
bar.left = 0
if bar.right > WIDTH:
bar.right = WIDTH

# Move the ball by velocity
ball.move_ip(vx, vy)

# When the ball hits left or rignt wall
if ball.left < 0 or ball.right > WIDTH:
vx = -vx # Make x of velocity opposite direction
sounds.wall.play()

# When the ball hits upper wall
if ball.top < 0:
vy = -vy # Make y of velocity opposite direction
sounds.wall.play()

# When the ball hits the bar
if ball.circle_colliderect(bar) == True:
ball.y -= 10 # 10픽셀 수직으로 먼저 튀어오르기
vy = -vy # Make y of velocity opposite direction
sounds.bar.play()

# When the ball hits the block
b_index = ball.collidelist(blocks)
if b_index != -1:
vy = -vy # Make y of velocity opposite direction
sounds.block.play()
blocks.pop(b_index)

# Exit the game
if ball.bottom > HEIGHT:
sounds.die.play()
game.exit()

if not blocks:
sounds.win.play()
vx = 0
vy = 0

def on_mouse_move(pos):
x, y = pos
bar.centerx = x
Binary file added mu/resources/pygamezero/enemy1_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/enemy1_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/enemy_bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/explosion1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/explosion2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/grass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/main_theme.mp3
Binary file not shown.
Binary file added mu/resources/pygamezero/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/player_bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/sfx_exp_medium12.wav
Binary file not shown.
Binary file not shown.
Binary file added mu/resources/pygamezero/tank_blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/tank_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/tank_green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/tank_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mu/resources/pygamezero/tank_sand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4e728d2

Please sign in to comment.