Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created September 21, 2021 07:44
Show Gist options
  • Save shinysu/1073c5b319c4f6f2ad336c6786f93c0d to your computer and use it in GitHub Desktop.
Save shinysu/1073c5b319c4f6f2ad336c6786f93c0d to your computer and use it in GitHub Desktop.
battle_city
import pgzrun
WIDTH = 800
HEIGHT = 600
tank = Actor('tank_blue')
tank.x = WIDTH / 2
tank.bottom = HEIGHT
tank.angle = 90
def draw():
screen.clear()
tank.draw()
pgzrun.go()
import pgzrun
WIDTH = 800
HEIGHT = 600
tank = Actor('tank_blue')
tank.x = WIDTH / 2
tank.bottom = HEIGHT
tank.angle = 90
def draw():
screen.clear()
screen.blit('grass', (0,0))
tank.draw()
def update():
move_tank()
check_tank_boundaries()
def move_tank():
if keyboard.left:
tank.x -= 5
tank.angle = 180
if keyboard.right:
tank.x += 5
tank.angle = 0
if keyboard.up:
tank.y -= 5
tank.angle = 90
if keyboard.down:
tank.y += 5
tank.angle = 270
def check_tank_boundaries():
if tank.right > WIDTH:
tank.right = WIDTH
if tank.left < 0:
tank.left = 0
if tank.top < 0:
tank.top = 0
if tank.bottom > HEIGHT:
tank.bottom = HEIGHT
pgzrun.go()
'''
draw the wall
'''
import pgzrun
WIDTH = 800
HEIGHT = 600
tank = Actor('tank_blue')
tank.x = WIDTH / 2
tank.bottom = HEIGHT
tank.angle = 90
walls = []
def draw():
screen.clear()
screen.blit('grass', (0,0))
tank.draw()
for wall in walls:
wall.draw()
def update():
move_tank()
check_tank_boundaries()
def move_tank():
if keyboard.left:
tank.x -= 5
tank.angle = 180
if keyboard.right:
tank.x += 5
tank.angle = 0
if keyboard.up:
tank.y -= 5
tank.angle = 90
if keyboard.down:
tank.y += 5
tank.angle = 270
def check_tank_boundaries():
if tank.right > WIDTH:
tank.right = WIDTH
if tank.left < 0:
tank.left = 0
if tank.top < 0:
tank.top = 0
if tank.bottom > HEIGHT:
tank.bottom = HEIGHT
def create_new_wall():
for i in range(WIDTH//50):
for j in range(HEIGHT//50):
wall = Actor('wall')
wall.topleft = (i * 50, j * 50)
walls.append(wall)
create_new_wall()
pgzrun.go()
'''
randomise the wall
'''
import pgzrun
from random import randint
WIDTH = 800
HEIGHT = 600
tank = Actor('tank_blue')
tank.x = WIDTH / 2
tank.bottom = HEIGHT
tank.angle = 90
walls = []
def draw():
screen.clear()
screen.blit('grass', (0,0))
tank.draw()
for wall in walls:
wall.draw()
def update():
move_tank()
check_tank_boundaries()
def move_tank():
if keyboard.left:
tank.x -= 5
tank.angle = 180
if keyboard.right:
tank.x += 5
tank.angle = 0
if keyboard.up:
tank.y -= 5
tank.angle = 90
if keyboard.down:
tank.y += 5
tank.angle = 270
def check_tank_boundaries():
if tank.right > WIDTH:
tank.right = WIDTH
if tank.left < 0:
tank.left = 0
if tank.top < 0:
tank.top = 0
if tank.bottom > HEIGHT:
tank.bottom = HEIGHT
def create_new_wall():
for i in range(16):
for j in range(10):
if randint(0, 100) > 50:
wall = Actor('wall')
wall.topleft = (i * 50, 50 + j * 50)
walls.append(wall)
create_new_wall()
pgzrun.go()
'''
tank - wall - collision
'''
import pgzrun
from random import randint
WIDTH = 800
HEIGHT = 600
tank = Actor('tank_blue')
tank.x = WIDTH / 2
tank.bottom = HEIGHT
tank.angle = 90
walls = []
bullets = []
def draw():
screen.clear()
screen.blit('grass', (0,0))
tank.draw()
for wall in walls:
wall.draw()
for bullet in bullets:
bullet.draw()
def update():
original_x = tank.x
original_y = tank.y
move_tank()
check_tank_boundaries()
tank_wall_collision(original_x, original_y)
def on_key_down(key):
if key == keys.SPACE:
create_new_bullet()
def move_tank():
if keyboard.left:
tank.x -= 5
tank.angle = 180
if keyboard.right:
tank.x += 5
tank.angle = 0
if keyboard.up:
tank.y -= 5
tank.angle = 90
if keyboard.down:
tank.y += 5
tank.angle = 270
def check_tank_boundaries():
if tank.right > WIDTH:
tank.right = WIDTH
if tank.left < 0:
tank.left = 0
if tank.top < 0:
tank.top = 0
if tank.bottom > HEIGHT:
tank.bottom = HEIGHT
def create_new_wall():
for i in range(16):
for j in range(10):
if randint(0, 100) > 50:
wall = Actor('wall')
wall.topleft = (i * 50, 50 + j * 50)
walls.append(wall)
def tank_wall_collision(original_x, original_y):
for wall in walls:
if tank.colliderect(wall):
tank.x = original_x
tank.y = original_y
def create_new_bullet():
bullet = Actor('bulletblue2')
bullet.pos = tank.pos
bullet.angle = tank.angle
bullets.append(bullet)
create_new_wall()
pgzrun.go()
'''
tank - wall - collision
'''
import pgzrun
from random import randint
WIDTH = 800
HEIGHT = 600
tank = Actor('tank_blue')
tank.x = WIDTH / 2
tank.bottom = HEIGHT
tank.angle = 90
walls = []
bullets = []
def draw():
screen.clear()
screen.blit('grass', (0,0))
tank.draw()
for wall in walls:
wall.draw()
for bullet in bullets:
bullet.draw()
def update():
original_x = tank.x
original_y = tank.y
move_tank()
check_tank_boundaries()
tank_wall_collision(original_x, original_y)
move_bullets()
def on_key_down(key):
if key == keys.SPACE:
create_new_bullet()
def move_tank():
if keyboard.left:
tank.x -= 5
tank.angle = 180
if keyboard.right:
tank.x += 5
tank.angle = 0
if keyboard.up:
tank.y -= 5
tank.angle = 90
if keyboard.down:
tank.y += 5
tank.angle = 270
def check_tank_boundaries():
if tank.right > WIDTH:
tank.right = WIDTH
if tank.left < 0:
tank.left = 0
if tank.top < 0:
tank.top = 0
if tank.bottom > HEIGHT:
tank.bottom = HEIGHT
def create_new_wall():
for i in range(16):
for j in range(10):
if randint(0, 100) > 50:
wall = Actor('wall')
wall.topleft = (i * 50, 50 + j * 50)
walls.append(wall)
def tank_wall_collision(original_x, original_y):
for wall in walls:
if tank.colliderect(wall):
tank.x = original_x
tank.y = original_y
def create_new_bullet():
bullet = Actor('bulletblue2')
bullet.pos = tank.pos
bullet.angle = tank.angle
bullets.append(bullet)
def move_bullets():
for bullet in bullets:
if bullet.angle == 0:
bullet.x += 5
if bullet.angle == 90:
bullet.y -= 5
if bullet.angle == 180:
bullet.x -= 5
if bullet.angle == 270:
bullet.y += 5
create_new_wall()
pgzrun.go()
'''
destroying walls
'''
import pgzrun
from random import randint
WIDTH = 800
HEIGHT = 600
tank = Actor('tank_blue')
tank.x = WIDTH / 2
tank.bottom = HEIGHT
tank.angle = 90
walls = []
bullets = []
def draw():
screen.clear()
screen.blit('grass', (0,0))
tank.draw()
for wall in walls:
wall.draw()
for bullet in bullets:
bullet.draw()
def update():
original_x = tank.x
original_y = tank.y
move_tank()
check_tank_boundaries()
tank_wall_collision(original_x, original_y)
move_bullets()
wall_bullet_collision()
check_bullet_boundary()
def on_key_down(key):
if key == keys.SPACE:
create_new_bullet()
def move_tank():
if keyboard.left:
tank.x -= 5
tank.angle = 180
if keyboard.right:
tank.x += 5
tank.angle = 0
if keyboard.up:
tank.y -= 5
tank.angle = 90
if keyboard.down:
tank.y += 5
tank.angle = 270
def check_tank_boundaries():
if tank.right > WIDTH:
tank.right = WIDTH
if tank.left < 0:
tank.left = 0
if tank.top < 0:
tank.top = 0
if tank.bottom > HEIGHT:
tank.bottom = HEIGHT
def create_new_wall():
for i in range(16):
for j in range(10):
if randint(0, 100) > 50:
wall = Actor('wall')
wall.topleft = (i * 50, 50 + j * 50)
walls.append(wall)
def tank_wall_collision(original_x, original_y):
for wall in walls:
if tank.colliderect(wall):
tank.x = original_x
tank.y = original_y
def create_new_bullet():
bullet = Actor('bulletblue2')
bullet.pos = tank.pos
bullet.angle = tank.angle
bullets.append(bullet)
def move_bullets():
for bullet in bullets:
if bullet.angle == 0:
bullet.x += 5
if bullet.angle == 90:
bullet.y -= 5
if bullet.angle == 180:
bullet.x -= 5
if bullet.angle == 270:
bullet.y += 5
def wall_bullet_collision():
for bullet in bullets:
for wall in walls:
if bullet.colliderect(wall):
bullets.remove(bullet)
walls.remove(wall)
def check_bullet_boundary():
for bullet in bullets:
if bullet.x > WIDTH or bullet.x < 0 or bullet.y > HEIGHT or bullet.y < 0:
bullets.remove(bullet)
create_new_wall()
pgzrun.go()
'''
one enemy
'''
import pgzrun
from random import randint
WIDTH = 800
HEIGHT = 600
tank = Actor('tank_blue')
tank.x = WIDTH / 2
tank.bottom = HEIGHT
tank.angle = 90
walls = []
bullets = []
enemy = Actor('tank_red')
enemy.x = randint(100, WIDTH-100)
enemy.y = 20
step_count = 0
def draw():
screen.clear()
screen.blit('grass', (0,0))
tank.draw()
for wall in walls:
wall.draw()
for bullet in bullets:
bullet.draw()
enemy.draw()
def update():
original_x = tank.x
original_y = tank.y
move_tank()
check_tank_boundaries()
tank_wall_collision(original_x, original_y)
move_bullets()
wall_bullet_collision()
check_bullet_boundary()
enemy_action()
def on_key_down(key):
if key == keys.SPACE:
create_new_bullet()
def move_tank():
if keyboard.left:
tank.x -= 5
tank.angle = 180
if keyboard.right:
tank.x += 5
tank.angle = 0
if keyboard.up:
tank.y -= 5
tank.angle = 90
if keyboard.down:
tank.y += 5
tank.angle = 270
def check_tank_boundaries():
if tank.right > WIDTH:
tank.right = WIDTH
if tank.left < 0:
tank.left = 0
if tank.top < 0:
tank.top = 0
if tank.bottom > HEIGHT:
tank.bottom = HEIGHT
def create_new_wall():
for i in range(16):
for j in range(10):
if randint(0, 100) > 50:
wall = Actor('wall')
wall.topleft = (i * 50, 50 + j * 50)
walls.append(wall)
def tank_wall_collision(original_x, original_y):
for wall in walls:
if tank.colliderect(wall):
tank.x = original_x
tank.y = original_y
def create_new_bullet():
bullet = Actor('bulletblue2')
bullet.pos = tank.pos
bullet.angle = tank.angle
bullets.append(bullet)
def move_bullets():
for bullet in bullets:
if bullet.angle == 0:
bullet.x += 5
if bullet.angle == 90:
bullet.y -= 5
if bullet.angle == 180:
bullet.x -= 5
if bullet.angle == 270:
bullet.y += 5
def wall_bullet_collision():
for bullet in bullets:
for wall in walls:
if bullet.colliderect(wall):
bullets.remove(bullet)
walls.remove(wall)
def check_bullet_boundary():
for bullet in bullets:
if bullet.x > WIDTH or bullet.x < 0 or bullet.y > HEIGHT or bullet.y < 0:
bullets.remove(bullet)
def enemy_action():
global step_count
choice = randint(0, 2)
if step_count > 0:
step_count -= 1
if enemy.angle == 0:
enemy.x += 5
if enemy.angle == 90:
enemy.y -= 5
if enemy.angle == 180:
enemy.x -= 5
if enemy.angle == 270:
enemy.y += 5
elif choice == 0:
step_count = 20
elif choice == 1:
print("turn")
else:
print("shoot")
create_new_wall()
pgzrun.go()
'''
enemy shooting
'''
import pgzrun
from random import randint
WIDTH = 800
HEIGHT = 600
tank = Actor('tank_blue')
tank.x = WIDTH / 2
tank.bottom = HEIGHT
tank.angle = 90
walls = []
bullets = []
enemy = Actor('tank_red')
enemy.x = randint(100, WIDTH-100)
enemy.y = 20
step_count = 0
def draw():
screen.clear()
screen.blit('grass', (0,0))
tank.draw()
for wall in walls:
wall.draw()
for bullet in bullets:
bullet.draw()
enemy.draw()
def update():
original_x = tank.x
original_y = tank.y
move_tank()
check_tank_boundaries()
tank_wall_collision(original_x, original_y)
move_bullets()
wall_bullet_collision()
check_bullet_boundary()
enemy_action()
def on_key_down(key):
if key == keys.SPACE:
create_new_bullet()
def move_tank():
if keyboard.left:
tank.x -= 5
tank.angle = 180
if keyboard.right:
tank.x += 5
tank.angle = 0
if keyboard.up:
tank.y -= 5
tank.angle = 90
if keyboard.down:
tank.y += 5
tank.angle = 270
def check_tank_boundaries():
if tank.right > WIDTH:
tank.right = WIDTH
if tank.left < 0:
tank.left = 0
if tank.top < 0:
tank.top = 0
if tank.bottom > HEIGHT:
tank.bottom = HEIGHT
def create_new_wall():
for i in range(16):
for j in range(10):
if randint(0, 100) > 50:
wall = Actor('wall')
wall.topleft = (i * 50, 50 + j * 50)
walls.append(wall)
def tank_wall_collision(original_x, original_y):
for wall in walls:
if tank.colliderect(wall):
tank.x = original_x
tank.y = original_y
def create_new_bullet():
bullet = Actor('bulletblue2')
bullet.pos = tank.pos
bullet.angle = tank.angle
bullets.append(bullet)
def move_bullets():
for bullet in bullets:
if bullet.angle == 0:
bullet.x += 5
if bullet.angle == 90:
bullet.y -= 5
if bullet.angle == 180:
bullet.x -= 5
if bullet.angle == 270:
bullet.y += 5
def wall_bullet_collision():
for bullet in bullets:
for wall in walls:
if bullet.colliderect(wall):
bullets.remove(bullet)
walls.remove(wall)
def check_bullet_boundary():
for bullet in bullets:
if bullet.x > WIDTH or bullet.x < 0 or bullet.y > HEIGHT or bullet.y < 0:
bullets.remove(bullet)
def enemy_action():
global step_count
choice = randint(0, 2)
if step_count > 0:
step_count -= 1
if enemy.angle == 0:
enemy.x += 5
if enemy.angle == 90:
enemy.y -= 5
if enemy.angle == 180:
enemy.x -= 5
if enemy.angle == 270:
enemy.y += 5
elif choice == 0:
step_count = 20
elif choice == 1:
enemy.angle = randint(0, 3) * 90
else:
print("shoot")
create_new_wall()
pgzrun.go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment