skip to Main Content

I have a pygame menu where i have drawn some buttons, which represent the level difficulty of my game. For user convenience, i have made a sprite which indicates which level-button is selected(think of it as a light green frame around the button). Now, if i have a solid color as my background, i can just fill the frame with the bg color. But i wanna have a custom image. However i am not sure how to do the deleting stuff with this image. I dont want to have to do a surface.blit(bgImage, surface.get_rect())
in every while-loop. Is there any way to tell pygame to blit just part of the image? So the end-result is still fine-looking. Here is my code when i have a color as the background

(please note that my question does not apply only to this scenario, its more of a general way as to blitting part of an image, without having to rely on cropping the image using 3rd party software like paint(net), photoshop etc.):

#class for the highlight sprite that appears when a level button is clicked         
class HighLightImage(Sprite):

def __init__(self, spriteX, spriteY, width = 180, height =  60):
    Sprite.__init__(self)
    self.rect  = pygame.Rect(spriteX, spriteY, width, height)
    self.image =  pygame.image.load("highlight.png")

#function to draw the highlight sprite, after deleting its older position.
def draw(self, newSpriteX, newSpriteY):
    #due to technical issues the following method is using 4 dirty sprite deletions.
    surface.fill(bgCol, (self.rect.x, self.rect.y, self.rect.width, 10))
    surface.fill(bgCol, (self.rect.x, self.rect.y + self.rect.height-10, self.rect.width, 10))
    surface.fill(bgCol, (self.rect.x, self.rect.y, 10, self.rect.height))
    surface.fill(bgCol, ( self.rect.x + self.rect.width-10, self.rect.y, 10, self.rect.height))
    self.rect.x = newSpriteX
    self.rect.y = newSpriteY
    surface.blit(self.image, self.rect)

And here is the main while-loop

def mainIntro():    

#snake image
snakeImg = pygame.image.load("snakeB.png")
snakeImg = pygame.transform.scale(snakeImg, (150,200))


#highlight obj
hlObj = HighLightImage(0, 0)
#starting level = 1
levels = 1
#initial fill
surface.fill(bgCol)

intro = True

#start button
startButton = StartButton(WIDTH/2-330, HEIGHT - 150)
startButton.draw("Start")
#Exit button
exitButton = ExitButton(WIDTH/2+110, HEIGHT - 150)
exitButton.draw("Exit")
#level buttons
easyLvl = EasyLevelButton( 65, HEIGHT/2 )
easyLvl.draw("Easy")
midLvl = MediumLevelButton( 320, HEIGHT/2 )
midLvl.draw("Medium")
hardLvl = HardLevelButton( 570, HEIGHT/2 )
hardLvl.draw("Hard")    

instructions()


surface.blit(snakeImg, (WIDTH/2-75, HEIGHT - 250))


while intro:
    for ev in pygame.event.get():
        # X exit event
        if ev.type == QUIT:
            pygame.quit()
            sys.exit()
        if ev.type == MOUSEMOTION:
            startButton.hover()
            exitButton.hover()
            easyLvl.hover()
            midLvl.hover()
            hardLvl.hover()
        if ev.type == MOUSEBUTTONDOWN:
            if easyLvl.clicked():
                levels = 1
            if midLvl.clicked():
                levels = 2
            if hardLvl.clicked():
                levels = 4              
            #button exit event
            elif exitButton.clicked():
                pygame.quit()
                sys.exit()
            elif startButton.clicked():
                intro = False

        #highlight frame, according to level-button chosen
        if levels == 1:
            hlObj.draw(easyLvl.x-10, easyLvl.y-10)
        elif levels == 2:
            hlObj.draw(midLvl.x-10, midLvl.y-10)
        elif levels == 4:
            hlObj.draw(hardLvl.x-10, hardLvl.y-10)



    update()

return levels

Finally here is an image of the end result : end result of hl button

P.s In the above code snippets i have not included the button classes, and the global variables like colors, width, height etc., since i dont think they are relevant with what i want to accomplice. Feel free to correct my code, and/or suggest improvements.

2

Answers


  1. As @cmd said above, the area param would be a good option, but for more information, try the pygame docs or have a look at this question or try pygame.transform.chop()

    Login or Signup to reply.
  2. Try the code below:

    pygame.init()
    
    size = width, height = 1200, 800
    screen = pygame.display.set_mode(size)
    image = pygame.image.load("example.png")
    rect = image.get_rect()
    cropx,cropy = 100,10 #Change value to crop different rect areas
    cropRect = (cropx, cropy, rect.w,rect.h)
    
    while(True):
        for event in pygame.event.get():
            if(event.type == pygame.QUIT):
                pygame.quit()
                sys.exit()
        screen.blit(image,cropRect,cropRect)
        pygame.display.update()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search