def __init__(self,x,y,w,h): self.screen_vars = ScreenVars() self.image = pygame.image.load("skeleton.jpeg") self.width = w self.height = h self.pos_x = x self.pos_y = y self.delay = random.randint(10,100) self.rect = (self.pos_x,self.pos_y,self.width,self.height) self.image = pygame.transform.scale(self.image, (self.width,self.height))
class Skeleton(object): def __init__(self,x,y,w,h): self.screen_vars = ScreenVars() self.image = pygame.image.load("skeleton.jpeg") self.width = w self.height = h self.pos_x = x self.pos_y = y self.delay = random.randint(10,100) self.rect = (self.pos_x,self.pos_y,self.width,self.height) self.image = pygame.transform.scale(self.image, (self.width,self.height)) def get_image(self): return self.image def get_rect(self): if self.delay > 0: return (-100,-100,self.width, self.height) return self.rect def move(self, speed): if self.delay>0: return self.pos_y += speed if self.pos_y > self.screen_vars.get_height(): self.delay = random.randint(10,100) self.pos_y = 0 self.rect = (self.pos_x,self.pos_y,self.width,self.height) def get_x(self): return self.pos_x def get_y(self): return self.pos_y def get_width(self): return self.width def get_height(self): return self.height def update(self): if self.delay > 0: self.delay -= 1 def kill(self): self.delay = random.randint(10,100) self.pos_y = 0
def __init__(self): self.section_1_y = 325 self.section_2_y = 400 self.section_3_y = 500 self.screen_vars = ScreenVars() self.dark_yellow = (153,153,0) self.yellow = (255,255,0) self.dark_green = (0,153,0) self.green = (0,255,0) self.section_1 = False self.section_2 = False self.section_3 = False self.key_array = [False,False,False,False] self.points = 0 self.started_game = False self.high_score = 0 self.timer = 0 self.last_time = 0
from game import Game from witch import Witch from zombie import Zombie from monster import Monster from skeleton import Skeleton import pprint pygame.init() FRAME_TIME = 50 #50ms is 20 frames per second if __name__ == '__main__': print("Starting Pumpkin Hero...") screen_vars = ScreenVars() screen = pygame.display.set_mode((screen_vars.get_width(), screen_vars.get_height())) black = (0,0,0) witch = Witch(50,0,75,75) zombie = Zombie(250,0,75,75) monster = Monster(450,0,75,75) skeleton = Skeleton(650,0,75,75) done = False last_time = time.time()*1000.0 SPEED = 5 game = Game() key_monitor = KeyMonitor()
class Game(object): def __init__(self): self.section_1_y = 325 self.section_2_y = 400 self.section_3_y = 500 self.screen_vars = ScreenVars() self.dark_yellow = (153,153,0) self.yellow = (255,255,0) self.dark_green = (0,153,0) self.green = (0,255,0) self.section_1 = False self.section_2 = False self.section_3 = False self.key_array = [False,False,False,False] self.points = 0 self.started_game = False self.high_score = 0 self.timer = 0 self.last_time = 0 def draw_background(self, screen): if self.section_1: pygame.draw.rect(screen, self.yellow, (0,self.section_1_y,self.screen_vars.get_width(),75), 0) else: pygame.draw.rect(screen, self.dark_yellow, (0,self.section_1_y,self.screen_vars.get_width(),75), 0) if self.section_2: pygame.draw.rect(screen, self.green, (0,self.section_2_y,self.screen_vars.get_width(),100), 0) else: pygame.draw.rect(screen, self.dark_green, (0,self.section_2_y,self.screen_vars.get_width(),100), 0) if self.section_3: pygame.draw.rect(screen, self.yellow, (0,self.section_3_y,self.screen_vars.get_width(),75), 0) else: pygame.draw.rect(screen, self.dark_yellow, (0,self.section_3_y,self.screen_vars.get_width(),75), 0) #Print remaining time myfont = pygame.font.SysFont("monospace", 45) text = myfont.render("Time: " + str(self.timer), 1, (255,255,0)) screen.blit(text, (5,5)) score = myfont.render("Score: " + str(self.points), 1, (255,255,0)) screen.blit(score, (245, 5)) def check_piece(self, pieceary, keymonitor): self.section_1 = False self.section_2 = False self.section_3 = False key_just_pressed = [False,False,False,False] if keymonitor.get_key(0)==True and self.key_array[0]==False: self.key_array[0] = True key_just_pressed[0] = True elif not keymonitor.get_key(0)==False: self.key_array[0] = False if keymonitor.get_key(1)==True and self.key_array[1]==False: self.key_array[1] = True key_just_pressed[1] = True elif not keymonitor.get_key(1)==False: self.key_array[1] = False if keymonitor.get_key(2)==True and self.key_array[2]==False: self.key_array[2] = True key_just_pressed[2] = True elif not keymonitor.get_key(2)==False: self.key_array[2] = False if keymonitor.get_key(3)==True and self.key_array[3]==False: self.key_array[3] = True key_just_pressed[3] = True elif not keymonitor.get_key(3)==False: self.key_array[3] = False for index,piece in enumerate(pieceary): y_pos = piece.get_y() + piece.get_height() piece_killed = False if y_pos > self.section_1_y and y_pos<self.section_1_y+75: self.section_1 = True if key_just_pressed[index]: piece.kill() self.points += 1 piece_killed = True if y_pos > self.section_2_y and y_pos<self.section_2_y+100: self.section_2 = True if key_just_pressed[index]: piece.kill() self.points += 2 piece_killed = True if y_pos > self.section_3_y and y_pos<self.section_3_y+75: self.section_3 = True if key_just_pressed[index]: piece.kill() self.points += 1 piece_killed = True if not piece_killed and key_just_pressed[index]: piece.kill() def draw_score(self, screen): myfont = pygame.font.SysFont("monospace", 45) # render text label = myfont.render("Pumpkin Hero", 1, (255,255,0)) screen.blit(label, (100, 100)) myfont2 = pygame.font.SysFont("monospace", 35) score = myfont2.render("Previous Score: " + str(self.points), 1, (255,255,0)) screen.blit(score, (150, 300)) hiscore = myfont2.render("High Score: " + str(self.high_score), 1, (255,255,0)) screen.blit(hiscore, (150, 250)) start = myfont2.render("Press Space to start", 1, (255,255,0)) screen.blit(start, (200, 400)) def tick(self): if self.started_game and time.time()-self.last_time > 1: self.timer -= 1 self.last_time = time.time() if self.timer < 1 and self.started_game: # Store high score for this session if self.points > self.high_score: self.high_score = self.points self.timer = 0 self.started_game = False def started(self): return self.started_game def start(self): if self.started_game == False: print("Game starting") self.started_game = True self.last_time = time.time() self.points = 0 self.timer = 30 else: print("Game has already started - can't restart")