class Sky: def __init__(self): self.mMoon = Moon() return def draw(self, surface): color = (0, 0, 0) rectangle = pygame.Rect(0, 0, 600, 500) # x and y pygame.draw.rect(surface, color, rectangle, 0) # 0 = width of border self.mMoon.draw(surface) return
class Game: def __init__(self): pygame.init() self.screen = pygame.display.set_mode([WIDTH, HEIGHT]) pygame.display.set_caption(TITLE) self.clock = pygame.time.Clock() self.start() def start(self): self.moon = Moon(self, WIDTH, HEIGHT * 0.75, 10) self.moon.generate_terrain() self.player = Player(self, WIDTH // 2, 0) def run(self): self.playing = True while self.playing: self.dt = self.clock.tick(FPS) self.events() self.update() self.draw() def events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.playing = False def update(self): self.player.update(self.moon) def draw(self): self.screen.fill(DARKGREY) self.moon.draw() self.player.draw() pygame.draw.rect(self.screen, DARKRED, pygame.Rect(5, 5, 200, 25)) pygame.draw.rect(self.screen, YELLOW, pygame.Rect(5, 5, 200 * self.player.fuel, 25)) pygame.display.flip()
def menu(): title_font = pygame.font.Font(os.path.join('assets','atari.ttf'),40) top_gap = 50 title_text = title_font.render("SPACE INVADERS",True,WHITE) def load_moon_images(): directory = os.path.join('assets','hjm-moon') moon_images = [] for file_ in os.listdir(directory): moon_image = pygame.image.load(os.path.join(directory,file_)).convert_alpha() moon_images.append(moon_image) return moon_images transparent_red = (255,0,0,128) high_scores_text = title_font.render("HIGH SCORES",True,WHITE) high_scores_text_surface = pygame.Surface((high_scores_text.get_width() + 50,high_scores_text.get_height() + 50),pygame.SRCALPHA) high_scores_text_rect = high_scores_text.get_rect(center=(high_scores_text_surface.get_width()//2,high_scores_text_surface.get_height()//2)) high_scores_text_surface.fill(RED) high_scores_text_surface_rect = high_scores_text_surface.get_rect(center=(WIDTH//2,HEIGHT - top_gap - high_scores_text_surface.get_height()//2)) high_scores_text_surface.blit(high_scores_text,high_scores_text_rect) alpha_high_score_text_surface = pygame.Surface(high_scores_text.get_size(),pygame.SRCALPHA) alpha_high_score_text_surface.fill((255,0,0,128)) title_text_rect= title_text.get_rect(center=(WIDTH//2,top_gap + title_text.get_height()//2)) enter_font = pygame.font.Font(os.path.join('assets','atari.ttf'),30) enter_text = enter_font.render("Hit ENTER To Play!",True,WHITE) enter_text_rect = enter_text.get_rect(center=(WIDTH//2,HEIGHT//2)) topleft = (0,0) pygame.mixer.music.load(os.path.join('assets','intro.ogg')) pygame.mixer.music.play(-1) alpha_surface = pygame.Surface(enter_text.get_size(),pygame.SRCALPHA) alpha = 255 title_font_size = 40 moon = Moon(WIDTH//2,title_text_rect.bottom + 150) moon = pygame.sprite.GroupSingle(moon) buttons = pygame.sprite.Group() button_1 = Button("HOW TO PLAY",WIDTH//2,high_scores_text_surface_rect.top - 100,WHITE,RED,instructions_screen,title_font_size) button_2 = Button("HIGH SCORES",WIDTH//2,HEIGHT - top_gap - 50,WHITE,RED,high_score_screen,title_font_size) buttons.add(button_1) buttons.add(button_2) MOON_CHANGE_EVENT = pygame.USEREVENT + 4 milliseconds = 200 pygame.time.set_timer(MOON_CHANGE_EVENT,milliseconds) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: pygame.mixer.music.stop() game() pygame.mixer.music.load(os.path.join('assets','intro.ogg')) pygame.mixer.music.play(-1) elif event.type == pygame.MOUSEBUTTONDOWN: point = pygame.mouse.get_pos() for button in buttons: if button.clicked_on(point): button.callback() #if high_scores_text_surface_rect.collidepoint(point): #high_score_screen() elif event.type == MOON_CHANGE_EVENT: moon.update() if alpha > 0: alpha = max(alpha - 4,0) enter_text_copy = enter_text.copy() alpha_surface.fill((255,255,255,alpha)) enter_text_copy.blit(alpha_surface,(0,0),special_flags=pygame.BLEND_RGBA_MULT) else: alpha = 255 point = pygame.mouse.get_pos() if high_scores_text_surface_rect.collidepoint(point): high_scores_text_surface.fill(transparent_red) high_scores_text_surface.blit(high_scores_text,high_scores_text_rect) else: high_scores_text_surface.fill(RED) high_scores_text_surface.blit(high_scores_text,high_scores_text_rect) buttons.update(point) screen.blit(background_image,topleft) moon.draw(screen) screen.blit(title_text,title_text_rect) screen.blit(enter_text_copy,enter_text_rect) #screen.blit(high_scores_text_surface,high_scores_text_surface_rect) buttons.draw(screen) pygame.display.update()
def run(self): while self.paused[0] == 1: pass sky = Sky(self.queue) sun = Sun(self.queue, 300, 100) moon = Moon(self.queue, 300, 100) ground = Ground(self.queue) time = 75 self.shared[0] = time day = 0 seasons = ["spring", "summer", "fall", "winter"] season = "spring" self.shared[2] = seasons.index(season) seasonStages = ["early", "mid", "mid", "late"] seasonStage = "mid" weatherOptions = ["clear", "clear", "clear", "clear", "cloudy", "cloudy", "overcast", "rain", "rain", "storm"] weather = choice(weatherOptions) self.shared[1] = weatherOptions.index(weather) self.queue.put(QueueItem("cont")) self.paused[0] = 1 self.paused[3] = 0 while self.paused[0] == 1: pass while True: sky.update(time) sun.update(time) moon.update(time, day) ground.update(time, season, seasonStage, seasons) sun.draw() moon.draw() ground.draw() time += 3 if time > 1600: time = 0 day += 1 if day > 15: day = 0 season = seasons[int(day/4)] self.shared[2] = seasons.index(season) seasonStage = seasonStages[day%len(seasonStages)] if season == "winter" and seasonStage == "early": weather = "rain" else: weather = choice(weatherOptions) self.shared[0] = time self.shared[1] = weatherOptions.index(weather) self.queue.put(QueueItem("cont")) self.paused[0] = 1 sleep(0.05) while self.paused[0] == 1: pass