Created
September 21, 2021 09:54
-
-
Save shinysu/9a9dadda38b71c20bfedde210c0e028c to your computer and use it in GitHub Desktop.
gist 3
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() |
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
''' | |
creating bullets | |
''' | |
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 = [] | |
bullets = [] | |
def draw(): | |
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_boundary() | |
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.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 | |
def create_new_bullet(): | |
bullet = Actor('bulletblue2') | |
bullet.pos = tank.pos | |
bullet.angle = tank.angle | |
bullets.append(bullet) | |
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
''' | |
moving bullets | |
''' | |
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 = [] | |
bullets = [] | |
def draw(): | |
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_boundary() | |
tank_wall_collision(original_x, original_y) | |
move_bullets() | |
check_bullet_boundary() | |
def on_key_down(key): | |
if key == keys.SPACE: | |
create_new_bullet() | |
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 | |
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 | |
elif bullet.angle == 90: | |
bullet.y -= 5 | |
elif bullet.angle == 180: | |
bullet.x -= 5 | |
elif bullet.angle == 270: | |
bullet.y += 5 | |
def check_bullet_boundary(): | |
for bullet in bullets: | |
if bullet.x < 0 or bullet.x > WIDTH or bullet.y < 0 or bullet.y > HEIGHT: | |
bullets.remove(bullet) | |
create_new_wall() | |
pgzrun.go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment