Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created September 21, 2021 06:50
Show Gist options
  • Save shinysu/c6584af74770298252c090d786f3d939 to your computer and use it in GitHub Desktop.
Save shinysu/c6584af74770298252c090d786f3d939 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 = []
def draw():
screen.clear()
screen.blit('grass', (0,0))
tank.draw()
for wall in walls:
wall.draw()
def update():
original_x = tank.x
original_y = tank.y
move_tank()
check_tank_boundaries()
tank_wall_collision(original_x, original_y)
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
create_new_wall()
pgzrun.go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment