Commit a6245548 authored by harry.dale's avatar harry.dale

initial commit

push

initialcommit
parents
WIDTH = 800
HEIGHT = 480
HALF_WINDOW_W = WIDTH / 2
HALF_WINDOW_H = HEIGHT / 2
# Size of level, including both the pitch and the boundary surrounding it
LEVEL_W = 1000
LEVEL_H = 1400
HALF_LEVEL_W = LEVEL_W // 2
HALF_LEVEL_H = LEVEL_H // 2
PLAYER_W = 20
PLAYER_H = 25
\ No newline at end of file
import pgzero, pgzrun, pygame
import math, sys, random
from myactors import Player, Monster, Bat
from constants import *
from pygame.math import Vector2
class Game:
def __init__(self):
self.player = Player(HALF_LEVEL_W, HALF_LEVEL_H)
self.monster = []
self.timer = 0
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 mob in self.monster:
mob.draw(offset_x, offset_y)
def update(self):
self.player.update()
self.timer += 1
if (self.timer == 20):
self.timer = 0
self.monster.append(Bat(self.screencoords()))
for mob in self.monster:
mob.update(self.player)
if (not mob.alive):
self.monster.remove(mob)
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
This diff is collapsed.
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)
import pgzero, pgzrun, pygame
import math, sys, random
from enum import Enum
from game import Game
from constants import *
if sys.version_info < (3,5):
print("This game requires at least version 3.5 of Python. Please download it from www.python.org")
sys.exit()
pgzero_version = [int(s) if s.isnumeric() else s for s in pgzero.__version__.split('.')]
if pgzero_version < [1,2]:
print("This game requires at least version 1.2 of Pygame Zero. You have version {0}. Please upgrade using the command 'pip3 install --upgrade pgzero'".format(pgzero.__version__))
sys.exit()
def update():
game.update()
def draw():
game.draw(screen)
game = Game()
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