1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
class Menu():
font_path = 'coders_crux.ttf'
def __init__(self, pos, data=[], textsize=64,\
wordcolor=[0,255,255], backcolor=[51, 51, 51], selectedcolor=[153,102,255], lprn=20):
import pygame
self.rect = pygame.Rect((pos), (0, 950))
self.data = data
self.textrects = []
self.textsurfs = []
self.cursorpos = 0
self.pos = pos
self.font = pygame.font.SysFont('coders_crux.ttf', 32)
self.wordcolor = wordcolor
self.backcolor = backcolor
self.selectedcolor = selectedcolor
self.textsize = textsize
self.looprun = lprn#looprun
self.scroll = False
self.selected = False
for word in self.data:
self.textsurfs.append(self.font.render(str(word), True, self.wordcolor))
rect = self.font.render(str(word), True, self.wordcolor).get_rect()
self.textrects.append(rect)
def add(self, words):
self.data.extend(words)
self.font = pygame.font.Font(self.font_path, 32)
for word in words:
self.textrects.append(self.font.render(str(word), True, self.wordcolor).get_rect())
self.textsurfs.append(self.font.render(str(word), True, self.wordcolor))
def remove(self, item=False):
if not item:
self.data.pop()
self.textrects.pop()
self.textsurfs.pop()
else:
thingpop = self.data.index(item)
self.data.pop(thingpop)
self.textrects.pop(thingpop)
self.textsurfs.pop(thingpop)
def update(self, screen, event):
import pygame
self.font = pygame.font.Font(self.font_path, 64)
height = 0
curpos = []# Stop python from making curpos and self.pos point to the same list
for n in self.pos:
curpos.append(n)
curpos[0] += 1
self.rect.height = len(self.data)*self.textsize# Define the max height of the text box
if self.rect.height > screen.get_height():
self.scroll = True
else:
self.scroll = False
# Define the max width of the text box based on the word length it holds
maxsize = sorted(self.data, reverse=True)
maxsize = len(maxsize[0])
self.rect.width = (maxsize)*(self.textsize)
# Update rects and surfaces
for surf in self.textsurfs:
if not self.textsurfs.index(surf) == self.cursorpos:
screen.blit(surf, curpos)
self.textrects[self.textsurfs.index(surf)].topleft = curpos
else:
rect = self.textrects[self.textsurfs.index(surf)]
rect.topleft = curpos
surf = self.font.render(str(self.data[self.textsurfs.index(surf)]), True, self.selectedcolor)
screen.blit(surf, curpos)
curpos[1] += self.textsize
e = event
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_UP:
if self.cursorpos > 0:
self.cursorpos -= 1
elif e.key == pygame.K_DOWN:
if self.cursorpos < len(self.data)-1:
self.cursorpos += 1
elif e.key == pygame.K_SPACE:
self.selected = self.data[self.cursorpos]
elif e.type == pygame.MOUSEMOTION:
mousepos = pygame.mouse.get_pos()
for rect in self.textrects:
if rect.collidepoint(mousepos):
self.cursorpos = self.textrects.index(rect)
elif e.type == pygame.MOUSEBUTTONDOWN:
self.selected = self.data[self.cursorpos]
elif e.type == pygame.QUIT:
self.selected = pygame.QUIT
return |
Partager