I am making a game in pygame, and so far, I am just on the screen where you pick your characters.
My problem is that I have a list of pygame.Rects, (Technically, it is my own class derived from pygame.Rect, but the only changes I have are methods to have it be shaded or underlined.) and when the mouse is clicked on the box, I want it to be underlined, but my main point right now is getting it so that it can actually respond when it has been clicked on.
A runnable example:
import sys
from pygame.locals import *
import pygame as pg
WIN_X, WIN_Y = 800, 600
def Box(size, colour, pos, alpha=None, image=None):
'''
return a square rectangle, surface pair
uses MyRect
'''
print(pos)
new_surf = pg.surface.Surface(size)
new_surf.fill(colour)
new_surf.set_alpha(int(alpha))
SURFACE.blit(new_surf, pos)
if image is not None:
SURFACE.blit(image, pos)
return new_surf.get_rect(), new_surf
def main():
global SURFACE
pg.init()
SURFACE = pg.display.set_mode((WIN_X, WIN_Y))
test()
def test():
surf=pg.display.set_mode((1000,1000))
box_list = []
for i in range(WIN_X // 4, WIN_X // 4 * 3, 100):
box_list.append(
Box((25, 25), (211,211,211), (i, WIN_Y // 2), 150)[0])
while True:
for event in pg.event.get():
if event.type == MOUSEBUTTONDOWN:
x,y = event.pos
for rect in box_list:
if rect.collidepoint(x, y):
print('box clicked!')
elif event.type==QUIT:
pg.quit()
sys.exit()
pg.display.update()
if __name__ == '__main__':
main()
I have been trying to get around this for HOURS and nothing has worked. (By the way, I am relatively new to pygame, so if you see any possible other problems, please point them out).