Last active
April 9, 2024 20:30
Zombie Survival Python (Hope you enjoy)
This file contains 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 turtle | |
import time | |
import random | |
import math | |
print('"Zombie Survival (3.2)" made by Braydan Smith') | |
print() | |
print("Would you like a tutorial? Enter 'yes' or 'no' (and DO NOTHING ELSE!).") | |
awnser=input(":") | |
awnser=awnser.lower() | |
if "yes" in awnser: | |
print() | |
print("You are playing as the blue dude, trying to avoid the green zombie.") | |
time.sleep(3) | |
print() | |
print("Use wasd to move or unpause, and the arrow keys to shoot in different directions.") | |
time.sleep(4) | |
print() | |
print("Press any other key to pause.") | |
time.sleep(4) | |
print() | |
print("You cannot shoot again until the bullet is off the screen.") | |
time.sleep(4) | |
print() | |
print("When you shoot the zombie, you gain 10 points and the game speeds up slightly.") | |
time.sleep(4) | |
print() | |
print("You can travel off the screen to teleport to the opposite side.") | |
time.sleep(4) | |
print() | |
print("If the zombie reaches you, you die.") | |
print() | |
time.sleep(4) | |
print("The game will start in 5 seconds.") | |
print() | |
print("5...") | |
time.sleep(1) | |
print() | |
print("4...") | |
time.sleep(1) | |
print() | |
print("3...") | |
time.sleep(1) | |
print() | |
print("2...") | |
time.sleep(1) | |
print() | |
print("1...") | |
time.sleep(1) | |
elif "yes" not in awnser: | |
print("The game will start in 3 seconds.") | |
time.sleep(3) | |
score=0 | |
delay = 0.05 | |
high_score = 0 | |
gamemode="play" | |
wn = turtle.Screen() | |
wn.title("Zombie Survival") | |
wn.bgcolor("black") | |
wn.setup(width=600, height=600) | |
wn.tracer(0) | |
pen = turtle.Turtle() | |
pen.speed(0) | |
pen.color("white") | |
pen.penup() | |
pen.setposition(-300,-300) | |
pen.pendown() | |
pen.pensize(3) | |
for side in range(4): | |
pen.fd(600) | |
pen.lt(90) | |
pen.penup() | |
pen.hideturtle() | |
pen.goto(0,250) | |
pen.pendown() | |
pen.write("Score: 0 High Score: 0", align="center", | |
font=("candara", 24, "bold")) | |
player = turtle.Turtle() | |
player.shape("square") | |
player.color("navy blue") | |
player.penup() | |
player.goto(0,0) | |
player.direction = "Stop" | |
zombie = turtle.Turtle() | |
zombie.shape("square") | |
zombie.color("lime") | |
zombie.penup() | |
x = random.randint(-300, 300) | |
y = random.randint(-300,300) | |
zombie.setposition(x,y) | |
zombiesize=26 | |
bullet = turtle.Turtle() | |
bullet.penup() | |
bullet.shape("circle") | |
bullet.color("red") | |
bullet.shapesize(0.5) | |
bullet.hideturtle() | |
bulletspeed = 15 | |
bulletstate = "ready" | |
def fbu(): | |
global bulletstate | |
if bulletstate == "ready" and player.direction!="Stop": | |
bulletstate = "fu" | |
x = player.xcor() | |
y = player.ycor()+1 | |
bullet.setposition(x,y) | |
bullet.showturtle() | |
def fbd(): | |
global bulletstate | |
if bulletstate == "ready" and player.direction!="Stop": | |
bulletstate = "fd" | |
x = player.xcor() | |
y = player.ycor()-1 | |
bullet.setposition(x,y) | |
bullet.showturtle() | |
def fbl(): | |
global bulletstate | |
if bulletstate == "ready" and player.direction!="Stop": | |
bulletstate = "fl" | |
x = player.xcor()-1 | |
y = player.ycor() | |
bullet.setposition(x,y) | |
bullet.showturtle() | |
def fbr(): | |
global bulletstate | |
if bulletstate == "ready" and player.direction!="Stop": | |
bulletstate = "fr" | |
x = player.xcor()+1 | |
y = player.ycor() | |
bullet.setposition(x,y) | |
bullet.showturtle() | |
def isCollision(bullet, zombie): | |
distance = math.sqrt(math.pow(bullet.xcor()-zombie.xcor(),2)+math.pow(bullet.ycor()-zombie.ycor(),2)) | |
if distance < zombiesize: | |
return True | |
else: | |
return False | |
def goup(): | |
player.direction = "up" | |
def godown(): | |
player.direction = "down" | |
def goleft(): | |
player.direction = "left" | |
def goright(): | |
player.direction = "right" | |
def move(): | |
if player.direction == "up": | |
y = player.ycor() | |
player.sety(y+7) | |
if player.direction == "down": | |
y = player.ycor() | |
player.sety(y-7) | |
if player.direction == "left": | |
x = player.xcor() | |
player.setx(x-7) | |
if player.direction == "right": | |
x = player.xcor() | |
player.setx(x+7) | |
def movezombie(): | |
if player.direction != "Stop": | |
if player.ycor()>zombie.ycor(): | |
y = zombie.ycor() | |
zombie.sety(y+3.5) | |
if player.ycor()<zombie.ycor(): | |
y = zombie.ycor() | |
zombie.sety(y-3.5) | |
if player.xcor()<zombie.xcor(): | |
x = zombie.xcor() | |
zombie.setx(x-3.5) | |
if player.xcor()>zombie.xcor(): | |
x = zombie.xcor() | |
zombie.setx(x+3.5) | |
def pause(): | |
player.direction="Stop" | |
wn.listen() | |
wn.onkeypress(goup,"w") | |
wn.onkeypress(godown, "s") | |
wn.onkeypress(goleft, "a") | |
wn.onkeypress(goright, "d") | |
wn.onkeypress(fbu, "Up") | |
wn.onkeypress(fbd, "Down") | |
wn.onkeypress(fbl, "Left") | |
wn.onkeypress(fbr, "Right") | |
wn.onkeypress(pause, " ") | |
while True: | |
wn.update() | |
move() | |
movezombie() | |
time.sleep(delay) | |
if isCollision(bullet,zombie): | |
if bulletstate != "ready": | |
bullet.ht() | |
bulletstate = "ready" | |
bullet.setposition(0,-400) | |
die=random.choice(["a","b","c","d"]) | |
x1=player.xcor()-100 | |
x2=player.xcor()+100 | |
y1=player.ycor()-100 | |
y2=player.ycor()+100 | |
zombie.ht() | |
if die=="a": | |
if player.xcor()>-200 and player.ycor()<200: | |
x3=random.randint(-300, x1) | |
y3=random.randint(y2, 300) | |
elif player.xcor()<-199 or player.ycor()>199: | |
die=random.choice(["a","b","c","d"]) | |
if die=="b": | |
if player.xcor()<200 and player.ycor()<200: | |
x3=random.randint(x2, 300) | |
y3=random.randint(y2, 300) | |
elif player.xcor()>199 or player.ycor()>199: | |
die=random.choice(["a","b","c","d"]) | |
if die=="c": | |
if player.xcor()>-200 and player.ycor()>-200: | |
x3=random.randint(-300, x1) | |
y3=random.randint(-300,y1) | |
elif player.xcor()<-199 or player.ycor()<-199: | |
die=random.choice(["a","b","c","d"]) | |
if die=="d": | |
if player.xcor()<200 and player.ycor()>-200: | |
x3=random.randint(x2, 300) | |
y3=random.randint(-300,y1) | |
elif player.xcor()>199 or player.ycor()<-299: | |
die=random.choice(["a","b","c","d"]) | |
zombie.setposition(x3,y3) | |
zombie.st() | |
delay -= 0.0005 | |
score += 10 | |
if score > 990: | |
delay = 0.0005 | |
zombiesize=75 | |
zombie.shapesize(3,3) | |
zombie.color("red") | |
if score > high_score: | |
high_score = score | |
pen.clear() | |
pen.penup() | |
pen.setposition(-300,-300) | |
pen.pendown() | |
pen.pensize(3) | |
for side in range(4): | |
pen.fd(600) | |
pen.lt(90) | |
pen.penup() | |
pen.hideturtle() | |
pen.goto(0,250) | |
pen.pendown() | |
pen.write("Score: {} High Score: {}".format( | |
score, high_score), align = "center", font=("candara", 24, "bold")) | |
if isCollision(zombie,player): | |
player.ht() | |
zombie.ht() | |
player.goto(0,0) | |
player.direction = "Stop" | |
x = random.randint(-200, 200) | |
y = random.randint(100,250) | |
zombie.setposition(x,y) | |
player.st() | |
zombie.st() | |
score = 0 | |
delay = 0.05 | |
pen.clear() | |
pen.penup() | |
pen.setposition(-300,-300) | |
pen.pendown() | |
pen.pensize(3) | |
for side in range(4): | |
pen.fd(600) | |
pen.lt(90) | |
pen.penup() | |
pen.hideturtle() | |
pen.goto(0,250) | |
pen.pendown() | |
pen.write("Score: {} High Score: {}".format( | |
score, high_score), align="center", font=("candara", 24, "bold")) | |
if player.direction!="Stop": | |
if bulletstate == "fu": | |
y = bullet.ycor() | |
y += bulletspeed | |
bullet.sety(y) | |
if bulletstate == "fd": | |
y = bullet.ycor() | |
y -= bulletspeed | |
bullet.sety(y) | |
if bulletstate == "fl": | |
x = bullet.xcor() | |
x -= bulletspeed | |
bullet.setx(x) | |
if bulletstate == "fr": | |
x = bullet.xcor() | |
x += bulletspeed | |
bullet.setx(x) | |
if bullet.ycor()>300 or bullet.ycor()<-300 or bullet.xcor()<-300 or bullet.xcor()>300: | |
bullet.hideturtle() | |
bulletstate = "ready" | |
if player.ycor()>300: | |
x = player.xcor() | |
y = player.ycor()-600 | |
player.hideturtle() | |
player.goto(x,y) | |
player.showturtle() | |
if player.ycor()<-300: | |
x = player.xcor() | |
y = player.ycor()+600 | |
player.hideturtle() | |
player.goto(x,y) | |
player.showturtle() | |
if player.xcor()<-300: | |
x = player.xcor()+600 | |
y = player.ycor() | |
player.hideturtle() | |
player.goto(x,y) | |
player.showturtle() | |
if player.xcor()>300: | |
x = player.xcor()-600 | |
y = player.ycor() | |
player.hideturtle() | |
player.goto(x,y) | |
player.showturtle() | |
if score < 1000: | |
zombiesize=26 | |
zombie.shapesize(1,1) | |
zombie.color("lime") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment