Commit 9efd9ac7 authored by harry.dale's avatar harry.dale

gun added as class template etc needs to be prettied up but working for now :)

parent b2b27542
import pgzero, pgzrun, pygame import pgzero, pgzrun, pygame
import math, sys, random import math, sys, random
from myactors import Player, Monster, Bat from myactors import Player, Monster, Bat, Bullet
from constants import * from constants import *
from pygame.math import Vector2 from pygame.math import Vector2
class Game: class Game:
def __init__(self): def __init__(self):
self.player = Player(HALF_LEVEL_W, HALF_LEVEL_H) self.player = Player(HALF_LEVEL_W, HALF_LEVEL_H)
self.bullet = []
self.monster = [] self.monster = []
self.timer = 0 self.timer = 0
self.timer2 = 0
self.timer3 = 0
def draw(self,screen): def draw(self,screen):
...@@ -19,6 +23,8 @@ class Game: ...@@ -19,6 +23,8 @@ class Game:
screen.blit("pitch", (-offset_x, -offset_y)) screen.blit("pitch", (-offset_x, -offset_y))
self.player.draw(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: for mob in self.monster:
mob.draw(offset_x, offset_y) mob.draw(offset_x, offset_y)
...@@ -30,8 +36,22 @@ class Game: ...@@ -30,8 +36,22 @@ class Game:
self.timer = 0 self.timer = 0
self.monster.append(Bat(self.screencoords())) self.monster.append(Bat(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: for mob in self.monster:
mob.update(self.player) mob.update(self.player,self.bullet)
if (not mob.alive): if (not mob.alive):
self.monster.remove(mob) self.monster.remove(mob)
...@@ -43,3 +63,5 @@ class Game: ...@@ -43,3 +63,5 @@ class Game:
coords = [left, top, right, bottom] coords = [left, top, right, bottom]
return coords return coords
'''def on_mouse_move(self, pos):
self.player.on_mouse_move(pos)'''
images/princess_8.png

359 Bytes | W: | H:

images/princess_8.png

398 Bytes | W: | H:

images/princess_8.png
images/princess_8.png
images/princess_8.png
images/princess_8.png
  • 2-up
  • Swipe
  • Onion skin
...@@ -18,16 +18,27 @@ class MyActor(Actor): ...@@ -18,16 +18,27 @@ class MyActor(Actor):
def update(self): def update(self):
self.timer += 1 self.timer += 1
direction = 1
if (self.dy == 0):
if (self.dx < 0): if (self.dx < 0):
direction = 4 direction = 4 #left
elif (self.dx > 0):
direction = 7 #right
elif (self.dx == 0): elif (self.dx == 0):
if (self.dy < 0): if (self.dy < 0):
direction = 10 direction = 10 #up
elif (self.dy >= 0): elif (self.dy > 0):
direction = 1 direction = 1 #down
else: elif (self.dy > 0):
direction = 7 if (self.dx > 0):
direction = 13 #DR
elif (self.dx < 0):
direction = 16 # DL
elif (self.dy < 0):
if (self.dx > 0):
direction = 19 #UR
elif (self.dx < 0):
direction = 22 # UL
if (self.olddirection != direction): if (self.olddirection != direction):
self.imgno = direction self.imgno = direction
...@@ -36,14 +47,22 @@ class MyActor(Actor): ...@@ -36,14 +47,22 @@ class MyActor(Actor):
self.timer = 0 self.timer = 0
if (self.olddirection == direction): if (self.olddirection == direction):
self.imgno +=1 self.imgno +=1
if ((self.imgno==4) or (self.imgno==7) or (self.imgno==10) or (self.imgno==13)): 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.imgno -= 3
self.image = f'{self.myimg}_{self.imgno}' self.image = f'{self.myimg}_{self.imgno}'
self.olddx = self.dx self.olddx = self.dx
self.olddirection = direction self.olddirection = direction
if self.dx > 1:
self.dx = 1
if self.dx < -1:
self.dx = -1
if self.dy > 1:
self.dy = 1
if self.dy < -1:
self.dy = -1
# Return vector representing amount of movement that should occur # Return vector representing amount of movement that should occur
self.dx = self.dx * self.speed self.dx = self.dx * self.speed
self.dy = self.dy * self.speed self.dy = self.dy * self.speed
...@@ -64,8 +83,9 @@ class Player(MyActor): ...@@ -64,8 +83,9 @@ class Player(MyActor):
super().__init__(self.img,x,y,5) super().__init__(self.img,x,y,5)
def update(self): def update(self):
# Return vector representing amount of movement that should occur #Return vector representing amount of movement that should occur
self.dx, self.dy = 0, 0 self.dx, self.dy = 0, 0
if keyboard.a: if keyboard.a:
self.dx = -1 self.dx = -1
elif keyboard.d: elif keyboard.d:
...@@ -77,6 +97,21 @@ class Player(MyActor): ...@@ -77,6 +97,21 @@ class Player(MyActor):
super().update() super().update()
#def on_mouse_move(self, pos):
''' if pos[0] > self.x:
self.dx = 1
elif pos[1] / self.y == 1:
self.dx = 0
else:
self.dx = -1
if pos[1] > self .y:
self.dy = 1
elif pos[0] / self.x == 1:
self.dy = 0
else:
self.dy = -1 '''
def hurt(self,damage): def hurt(self,damage):
self.health -= damage self.health -= damage
if (self.health<=0): if (self.health<=0):
...@@ -87,14 +122,24 @@ class Monster(MyActor): ...@@ -87,14 +122,24 @@ class Monster(MyActor):
def __init__(self, img, posx, posy,spd): def __init__(self, img, posx, posy,spd):
super().__init__(img, posx, posy, spd) super().__init__(img, posx, posy, spd)
self.alive = True self.alive = True
def hurt(self,damage):
self.health -= damage
if self.health <= 0:
self.alive = False
def update(self,player): def update(self,player,bullets):
# Return vector representing amount of movement that should occur # Return vector representing amount of movement that should occur
super().update() super().update()
if (self.colliderect(player)): if (self.colliderect(player)):
player.hurt(10) player.hurt(10)
self.alive = False self.alive = False
for aBullet in bullets:
if (self.colliderect(aBullet)):
self.hurt(1)
aBullet.despawn = True
class Bat(Monster): class Bat(Monster):
def __init__(self, screencoords): def __init__(self, screencoords):
...@@ -104,6 +149,10 @@ class Bat(Monster): ...@@ -104,6 +149,10 @@ class Bat(Monster):
RIGHT = 2 RIGHT = 2
BOTTOM = 3 BOTTOM = 3
self.health = 1
if self.health <= 0:
self.alive = False
side = random.randint(0,3) side = random.randint(0,3)
if (side == LEFT): if (side == LEFT):
...@@ -121,7 +170,7 @@ class Bat(Monster): ...@@ -121,7 +170,7 @@ class Bat(Monster):
super().__init__("bat", posx, posy, 1) super().__init__("bat", posx, posy, 1)
def update(self,player): def update(self,player,bullets):
if (self.vposx > player.vposx): if (self.vposx > player.vposx):
self.dx = -1 self.dx = -1
...@@ -136,7 +185,67 @@ class Bat(Monster): ...@@ -136,7 +185,67 @@ class Bat(Monster):
else: else:
self.dy = 0 self.dy = 0
super().update(player) super().update(player,bullets)
class Bullet(MyActor):
def __init__(self, theGame):
self.img = "bullet"
super().__init__(self.img, 0,0, 10)
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"
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()
\ No newline at end of file
...@@ -20,7 +20,8 @@ def update(): ...@@ -20,7 +20,8 @@ def update():
def draw(): def draw():
game.draw(screen) game.draw(screen)
'''def on_mouse_move(pos):
game.on_mouse_move(pos)'''
game = Game() game = Game()
pgzrun.go() pgzrun.go()
\ No newline at end of file
from pgzero.builtins import Actor, keyboard, keys
import math, sys, random
from constants import *
class MyActor(Actor):
def __init__(self,img,x,y,speed):
self.myimg = img
self.imgno = 1
myimg = f'{self.myimg}_{self.imgno}'
super().__init__(myimg, (x,y))
self.vposx, self.vposy = x, y
self.dx, self.dy = 0, 0
self.speed = speed
self.timer = 0
self.olddx = -100
self.olddirection = -100
def update(self):
self.timer += 1
if (self.dx < 0):
direction = 4
elif (self.dx == 0):
if (self.dy < 0):
direction = 10
elif (self.dy >= 0):
direction = 1
else:
direction = 7
if (self.olddirection != direction):
self.imgno = direction
if (self.timer==10):
self.timer = 0
if (self.olddirection == direction):
self.imgno +=1
if ((self.imgno==4) or (self.imgno==7) or (self.imgno==10) or (self.imgno==13)):
self.imgno -= 3
self.image = f'{self.myimg}_{self.imgno}'
self.olddx = self.dx
self.olddirection = direction
# Return vector representing amount of movement that should occur
self.dx = self.dx * self.speed
self.dy = self.dy * self.speed
self.vposx += self.dx
self.vposx = max(0+PLAYER_W,min(self.vposx, LEVEL_W-PLAYER_W))
self.vposy += self.dy
self.vposy = max(0+PLAYER_H,min(self.vposy, LEVEL_H-PLAYER_H))
def draw(self, offset_x, offset_y):
self.pos = (self.vposx - offset_x, self.vposy - offset_y)
super().draw()
class Player(MyActor):
def __init__(self, x, y):
self.img = "princess"
self.health = 100
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 keyboard.a:
self.dx = -1
elif keyboard.d:
self.dx = 1
if keyboard.w:
self.dy = -1
elif keyboard.s:
self.dy = 1
super().update()
def hurt(self,damage):
self.health -= damage
if (self.health<=0):
print("game over")
class Monster(MyActor):
def __init__(self, img, posx, posy,spd):
super().__init__(img, posx, posy, spd)
self.alive = True
def update(self,player):
# Return vector representing amount of movement that should occur
super().update()
if (self.colliderect(player)):
player.hurt(10)
self.alive = False
class Bat(Monster):
def __init__(self, screencoords):
LEFT = 0
TOP = 1
RIGHT = 2
BOTTOM = 3
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__("bat", posx, posy, 1)
def update(self,player):
if (self.vposx > player.vposx):
self.dx = -1
elif (self.vposx < player.vposx):
self.dx = 1
else:
self.dx = 0
if (self.vposy > player.vposy):
self.dy = -0.5
elif (self.vposx < player.vposy):
self.dy = 0.5
else:
self.dy = 0
super().update(player)
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