Sooooo... I've created a very basic menu program. At present, it is merely 3 rectangles, with choice detection.
So far, it's as basic as can be - but it took me long enough to work out that I thought I would share it. :lol: Hopefully tomorrow I'll make some further progress with it, and actually have it operating as a full functioning menu with text and images and everything.
The code is very basic and untidy at present. Tomorrow I plan to re-write the code as OOP and expand the functionality of it. The code below isn't finished, I haven't added in any way of detecting an ENTER key event yet, but that's easy enough. Once it's added, pressing enter will use the choice variable to call the appropriate function (New, Load, Settings etc).
The overall idea is to write a decent looking menu program, which can then be implemented in larger game projects I intend on making.
Tomorrow:
So far, it's as basic as can be - but it took me long enough to work out that I thought I would share it. :lol: Hopefully tomorrow I'll make some further progress with it, and actually have it operating as a full functioning menu with text and images and everything.
The code is very basic and untidy at present. Tomorrow I plan to re-write the code as OOP and expand the functionality of it. The code below isn't finished, I haven't added in any way of detecting an ENTER key event yet, but that's easy enough. Once it's added, pressing enter will use the choice variable to call the appropriate function (New, Load, Settings etc).
The overall idea is to write a decent looking menu program, which can then be implemented in larger game projects I intend on making.
Code:
# Menu Program Rectangles.
import pygame
import sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x = 200
pos_y = 175
black = Color('black')
white = Color('white')
blue = Color('blue')
choice = 0
def print_text(font, x, y, text, color=(255,255,255)):
imgText = font.render(text, True, color)
screen = pygame.display.get_surface() #req'd when function moved into MyLibrary
screen.blit(imgText, (x,y))
font = pygame.font.Font(None, 30)
while True:
for event in pygame.event.get():
if event.type == QUIT: sys.exit()
elif event.type == KEYDOWN:
if event.key == pygame.K_DOWN:
choice += 1
if choice > 2: choice = 2
elif event.key == pygame.K_UP:
choice -= 1
if choice < 0: choice = 0
load = "LOAD"
new = "NEW"
screen.fill((black))
color0 = white
color1 = white
color2 = white
selected = ""
keys = pygame.key.get_pressed()
pos1 = pos_x, 150, 200, 50
pos2 = pos_x, 210, 200, 50
pos3 = pos_x, 270, 200, 50
width = 4
if choice == 0:
color0 = blue
elif choice == 1:
color1 = blue
elif choice == 2:
color2 = blue
pygame.draw.rect(screen,color0, pos1, width)
pygame.draw.rect(screen,color1, pos2, width)
pygame.draw.rect(screen,color2, pos3, width)
pygame.display.update()
Tomorrow:
- Re-write program as a class.
- Add "Enter" functionality for selection.
- Add text and images.
- Try implementing it in a game.