0

I am attempting to use Object Orientated Programming to create a take on a classic quiz show. I have managed to use an image object to "collide" with the mouse arrow and generate a board of hexagons each with their own random letter. In the vain hope of creating a similar collide point with the rectangle of the rendered letter I am able to retrieve rect information of the rendered font but am unable to create a collision point with that tendered font rectangle that would allow me to retrieve attributes of that hexagon when clicked. Partial success so far (having failed to detect the hexagon polygon itself) but the question for now is, is it possible for a mouse cursor to detect a collision point of the the surface rectangle that the font has been rendered onto...?

# Hex_Demo
# Code that creates instance of a hex
import pygame
import math
import time
import random
from hex import Hex
from buttons import Button


DISPLAY_WIDTH = 1000
DISPLAY_HEIGHT = 1000

WHITE   =   (255,255,255)
BLACK   =   (0,0,0)
RED     =   (255,0,0)
LIME    =   (0,255,0)
BLUE    =   (0,0,255)
YELLOW  =   (255,255,0)
CYAN    =   (0,255,255)
MAGENTA =   (255,0,255)
SILVER  =   (192,192,192)
GRAY    =   (128,128,128)
MAROON  =   (128,0,0)
OLIVE   =   (128,128,0)
GREEN   =   (0,128,0)
PURPLE  =   (128,0,128)
TEAL    =   (0,128,128)
NAVY    =   (0,0,128)




TITLE = "Hexbusters"
window = pygame.display.set_mode((DISPLAY_HEIGHT,DISPLAY_HEIGHT))
window.fill(WHITE)
pygame.display.update()
pygame.display.set_caption(TITLE)





Side_Length = 75
x = 200
y = 200
Seg_Height = math.sqrt((Side_Length**2)- ((0.5*Side_Length)**2))
Colour = GRAY
text = " "



alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

def letter():
    letter = alphabet[random.randint(0,25)]
    return letter


#####load button image
start_button_img = pygame.image.load('Start_Button.png').convert_alpha()



######Create Button Instance
start_button = Button(850,10,start_button_img,0.15)



def draw(x,y,Side_Length, Seg_Height, Colour):
    window.fill(WHITE)
    
    #########################1st Column##################################
    ###instances of the hexes that need drawing...using the hex module
    hex1 = Hex(x, y+(Seg_Height*0), Side_Length,Colour, text = letter())
    hex1.draw(window)
    hex1.get_Hex_letter()#retrieves letter from hex object
    hex1.get_Hex_letter_rectangle()#retrieves rectangle coordinates from letter render


    hex2 = Hex(x,y+(Seg_Height*2),Side_Length,Colour, text = letter())
    hex2.draw(window)
    hex2.get_Hex_letter()
    hex2.get_Hex_letter_rectangle()

    hex3 = Hex(x,y+(Seg_Height*4),Side_Length,Colour, text = letter())
    hex3.draw(window)

    hex4 = Hex(x,y+(Seg_Height*6),Side_Length,Colour, text = letter())
    hex4.draw(window)

    ###########################2nd Column#################################

    hex5 = Hex(x+(Side_Length*1.5) , y+Seg_Height, Side_Length,Colour, text = letter() )
    hex5.draw(window)

    hex6 = Hex(x+(Side_Length*1.5) , y+(Seg_Height*3), Side_Length,Colour, text = letter() )
    hex6.draw(window)

    hex7 = Hex(x+(Side_Length*1.5) , y+(Seg_Height*5), Side_Length,Colour, text = letter())
    hex7.draw(window)

    hex8 = Hex(x+(Side_Length*1.5) , y+(Seg_Height*7), Side_Length,Colour, text = letter() )
    hex8.draw(window)

    ############################3rd Column################################

    hex9 = Hex(x+(Side_Length*3) , y, Side_Length,Colour, text = letter() )
    hex9.draw(window)

    hex10 = Hex(x+(Side_Length*3) , y+(Seg_Height*2), Side_Length,Colour, text = letter())
    hex10.draw(window)

    hex11 = Hex(x+(Side_Length*3) , y+(Seg_Height*4), Side_Length,Colour, text = letter())
    hex11.draw(window)

    hex12 = Hex(x+(Side_Length*3) , y+(Seg_Height*6), Side_Length,Colour, text = letter())
    hex12.draw(window)

    #############################4th Column###############################

    hex13 = Hex(x+(Side_Length*4.5) , y+Seg_Height, Side_Length,Colour, text = letter())
    hex13.draw(window)

    hex14 = Hex(x+(Side_Length*4.5) , y+(Seg_Height*3), Side_Length,Colour, text = letter())
    hex14.draw(window)

    hex15 = Hex(x+(Side_Length*4.5) , y+(Seg_Height*5), Side_Length,Colour, text = letter())
    hex15.draw(window)

    hex16 = Hex(x+(Side_Length*4.5) , y+(Seg_Height*7), Side_Length, Colour, text = letter())
    hex16.draw(window)

    ##############################5th Column##############################

    hex17 = Hex(x+(Side_Length*6) , y, Side_Length,Colour, text = letter())
    hex17.draw(window)

    hex18 = Hex(x+(Side_Length*6) , y+(Seg_Height*2), Side_Length,Colour, text = letter())
    hex18.draw(window)

    hex19 = Hex(x+(Side_Length*6) , y+(Seg_Height*4), Side_Length,Colour, text = letter())
    hex19.draw(window)

    hex20 = Hex(x+(Side_Length*6) , y+(Seg_Height*6), Side_Length,Colour, text = letter())
    hex20.draw(window)

    #################################################################
    #########################Left Border Hexes#######################
    hex21 =  Hex(x-(Side_Length*1.5) , y-Seg_Height, Side_Length,Colour=RED, text = "" )
    hex21.draw(window)

    hex22 =  Hex(x-(Side_Length*1.5) , y+Seg_Height, Side_Length,Colour=RED, text = "" )
    hex22.draw(window)

    hex23 =  Hex(x-(Side_Length*1.5) , y+Seg_Height*3, Side_Length,Colour=RED, text = "" )
    hex23.draw(window)

    hex24 =  Hex(x-(Side_Length*1.5) , y+Seg_Height*5, Side_Length,Colour=RED, text = "" )
    hex24.draw(window)

    hex25 =  Hex(x-(Side_Length*1.5) , y+Seg_Height*7, Side_Length,Colour=RED, text = "" )
    hex25.draw(window)


    ##################################################################
    #########################Right Border Hexes#######################
    hex26 =  Hex(x+(Side_Length*7.5) , y-Seg_Height, Side_Length,Colour=RED, text = "" )
    hex26.draw(window)

    hex27 =  Hex(x+(Side_Length*7.5) , y+Seg_Height, Side_Length,Colour=RED, text = "" )
    hex27.draw(window)

    hex28 =  Hex(x+(Side_Length*7.5) , y+Seg_Height*3, Side_Length,Colour=RED, text = "" )
    hex28.draw(window)

    hex29 =  Hex(x+(Side_Length*7.5) , y+Seg_Height*5, Side_Length,Colour=RED, text = "" )
    hex29.draw(window)

    hex30 =  Hex(x+(Side_Length*7.5) , y+Seg_Height*7, Side_Length,Colour=RED, text = "" )
    hex30.draw(window)

    hex31 =  Hex(x+(Side_Length*7.5) , y+Seg_Height, Side_Length,Colour=RED, text = "" )
    hex31.draw(window)

    ##################################################################
    #########################Top Border Hexes#######################
    hex32 =  Hex(x , y-(Seg_Height*2), Side_Length,Colour=WHITE, text = "" )
    hex32.draw(window)

    hex33 =  Hex(x+(Side_Length*1.5) , y-Seg_Height, Side_Length,Colour=WHITE, text = "" )
    hex33.draw(window)

    hex34 =  Hex(x+(Side_Length*3) , y-Seg_Height*2, Side_Length,Colour=WHITE, text = "" )
    hex34.draw(window)

    hex35 =  Hex(x+(Side_Length*4.5) , y-Seg_Height, Side_Length,Colour=WHITE, text = "" )
    hex35.draw(window)

    hex36 =  Hex(x+(Side_Length*6) , y-Seg_Height*2, Side_Length,Colour=WHITE, text = "" )
    hex36.draw(window)

    hex37 =  Hex(x+(Side_Length*7.5) , y+Seg_Height, Side_Length,Colour=RED, text = "" )
    hex37.draw(window)


    #################################################################
    #########################Bottom Border Hexes#####################
    hex38 =  Hex(x , y+(Seg_Height*8), Side_Length,Colour=WHITE, text = "" )
    hex38.draw(window)

    hex39 =  Hex(x+(Side_Length*1.5) , y+Seg_Height*9, Side_Length,Colour=WHITE, text = "" )
    hex39.draw(window)

    hex40 =  Hex(x+(Side_Length*3) , y+Seg_Height*8, Side_Length,Colour=WHITE, text = "" )
    hex40.draw(window)

    hex41 =  Hex(x+(Side_Length*4.5) , y+Seg_Height*9, Side_Length,Colour=WHITE, text = " " )
    hex41.draw(window)

    hex42 =  Hex(x+(Side_Length*6) , y+Seg_Height*8, Side_Length,Colour=WHITE, text = "" )
    hex42.draw(window)

    #################################################################




    ##################################################################
run = False

while not run:




    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()#retrieves mouse x,y coordinates
        print(pos)
        
        if event.type==pygame.QUIT:
            run = True

        if start_button.draw(window) == True:
            print ("START")
            
            draw(x,y,Side_Length,Seg_Height,Colour)
        







    pygame.display.update()
pygame.quit()


########################Hex Module Code##########################
# Hexbuster Hex Class
import math
import pygame


class Hex():
    shape = "Hex"

    def __init__(self, x,y, Side_Length,Colour, text):
            self.text = text
            self.font = pygame.font.Font("arial_copy.ttf",20)
            self.letter = self.font.render(self.text, True, (0,0,0))
            self.Side_Length    = Side_Length
            self.Centre_pt_x    = x
            self.Centre_pt_y    = y
            self.Seg_Height     = math.sqrt((self.Side_Length**2)- ((0.5*self.Side_Length)**2))
            self.color          = Colour
            self.Vertices_1     = (self.Centre_pt_x - (0.5*self.Side_Length), self.Centre_pt_y-self.Seg_Height)
            self.Vertices_2     = (self.Centre_pt_x + (0.5*self.Side_Length), self.Centre_pt_y-self.Seg_Height)
            self.Vertices_3     = (self.Centre_pt_x +  self.Side_Length     , self.Centre_pt_y)
            self.Vertices_4     = (self.Centre_pt_x + (0.5*self.Side_Length), self.Centre_pt_y+self.Seg_Height)
            self.Vertices_5     = (self.Centre_pt_x - (0.5*self.Side_Length), self.Centre_pt_y+self.Seg_Height)
            self.Vertices_6     = (self.Centre_pt_x -  self.Side_Length     , self.Centre_pt_y)
            self.clicked        = False
            #self.letterbox_width = self.letter.get_width()
            #self.letterbox_height = self.letter.get_height()
            self.letterbox_rect = self.letter.get_rect()
            #print("letterbox rectangle = " ,self.letterbox_rect)


            self.WHITE   =   (255,255,255)
            self.BLACK   =   (0,0,0)
            self.RED     =   (255,0,0)
            self.LIME    =   (0,255,0)
            self.BLUE    =   (0,0,255)
            self.YELLOW  =   (255,255,0)
            self.CYAN    =   (0,255,255)
            self.MAGENTA =   (255,0,255)
            self.SILVER  =   (192,192,192)
            self.GRAY    =   (128,128,128)
            self.MAROON  =   (128,0,0)
            self.OLIVE   =   (128,128,0)
            self.GREEN   =   (0,128,0)
            self.PURPLE  =   (128,0,128)
            self.TEAL    =   (0,128,128)
            self.NAVY    =   (0,0,128)





    def draw(self, window):
        pygame.draw.polygon(window, self.color, [self.Vertices_1,self.Vertices_2,self.Vertices_3, self.Vertices_4, self.Vertices_5, self.Vertices_6])
        pygame.draw.polygon(window, self.BLACK, [self.Vertices_1,self.Vertices_2,self.Vertices_3, self.Vertices_4, self.Vertices_5, self.Vertices_6],1)
        window.blit(self.letter,(self.Centre_pt_x-5,self.Centre_pt_y-10))
        
        #getmouse position
        hex_mouse_pos = pygame.mouse.get_pos()
        print("Hex_mouse Pos = ", hex_mouse_pos)
        if self.letterbox_rect.collidepoint(hex_mouse_pos):
            print("Hex found")
        
        


    def get_Hex_letter(self):
        print("Hex Letter Getter activated...", self.text)
        return self.text
        
    def get_Hex_letter_rectangle(self):
        print("Rendering rectangle behind letter",self.text, " is..", self.letterbox_rect)
        return self.letterbox_rect
    
    
##########################Button Module Code############
# Button class
import pygame


class Button():
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()

        self.image = pygame.transform.scale(image, (int(width*scale), int(height*scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x,y)
        self.clicked = False

    def draw(self,window):
        action = False
        pos = pygame.mouse.get_pos() #gets mouse position
        if self.rect.collidepoint(pos):
            print("HOVER")
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True

            if pygame.mouse.get_pressed()[0] == 0:
                self.clicked = False


        window.blit(self.image, (self.rect.x, self.rect.y))

        return action


   
Dorsai
  • 1
  • 1
  • You must set the position of `self.letterbox_rect` before `self.letterbox_rect.collidepoint(hex_mouse_pos)`. Something like `self.letterbox_rect.topleft = (self.Centre_pt_x-5,self.Centre_pt_y-10)` – Rabbid76 Aug 30 '21 at 11:35
  • Thanks for the idea.... self.letter.get_rect() returns a value something like this (0, 0, 4, 23) which i believe are the dimensions of the rectangle surface that the font letter "sits" on. What I am going to need to work out is how to find the coordinates of WHERE that font surface is on the game window and from there, the collide position function will begin to work. One step forward , three steps back. Thanks for the advice. – Dorsai Aug 31 '21 at 11:20
  • Read the duplicate: [Why is my collision test always returning 'true' and why is the position of the rectangle of the image always wrong (0, 0)?](https://stackoverflow.com/questions/57730329/pygame-collide-rect-function-always-returning-true/57730378#57730378) – Rabbid76 Aug 31 '21 at 11:23
  • The _Surface_ is where you `blit` it: `window.blit(self.letter,(self.Centre_pt_x-5,self.Centre_pt_y-10))`. Therefore `topleft` is `(self.Centre_pt_x-5,self.Centre_pt_y-10)` – Rabbid76 Aug 31 '21 at 11:24
  • Had a bit of a re-hash and a think plus a few Youtube Channels and have now resolved the ability to retrieve the objects letter created by the font render. So the code (see below) works somewhat better now. https://github.com/DorsaiFeydakin/Hex_busters.git – Dorsai Aug 31 '21 at 14:15

0 Answers0