import sys, random, pygame
from pygame.locals import *
from MyLibrary import print_text, Point
class Main_Menu(object):
""" A Main Menu object. """
def __init__(self, background, screen, font):
"""Initialises Main Menu """
self.choice = 0
self.background = background
self.screen = screen
self.font = font
self.items = ['New', 'Load', 'Settings', 'Quit']
self.black = Color('black')
self.white = Color('white')
self.colors = [self.black, self.black, self.black, self.black]
def items(self, items):
self.items = items
def check_events(self):
"""Checks for new keyboard events. """
for event in pygame.event.get():
if event.type == QUIT: sys.exit()
elif event.type == KEYDOWN:
if event.key == pygame.K_DOWN:
self.choice += 1
if self.choice >= 3: self.choice = 3
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_UP:
self.choice -= 1
if self.choice <= 0: self.choice = 0
self.update_colors()
def update_colors(self):
self.colors = [self.black, self.black, self.black, self.black]
self.colors[self.choice] = self.white
def display(self):
self.color = self.colors[0]
self.screen.blit(self.background, (0,0))
self.pos = Point(50, 150)
text = self.items[0]
print_text(self.font, self.pos.x, self.pos.y, text, self.color)
print_text(self.font, 300, self.pos.y, str(self.choice + 1), self.black)
for n in range(1,4):
color = self.colors[n]
text = self.items[n]
self.pos.y += 50
print_text(self.font, self.pos.x, self.pos.y, text, color)
def main_menu():
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Menu Test")
font = pygame.font.Font(None, 50)
framerate = pygame.time.Clock()
bg = pygame.image.load("background.png").convert_alpha()
menu = Main_Menu(bg,screen,font)
while True:
framerate.tick(30)
ticks = pygame.time.get_ticks()
"""for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]: sys.exit()"""
menu.display()
menu.check_events()
pygame.display.update()
main_menu()