Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created September 21, 2021 07:09
Show Gist options
  • Save shinysu/a70edb3ae57cc862b9119b4a354aa768 to your computer and use it in GitHub Desktop.
Save shinysu/a70edb3ae57cc862b9119b4a354aa768 to your computer and use it in GitHub Desktop.
gist 2
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment