How to go about drawing the border of a rectangle in a resizable window using pygame?

Rayquados

I'm trying to create a resizable window in pygame in which a bordered rectangle should be in a fixed proportion with the window.

import pygame,sys 
pygame.init()

win = pygame.display.set_mode((400,400),pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)

while True:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.VIDEORESIZE:
            pygame.display.set_mode((event.w,event.h),pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)

    win_width,win_height = pygame.display.get_surface().get_size()
    surf = pygame.Surface((round(win_width/2),round(win_height/2)))
    surf.fill((255,255,255))
    win.blit(surf,(win_width/4,win_height/4))
    pygame.draw.rect(surf,(255,0,0),(0,0,surf.get_width(),surf.get_height()),10)
    pygame.display.update()

Thanks in advance!

Rabbid76

You draw the rectangle on the white surface (surf). Hence you've to draw it on surf, before you blit surf on win:

while True:
    # [...]

    surf.fill((255,255,255))
    
    pygame.draw.rect(surf,(255,0,0),(0,0,surf.get_width(),surf.get_height()),10)
    win.blit(surf,(win_width/4,win_height/4))
    
    pygame.display.update()   

Alternatively, you can draw a rectangle directly on win:

while True:
    # [...]

    surf.fill((255,255,255))
    rect = win.blit(surf,(win_width/4,win_height/4))
    pygame.draw.rect(win, (255,0,0), rect, 10)
    pygame.display.update()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pygame: Drawing a border of a rectangle

From Dev

Drawing rectangle with border only in matplotlib

From Dev

svg rectangle border not drawing properly

From Java

How to make a window border for a circle object in pygame

From Dev

Pygame not drawing rectangle while clicking

From Dev

How to make a resizable rectangle in QML?

From Dev

javaFX - How to create a resizable rectangle?

From Dev

Make a pygame resizable window that can be snapped to the screen

From Dev

How would I go about drawing a box area using lines in Babylonjs?

From Dev

Qt drawing a filled rounded rectangle with border

From Dev

How to use a resizable rounded rectangle as KeyIcon

From Dev

How to make a Tkinter window not resizable?

From Java

How to move a rectangle in pygame

From Dev

How to add a border to a rectangle in Java using setBorder and JFrame

From Dev

Java SWT: How to set the window to be resizable \ not resizable on the fly?

From Dev

Making a window resizable in Java using KeyEvent

From Dev

How to draw a point on a border of a rectangle?

From Dev

Drawing rectangle border is always white (on live camera stream)

From Dev

How to add text into a pygame rectangle

From Dev

How to add text into a pygame rectangle

From Dev

resizable and movable rectangle

From Dev

Drawing rectangle using mouse events in Tkinter

From Dev

Drawing a rectangle with asterisks using methods and loops

From Dev

Drawing a rectangle on UIImageView using Core Graphics

From Dev

Drawing resizable rectangles on Canvas

From Dev

Drawing a rectangle on some area in window causing screen flickering

From Dev

How to change the Drawing Rectangle and Thumb Image for a UISlider

From Dev

How to Temporarily Stop Drawing After Rectangle Complete

From Dev

SpriteKit: How to animate drawing of a rounded rectangle

Related Related

  1. 1

    Pygame: Drawing a border of a rectangle

  2. 2

    Drawing rectangle with border only in matplotlib

  3. 3

    svg rectangle border not drawing properly

  4. 4

    How to make a window border for a circle object in pygame

  5. 5

    Pygame not drawing rectangle while clicking

  6. 6

    How to make a resizable rectangle in QML?

  7. 7

    javaFX - How to create a resizable rectangle?

  8. 8

    Make a pygame resizable window that can be snapped to the screen

  9. 9

    How would I go about drawing a box area using lines in Babylonjs?

  10. 10

    Qt drawing a filled rounded rectangle with border

  11. 11

    How to use a resizable rounded rectangle as KeyIcon

  12. 12

    How to make a Tkinter window not resizable?

  13. 13

    How to move a rectangle in pygame

  14. 14

    How to add a border to a rectangle in Java using setBorder and JFrame

  15. 15

    Java SWT: How to set the window to be resizable \ not resizable on the fly?

  16. 16

    Making a window resizable in Java using KeyEvent

  17. 17

    How to draw a point on a border of a rectangle?

  18. 18

    Drawing rectangle border is always white (on live camera stream)

  19. 19

    How to add text into a pygame rectangle

  20. 20

    How to add text into a pygame rectangle

  21. 21

    resizable and movable rectangle

  22. 22

    Drawing rectangle using mouse events in Tkinter

  23. 23

    Drawing a rectangle with asterisks using methods and loops

  24. 24

    Drawing a rectangle on UIImageView using Core Graphics

  25. 25

    Drawing resizable rectangles on Canvas

  26. 26

    Drawing a rectangle on some area in window causing screen flickering

  27. 27

    How to change the Drawing Rectangle and Thumb Image for a UISlider

  28. 28

    How to Temporarily Stop Drawing After Rectangle Complete

  29. 29

    SpriteKit: How to animate drawing of a rounded rectangle

HotTag

Archive