i have finished main code game , have started on making menu screen. can display buttons on screen fine when click somewhere error:
how can go fixing this? if didn't make clear in question please tell me can clarify. thanks!
here code menuscreen:
import pygame import random import time pygame.init() #colours white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,155,0) blue = (50,50,155) display_width = 800 display_height = 600 gamedisplay = pygame.display.set_mode((display_width,display_height)) pygame.display.set_caption('numeracy ninjas') clock = pygame.time.clock() #fonts smallfont = pygame.font.sysfont("comicsansms", 25) medfont = pygame.font.sysfont("comicsansms", 50) largefont = pygame.font.sysfont("comicsansms", 75) #sprites img_button_start = pygame.image.load('sprites/buttons/button_start.png') img_button_options = pygame.image.load('sprites/buttons/button_options.png') gamedisplay.fill(white) pygame.display.update() class button(pygame.sprite.sprite): def __init__(self, image, buttonx, buttony): super().__init__() gamedisplay.blit(image, (buttonx, buttony)) pygame.display.update() selfrect = image.get_rect() def wasclicked(event): if selfrect.collidepoint(event.pos): return true def gameintro(): buttons = pygame.sprite.group() button_start = button(img_button_start, 27, 0) button_options = button(img_button_options, 27, 500) buttons.add(button_start) buttons.add(button_options) print(buttons) #main game loop running = true while running: event in pygame.event.get(): if event.type == pygame.mousebuttondown: print(event.pos) #check every button whether clicked btn in buttons: print('forbtninbuttons') if btn.wasclicked(): print('clicked!') if event.type == pygame.quit: pygame.quit()
you haven't declared attributes class, local variables. try doing self.selfrect = image.get_rect()
in initializer , in wasclicked(event)
method do:
def wasclicked(self, event): if self.selfrect.collidepoint(event.pos): return true
it's convention name rect variable rect though.
class button(pygame.sprite.sprite): def __init__(self, image, buttonx, buttony): super().__init__() # code doesn't make sense here. should inside game loop. # gamedisplay.blit(image, (buttonx, buttony)) # pygame.display.update() self.image = image # it's have reference image. self.rect = image.get_rect() def wasclicked(self, event): if self.rect.collidepoint(event.pos): return true else: return false
Comments
Post a Comment