Created
September 21, 2021 10:18
-
-
Save shinysu/da66ca6f3493deb2959f3f9fdfbeea7d to your computer and use it in GitHub Desktop.
one enemy
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
''' | |
One enemy | |
''' | |
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 = [] | |
enemy = Actor('tank_red') | |
enemy.x = randint(100, WIDTH-100) | |
enemy.y = 20 | |
step_count = 0 | |
def draw(): | |
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_boundary() | |
tank_wall_collision(original_x, original_y) | |
move_bullets() | |
check_bullet_boundary() | |
wall_bullet_collision() | |
enemy_action() | |
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) | |
def wall_bullet_collision(): | |
for wall in walls: | |
for bullet in bullets: | |
if wall.colliderect(bullet): | |
walls.remove(wall) | |
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 | |
elif enemy.angle == 90: | |
enemy.y -= 5 | |
elif enemy.angle == 180: | |
enemy.x -= 5 | |
elif 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment