1

How can I stick an image to a rectangle? I think it has something to do with the blit function. I want to blit the image onto the rectangle on line 50.

import pygame
import sys

pygame.init()

FPS = 60
start_smallfont = pygame.font.SysFont('Corbel', 45)
start_text = start_smallfont.render('Start', True, (255, 255, 255))
rect_smallfont = pygame.font.SysFont('Corbel', 33)
rect_text = rect_smallfont.render('You', True, (255, 255, 255))
x = 375
y = 335
vel = 0.1
startWIDTH, startHEIGHT = 170, 80
screenWIDTH, screenHEIGHT = 800, 720

WIN = pygame.display.set_mode((screenWIDTH, screenHEIGHT))
pygame.display.set_caption(":D")

def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if 800/2-85 <= mouse[0] <= 800/2-85+startWIDTH and 720/2-40 <= mouse[1] <= 720/2-40+startHEIGHT:
                    clock.tick(FPS)
                    run = False
                    while True:
                        global x, y
                        for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                pygame.quit()

                        WIN.fill((0, 0, 0))
                        keys = pygame.key.get_pressed()
                        if keys[pygame.K_LEFT] and x > vel:
                            x -= vel
                        if keys[pygame.K_RIGHT] and x < screenWIDTH - 50 - vel:
                            x += vel
                        if keys[pygame.K_UP] and y > vel:
                            y -= vel
                        if keys[pygame.K_DOWN] and y < screenHEIGHT - 50 - vel:
                            y += vel
                        
                        pygame.draw.rect(WIN, (255, 0, 0), (x, y, 50, 50))

                        
                        
                        pygame.display.update()

                        

        mouse = pygame.mouse.get_pos()

        if 800/2-85 <= mouse[0] <= 800/2-85+startWIDTH and 720/2-40 <= mouse[1] <= 720/2-40+startHEIGHT:
            pygame.draw.rect(WIN, (255, 91, 91), (800/2-85, 720/2-40, startWIDTH, startHEIGHT))
        else:
            pygame.draw.rect(WIN, (255, 0, 0), (800/2-85, 720/2-40, startWIDTH, startHEIGHT))

        WIN.blit(start_text, (800/2-85+42,720/2-40+20))
        pygame.display.update()





if __name__ == "__main__":
    main()
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Gratah2
  • 11
  • 1
  • Saying on "line x" is fairly useless with respect to code in questions here, since there are no line numbers being displayed. It's better to repeat the line in the body of the question and/or indicate it with a comment like `# <---- HERE` in the code. – martineau Jun 19 '21 at 01:11

1 Answers1

1

Any basic tutorial like this one shows how to do it, so it's not clear what you're having trouble with, since you didn't specify what issues you're having. This should work in your code though:

# goes at the top of the script:
image_to_draw = pygame.image.load('something.png')

# to draw the image:
WIN.blit(image_to_draw, (x, y, 50, 50))
Random Davis
  • 6,662
  • 4
  • 14
  • 24