Created
September 21, 2021 09:36
-
-
Save shinysu/9653b35124a0eda87ae151cfcbcd46a8 to your computer and use it in GitHub Desktop.
gist2
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 = 800 | |
HEIGHT = 600 | |
tank = Actor('tank_blue') | |
tank.bottom = HEIGHT | |
tank.x = WIDTH / 2 | |
tank.angle = 90 | |
def draw(): | |
screen.blit('grass', (0, 0)) | |
tank.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 tank | |
''' | |
import pgzrun | |
WIDTH = 800 | |
HEIGHT = 600 | |
tank = Actor('tank_blue') | |
tank.bottom = HEIGHT | |
tank.x = WIDTH / 2 | |
tank.angle = 90 | |
def draw(): | |
screen.blit('grass', (0, 0)) | |
tank.draw() | |
def update(): | |
move_tank() | |
def move_tank(): | |
if keyboard.left: | |
tank.angle = 180 | |
tank.x -= 5 | |
elif keyboard.right: | |
tank.angle = 0 | |
tank.x += 5 | |
elif keyboard.up: | |
tank.angle = 90 | |
tank.y -= 5 | |
elif keyboard.down: | |
tank.angle = 270 | |
tank.y += 5 | |
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
''' | |
build the wall | |
''' | |
import pgzrun | |
WIDTH = 800 | |
HEIGHT = 600 | |
tank = Actor('tank_blue') | |
tank.bottom = HEIGHT | |
tank.x = WIDTH / 2 | |
tank.angle = 90 | |
walls = [] | |
def draw(): | |
screen.blit('grass', (0, 0)) | |
tank.draw() | |
for wall in walls: | |
wall.draw() | |
def update(): | |
move_tank() | |
def move_tank(): | |
if keyboard.left: | |
tank.angle = 180 | |
tank.x -= 5 | |
elif keyboard.right: | |
tank.angle = 0 | |
tank.x += 5 | |
elif keyboard.up: | |
tank.angle = 90 | |
tank.y -= 5 | |
elif keyboard.down: | |
tank.angle = 270 | |
tank.y += 5 | |
def create_new_wall(): | |
for i in range(16): | |
for j in range(12): | |
wall = Actor('wall') | |
wall.topleft = (i * 50, j * 50) | |
walls.append(wall) | |
create_new_wall() | |
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
''' | |
randomise the wall | |
''' | |
import pgzrun | |
from random import randint | |
WIDTH = 800 | |
HEIGHT = 600 | |
tank = Actor('tank_blue') | |
tank.bottom = HEIGHT | |
tank.x = WIDTH / 2 | |
tank.angle = 90 | |
walls = [] | |
def draw(): | |
screen.blit('grass', (0, 0)) | |
tank.draw() | |
for wall in walls: | |
wall.draw() | |
def update(): | |
move_tank() | |
check_tank_boundary() | |
def move_tank(): | |
if keyboard.left: | |
tank.angle = 180 | |
tank.x -= 5 | |
elif keyboard.right: | |
tank.angle = 0 | |
tank.x += 5 | |
elif keyboard.up: | |
tank.angle = 90 | |
tank.y -= 5 | |
elif keyboard.down: | |
tank.angle = 270 | |
tank.y += 5 | |
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 check_tank_boundary(): | |
if tank.left < 0: | |
tank.left = 0 | |
elif tank.right > WIDTH: | |
tank.right = WIDTH | |
if tank.y < 0: | |
tank.y = 0 | |
elif tank.y > HEIGHT: | |
tank.y = HEIGHT | |
create_new_wall() | |
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
''' | |
Block the tank while there is a wall | |
''' | |
import pgzrun | |
from random import randint | |
WIDTH = 800 | |
HEIGHT = 600 | |
tank = Actor('tank_blue') | |
tank.bottom = HEIGHT | |
tank.x = WIDTH / 2 | |
tank.angle = 90 | |
walls = [] | |
def draw(): | |
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_boundary() | |
tank_wall_collision(original_x, original_y) | |
def move_tank(): | |
if keyboard.left: | |
tank.angle = 180 | |
tank.x -= 5 | |
elif keyboard.right: | |
tank.angle = 0 | |
tank.x += 5 | |
elif keyboard.up: | |
tank.angle = 90 | |
tank.y -= 5 | |
elif keyboard.down: | |
tank.angle = 270 | |
tank.y += 5 | |
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 check_tank_boundary(): | |
if tank.left < 0: | |
tank.left = 0 | |
elif tank.right > WIDTH: | |
tank.right = WIDTH | |
if tank.y < 0: | |
tank.y = 0 | |
elif tank.y > HEIGHT: | |
tank.y = HEIGHT | |
def tank_wall_collision(original_x, original_y): | |
for wall in walls: | |
if wall.colliderect(tank): | |
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