Commit 1829bdc9 authored by harry.dale's avatar harry.dale

added gui, new enemy, powerup, start and game

over, level up fixed movement
parent 9efd9ac7
import pgzero, pgzrun, pygame
import math, sys, random
from myactors import Player, Monster, Bat, Bullet
from myactors import Player, Monster, Bat, Bullet, Big_monster, Demon, Health_Pack
from constants import *
from pygame.math import Vector2
from pgzero.builtins import keyboard, keys
class Game:
def __init__(self):
state = "start"
def __init__(self):
self.player = Player(HALF_LEVEL_W, HALF_LEVEL_H)
self.bullet = []
self.monster = []
self.timer = 0
self.timer2 = 0
self.timer3 = 0
self.Big_monster = []
self.health_pack = []
self.timer = 0 #bat spawn
self.timer2 = 0 #bullet spawn
self.timer3 = 0 #demon spawn
self.timer4 = 0 #health pack spawn
def draw(self,screen):
if Game.state == "start":
screen.fill((0, 0, 0))
font = pygame.font.SysFont('arial', 40)
title = font.render('Survivors', True, (255, 255, 255))
start_button = font.render('Press Space', True, (255, 255, 255))
screen.blit(title, (HALF_WINDOW_W - title.get_width()/2, HALF_WINDOW_H - title.get_height()/2))
screen.blit(start_button, (HALF_WINDOW_W - start_button.get_width()/2, HALF_WINDOW_H + start_button.get_height()/2))
pygame.display.update()
def draw(self,screen):
offset_x = max(0, min(LEVEL_W - WIDTH, self.player.vposx - WIDTH / 2))
offset_y = max(0, min(LEVEL_H - HEIGHT, self.player.vposy - HEIGHT / 2))
offset = Vector2(offset_x, offset_y)
screen.blit("pitch", (-offset_x, -offset_y))
self.player.draw(offset_x, offset_y)
for bullet in self.bullet:
bullet.draw(offset_x,offset_y)
for mob in self.monster:
mob.draw(offset_x, offset_y)
def update(self):
self.player.update()
elif Game.state == "over":
screen.fill((0, 0, 0))
font = pygame.font.SysFont('arial', 40)
title = font.render('Game Over', True, (255, 255, 255))
start_button = font.render('Press Space to restart', True, (255, 255, 255))
screen.blit(title, (HALF_WINDOW_W - title.get_width()/2, HALF_WINDOW_H - title.get_height()/2))
screen.blit(start_button, (HALF_WINDOW_W - start_button.get_width()/2, HALF_WINDOW_H + start_button.get_height()/2))
pygame.display.update()
else:
offset_x = max(0, min(LEVEL_W - WIDTH, self.player.vposx - WIDTH / 2))
offset_y = max(0, min(LEVEL_H - HEIGHT, self.player.vposy - HEIGHT / 2))
offset = Vector2(offset_x, offset_y)
#default images for gui
level = "level1"
gun_2 = "gun_2_locked"
gun_3 = "gun_3_locked"
gun_4 = "gun_4_locked"
#change gui images with progression
if self.player.level == 2:
level = "level2"
gun_2 = "gun_2"
if self.player.level == 3:
level = "level3"
gun_2 = "gun_2"
gun_3 = "gun_3"
if self.player.level == 4:
level = "level4"
gun_2 = "gun_2"
gun_3 = "gun_3"
gun_4 = "gun_4"
#health bar
health_width = self.player.health * 3
if self.player.health < 5:
health_width = 1
health_im = pygame.image.load("images/health_bar.png")
health_bar = pygame.Surface((health_width,21))
screen.blit("pitch", (-offset_x, -offset_y))
screen.blit("health_border", (0,0))
health_bar.blit(health_im, (0,0))
screen.blit(health_bar, (9,16))
screen.blit(level, (0,0))
screen.blit("gun_1_active", (0,0))
screen.blit(gun_2, (0,0))
screen.blit(gun_3, (0,0))
screen.blit(gun_4, (0,0))
screen.blit("ammo", (0,0))
self.timer += 1
if (self.timer == 20):
self.timer = 0
self.monster.append(Bat(self.screencoords()))
self.player.draw(offset_x, offset_y)
for bullet in self.bullet:
bullet.draw(offset_x,offset_y)
for mob in self.monster:
mob.draw(offset_x, offset_y)
for mob in self.Big_monster:
mob.draw(offset_x, offset_y)
for pack in self.health_pack:
posx = random.randint(30,970)
posy = random.randint(30,1370)
pack.draw(offset_x, offset_y)
self.timer2 += 1
if (self.timer2 == 10):
self.timer2 = 0
self.bullet.append(Bullet(self))
def update(self):
if Game.state == "start" or Game.state == "over":
if keyboard.SPACE:
Game.state = "play"
elif Game.state == "play":
self.player.update()
if self.player.health < 1:
Game.state = "over"
self.player.health = 100
self.player.level = 1
self.player.xp = int(0)
self.player = Player(HALF_LEVEL_W, HALF_LEVEL_H)
self.bullet = []
self.monster = []
self.Big_monster = []
self.health_pack = []
self.timer = 0
self.timer2 = 0
self.timer3 = 0
self.timer4 = 0
self.timer += 1
if (self.timer == 20):
self.timer = 0
self.monster.append(Bat(self.screencoords()))
self.timer3 +=1
if (self.timer3 == 300):
self.timer3 = 0
self.Big_monster.append(Demon(self.screencoords()))
self.timer2 += 1
if (self.timer2 == 10):
self.timer2 = 0
self.bullet.append(Bullet(self))
for bullet in self.bullet:
bullet.update()
#print("x: ", bullet.vposx, "y:", bullet.vposy)
if (bullet.vposy > 1373 or bullet.vposy < 26) or (bullet.vposx > 978 or bullet.vposx < 24):
self.bullet.remove(bullet)
if (bullet.despawn):
self.bullet.remove(bullet)
for mob in self.monster:
mob.update(self.player,self.bullet)
if (not mob.alive):
self.monster.remove(mob)
self.timer4 += 1
if (self.timer4 == 80):
self.health_pack.append(Health_Pack(self))
self.timer4 = 0
def screencoords(self):
left = int(max(0, min(LEVEL_W - WIDTH, self.player.vposx - WIDTH / 2)))
top = int(max(0, min(LEVEL_H - HEIGHT, self.player.vposy - HEIGHT / 2)))
right = int(max(0, min(LEVEL_W + WIDTH, self.player.vposx + WIDTH / 2)))
bottom = int(max(0, min(LEVEL_H + HEIGHT, self.player.vposy + HEIGHT / 2)))
coords = [left, top, right, bottom]
return coords
for pack in self.health_pack:
pack.update(self.player)
if (not pack.alive):
self.health_pack.remove(pack)
for bullet in self.bullet:
bullet.update()
#print("x: ", bullet.vposx, "y:", bullet.vposy)
if (bullet.vposy > 1373 or bullet.vposy < 26) or (bullet.vposx > 978 or bullet.vposx < 24):
if bullet not in self.bullet:
pass
else: self.bullet.remove(bullet)
if (bullet.despawn):
if bullet not in self.bullet:
pass
else: self.bullet.remove(bullet)
for mob in self.monster:
mob.update(self.player,self.bullet)
if (not mob.alive):
self.monster.remove(mob)
for mob in self.Big_monster:
mob.update(self.player,self.bullet)
if (not mob.alive):
self.Big_monster.remove(mob)
'''def on_mouse_move(self, pos):
self.player.on_mouse_move(pos)'''
def screencoords(self):
left = int(max(0, min(LEVEL_W - WIDTH, self.player.vposx - WIDTH / 2)))
top = int(max(0, min(LEVEL_H - HEIGHT, self.player.vposy - HEIGHT / 2)))
right = int(max(0, min(LEVEL_W + WIDTH, self.player.vposx + WIDTH / 2)))
bottom = int(max(0, min(LEVEL_H + HEIGHT, self.player.vposy + HEIGHT / 2)))
coords = [left, top, right, bottom]
return coords
#def on_mouse_move(self, pos): self.player.on_mouse_move(pos)'''
images/princess_1.png

400 Bytes | W: | H:

images/princess_1.png

557 Bytes | W: | H:

images/princess_1.png
images/princess_1.png
images/princess_1.png
images/princess_1.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -14,45 +14,53 @@ class MyActor(Actor):
self.timer = 0
self.olddx = -100
self.olddirection = -100
self.direction = 1
def update(self):
self.timer += 1
direction = 1
self.timer += 1
if (self.dy == 0):
if (self.dx < 0):
direction = 4 #left
self.direction = 4 #left
hold = 4
elif (self.dx > 0):
direction = 7 #right
self.direction = 7 #right
hold = 7
elif (self.dx == 0):
if (self.dy < 0):
direction = 10 #up
self.direction = 10 #up
hold = 10
elif (self.dy > 0):
direction = 1 #down
self.direction = 1 #down
hold = 1
elif (self.dy > 0):
if (self.dx > 0):
direction = 13 #DR
self.direction = 13 #DR
hold = 13
elif (self.dx < 0):
direction = 16 # DL
self.direction = 16 # DL
hold = 16
elif (self.dy < 0):
if (self.dx > 0):
direction = 19 #UR
self.direction = 19 #UR
hold = 19
elif (self.dx < 0):
direction = 22 # UL
if (self.olddirection != direction):
self.imgno = direction
self.direction = 22 # UL
hold = 22
elif (self.dx == 0) and (self.dy == 0):
self.direction = hold
if (self.olddirection != self.direction):
self.imgno = self.direction
if (self.timer==10):
self.timer = 0
if (self.olddirection == direction):
if (self.olddirection == self.direction):
self.imgno +=1
if ((self.imgno==4) or (self.imgno==7) or (self.imgno==10) or (self.imgno==13) or (self.imgno==16) or (self.imgno==19) or (self.imgno==22) or (self.imgno==25)):
self.imgno -= 3
self.image = f'{self.myimg}_{self.imgno}'
self.olddx = self.dx
self.olddirection = direction
self.olddirection = self.direction
if self.dx > 1:
self.dx = 1
......@@ -80,12 +88,17 @@ class Player(MyActor):
def __init__(self, x, y):
self.img = "princess"
self.health = 100
self.level = 1
self.xp = int(0)
super().__init__(self.img,x,y,5)
def update(self):
#Return vector representing amount of movement that should occur
self.dx, self.dy = 0, 0
if (int(self.xp) == int(20*self.level*self.level)):
self.level += 1
print(self.xp)
print(self.level)
if keyboard.a:
self.dx = -1
elif keyboard.d:
......@@ -117,6 +130,13 @@ class Player(MyActor):
if (self.health<=0):
print("game over")
def heal(self,value):
self.health += value
if self.health > 99:
self.health = 100
def xp_up(self, amount):
self.xp += amount
class Monster(MyActor):
def __init__(self, img, posx, posy,spd):
......@@ -138,6 +158,9 @@ class Monster(MyActor):
if (self.colliderect(aBullet)):
self.hurt(1)
aBullet.despawn = True
if self.alive == False:
player.xp_up(2)
......@@ -186,66 +209,153 @@ class Bat(Monster):
self.dy = 0
super().update(player,bullets)
class Big_monster(MyActor):
def __init__(self, img, posx, posy,spd):
super().__init__(img, posx, posy, spd)
spd = 0.25
self.alive = True
def hurt(self,damage):
self.health -= damage
if self.health <= 0:
self.alive = False
def update(self,player,bullets):
# Return vector representing amount of movement that should occur
super().update()
if (self.colliderect(player)):
player.hurt(30)
self.alive = False
for aBullet in bullets:
if (self.colliderect(aBullet)):
self.hurt(1)
aBullet.despawn = True
if self.alive == False:
player.xp_up(5)
class Demon(Big_monster):
def __init__(self, screencoords):
LEFT = 0
TOP = 1
RIGHT = 2
BOTTOM = 3
self.health = 10
if self.health <= 0:
self.alive = False
side = random.randint(0,3)
if (side == LEFT):
posx = max(screencoords[LEFT] - 50, 0)
posy = random.randint(screencoords[TOP],screencoords[BOTTOM])
elif (side == TOP):
posx = random.randint(screencoords[LEFT],screencoords[RIGHT])
posy = max(screencoords[TOP] - 50, 0)
elif (side == RIGHT):
posx = min(screencoords[RIGHT] + 50, LEVEL_W)
posy = random.randint(screencoords[TOP],screencoords[BOTTOM])
elif (side == BOTTOM):
posx = random.randint(screencoords[LEFT],screencoords[RIGHT])
posy = min(screencoords[BOTTOM] + 50, LEVEL_H)
super().__init__("demon", posx, posy, 1)
def update(self,player,bullets):
if (self.vposx > player.vposx):
self.dx = -0.25
elif (self.vposx < player.vposx):
self.dx = 0.25
else:
self.dx = 0
if (self.vposy > player.vposy):
self.dy = -0.125
elif (self.vposx < player.vposy):
self.dy = 0.125
else:
self.dy = 0
super().update(player,bullets)
class Bullet(MyActor):
def __init__(self, theGame):
self.img = "bullet"
super().__init__(self.img, 0,0, 10)
gun_1 = True
if gun_1:
self.img = "bullet"
super().__init__(self.img, 0,0, 10)
self.despawn = False
self.despawn = False
if theGame.player.olddirection == 7: #right
x = theGame.player.vposx - 5
y = theGame.player.vposy + 5
self.dx = 1
self.dy = 0
self.img = "bullet_2"
elif theGame.player.olddirection == 10: #up
x = theGame.player.vposx + 5
y = theGame.player.vposy - 5
self.dx = 0
self.dy = -1
self.img = "bullet_4"
elif theGame.player.olddirection == 4: #left
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = -1
self.dy = 0
self.img = "bullet_1"
if theGame.player.olddirection == 7: #right
x = theGame.player.vposx - 5
y = theGame.player.vposy + 5
self.dx = 1
self.dy = 0
self.img = "bullet_2"
elif theGame.player.olddirection == 10: #up
x = theGame.player.vposx + 5
y = theGame.player.vposy - 5
self.dx = 0
self.dy = -1
self.img = "bullet_4"
elif theGame.player.olddirection == 4: #left
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = -1
self.dy = 0
self.img = "bullet_1"
elif theGame.player.olddirection == 1:
x = theGame.player.vposx - 5 #down
y = theGame.player.vposy + 5
self.dx = 0
self.dy = 1
self.img = "bullet_3"
elif theGame.player.olddirection == 13: #DR
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = 1
self.dy = 1
self.img = "bullet_13"
elif theGame.player.olddirection == 16: #DL
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = -1
self.dy = 1
self.img = "bullet_16"
elif theGame.player.olddirection == 19: #left
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = 1
self.dy = -1
self.img = "bullet_22"
else:
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = -1
self.dy = -1
self.img = "bullet_22"
self.img = "bullet"
self.vposx, self.vposy = x, y
elif theGame.player.olddirection == 1:
x = theGame.player.vposx - 5 #down
y = theGame.player.vposy + 5
self.dx = 0
self.dy = 1
self.img = "bullet_3"
elif theGame.player.olddirection == 13: #DR
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = 1
self.dy = 1
self.img = "bullet_13"
elif theGame.player.olddirection == 16: #DL
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = -1
self.dy = 1
self.img = "bullet_16"
elif theGame.player.olddirection == 19: #left
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = 1
self.dy = -1
self.img = "bullet_22"
else:
x = theGame.player.vposx + 5
y = theGame.player.vposy + 5
self.dx = -1
self.dy = -1
self.img = "bullet_22"
self.img = "bullet"
self.vposx, self.vposy = x, y
super().update()
class Health_Pack(MyActor):
def __init__(self, img):
self.img = "health_pack"
self.alive = True
super().__init__("health_pack", random.randint(30,970), random.randint(30,1370), 0)
super().update()
\ No newline at end of file
def update(self,player):
self.dx, self.dy = 0, 0
super().update()
if (self.colliderect(player)):
player.heal(30)
self.alive = False
\ No newline at end of file
......@@ -3,6 +3,7 @@ import math, sys, random
from enum import Enum
from game import Game
from constants import *
from pgzero.builtins import keyboard, keys
if sys.version_info < (3,5):
print("This game requires at least version 3.5 of Python. Please download it from www.python.org")
......@@ -16,10 +17,10 @@ if pgzero_version < [1,2]:
def update():
game.update()
def draw():
game.draw(screen)
'''def on_mouse_move(pos):
game.on_mouse_move(pos)'''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment