Fullscreen & Resizing - Pygame Tutorial

Поделиться
HTML-код
  • Опубликовано: 24 дек 2024

Комментарии • 95

  • @Traumatizes
    @Traumatizes 4 года назад +60

    You and Tech with Tim are goooold!

  • @DaFluffyPotato
    @DaFluffyPotato  4 года назад +28

    Forgot to boost the volume. Whoops.

  • @vaibhavkrkm
    @vaibhavkrkm 4 года назад +9

    Nice tutorial!!
    By the way, I have a tip, if anyone is having issues with the monitor resolutions in full screen mode, you can also give default window or any custom resolutions which can fix the problem as well, and that may fix the issue, cuz in some cases, we may have problems to properly set the window size to the monitor size accordingly with all the elements of the game!!

    • @suspensed_
      @suspensed_ 4 года назад +1

      Hi.
      How I can do this?

    • @vaibhavkrkm
      @vaibhavkrkm 4 года назад

      @@suspensed_ What problem are u facing?

    • @suspensed_
      @suspensed_ 4 года назад +1

      @@vaibhavkrkm I did it, Thanks

    • @vaibhavkrkm
      @vaibhavkrkm 4 года назад

      @@suspensed_ 👍👍👍

  • @powerupminion
    @powerupminion 2 года назад +4

    Currently recoding my pygame framework that i use for my projects. Quickly implemented your method with the choice when creating the framework object, you have a setting if the window should be resizable or not (of cource along with some scaling features and that sort of stuff):
    def __init__(self, title: str, windowSize: (int, int), quitMethod: staticmethod = pgQuit, backgroundColor: (int, int, int) = color.black, fps: int = 0, windowResizable: [True, False] = False): """Pygame setup rutine with all my preffered settings"""
    @property
    def windowSize(self) -> (int, int): """Shorthand for getting the size as (x, y) of the current window"""
    def windowSizeSet(self, windowSize: (int, int)): """Manual screen size setting"""
    def windowScaleReset(self): """Reset the origin screen size value that controls the scaling calulation"""
    @property
    def windowScale(self) -> (float, float): """A tuple (x, y) of the scaling from the original window size (current / origin)"""
    @property
    def windowPoints(self) -> list: """8 points on the screen (corners, middle edges and window center) dynamicly changing with screen size that can easily be used as anchers for elements"""
    I use a programmable event handler build on top of pygames event handler. The user can assign keys and assosiate a method to run when that key is in a chosen state. This is the buisness end of that honestly beautiful method (I realy love this kind of thing):
    def tick(self):
    """Run event handler. Must be run each frame."""
    for event in pg.event.get():
    if event.type == pg.QUIT:
    self.__eventQuit()
    elif event.type == pg.VIDEORESIZE:
    windowNew((event.w, event.h), self.__allowWindowResize)
    elif event.type == pg.KEYDOWN:
    self.__keyTest(event, self.keyDown)
    elif event.type == pg.KEYUP:
    self.__keyTest(event, self.keyUp)
    if len(self.__events["held"]) > 0:
    keys = pg.key.get_pressed()
    for event in self.__events["held"]:
    if keys[event[0]]: event[1]()
    I break my code up into very small snippits of code so this alone will do pretty much nothing on its own. The last line does however provide a significant clue to what this event handler does to execute functions according to key presses. What is missing here is the data handling and my underlying library build on top of pygame witch is where functions like "windowNew" comes from.
    My framework is just for the purpose of I can just call:
    WINDOW = framework("title", (x, y)...)
    ...and have all the stuff i need such as event handler, rendering, resizing, drawing, sound and much more already implemented from the start and then I can focus on my actual project.
    btw... If you are playing with pygame or other similar things in python, you NEED stuff like this:
    from dataclasses import dataclass
    @dataclass
    class color:
    """The 8 basic colors.
    teachengineering.org/content/spfun_/maker_challenges/spfun_rgbcolor_maker1_image1.png"""
    black: tuple = (0, 0, 0)
    white: tuple = (255, 255, 255)
    red: tuple = (255, 0, 0)
    green: tuple = (0, 255, 0)
    blue: tuple = (0, 0, 255)
    cyan: tuple = (0, 255, 255)
    magenta: tuple = (255, 0, 255)
    yellow: tuple = (255, 255, 0)
    @dataclass
    class resolution:
    """Basic screen resolutions."""
    x360p = (640, 360)
    x480p = (854, 480)
    x720p = (1280, 720)
    x1080p = (1920, 1080)
    x2k = (2560, 1440)
    x2_7k = (2704, 1520)
    x4k = (3840, 2160)
    x8k = (7680, 4320)

  • @KaleakEnma
    @KaleakEnma 2 года назад

    Thank u bro u just started a revolution

  • @ajiteshkumar
    @ajiteshkumar 3 года назад +2

    This helped a lot. Thank you so much!

  • @muhammadharisaamir3952
    @muhammadharisaamir3952 4 года назад +1

    That was exactly what I was looking for right now .... Thanks a lot !!!

  • @dartznight
    @dartznight 4 года назад +1

    You get a like on every single pygame video my helpful guy

  • @juniorgamerguy6074
    @juniorgamerguy6074 4 года назад +2

    You make so many useful video's thank you!!!

  • @bencusb
    @bencusb 4 года назад +3

    first!
    On this topic, scaling your screen to a specific resolution whould make a great video.

    • @dafluffypotatovr7362
      @dafluffypotatovr7362 4 года назад +2

      You do that every time you make a window. Just put static values for the dimensions when redefining your screen.

  • @unabonger777
    @unabonger777 4 года назад +1

    great info, thanks for sharing.

  • @emmanuelwestra6524
    @emmanuelwestra6524 3 года назад

    Thank you so much! That was very helpful.

  • @azalty
    @azalty 2 года назад

    Thanks a lot for this tutorial!

  • @Iuigi_t
    @Iuigi_t Год назад +1

    Da color realistic dell monitor, nice

  • @ktrilex7001
    @ktrilex7001 2 года назад +1

    Thank you for this tutorial! It really helped me.

  • @gavinthecrafter
    @gavinthecrafter 4 года назад +11

    Great video! Just one question: How can I resize the window only internally, without the user being able to resize it themselves?

    • @DaFluffyPotato
      @DaFluffyPotato  4 года назад +13

      Just redefine the screen to a different resolution

  • @MaxwellPrehoda
    @MaxwellPrehoda 4 года назад +7

    Are you gonna make any new tutorials soon? Love your videos and have learned PyGame and started developing my own platformer solely with the resources you've shown on your channel

    • @dafluffypotatovr7362
      @dafluffypotatovr7362 4 года назад +1

      Yep. I've just been a bit busy with work lately. I'm hoping to record another video tomorrow. (It may not be edited & published then though)

  • @kefrov
    @kefrov 3 года назад

    This is what I did too, before starting to use Kivy.

  • @vissandre3845
    @vissandre3845 3 года назад +2

    YOO ok thats cool but when I go to fullscreen and then go back to small screen the placement of the window is topleft and I cant grab the top bar to move it down what do I do??

  • @matter8405
    @matter8405 3 года назад +3

    When i resize back to normal it puts the window in the top left of the screen instead of the middle. How do I fix that?

    • @aarondraws7699
      @aarondraws7699 3 года назад

      got the same issue the window also becomes borderless

    • @sn0wyb0ss53
      @sn0wyb0ss53 3 года назад

      i found a way to fix this, just use pygame.display.toggle_fullscreen()

  • @wolnyczowiek8705
    @wolnyczowiek8705 Год назад +1

    After switching to fullscreen and then back to normal screen resizing doesn't work

  • @redthunder6183
    @redthunder6183 3 года назад +2

    I was working on my second game and I wanted it to have fullscreen capabilities, however, I think it would be really annoying if there isn't a way to exit fullscreen mode and when ever I try to exit the full-screen mode by using "screen= pygame.display.set_mode((500,500), pygame.RESIZABLE)" it makes a smaller window in the top left corner 500 by 500, but there isn't a task bar at the top that will let you drag the window around (the second you resize the window, the bar pops up again). What can I do to get it to go back to the basic window with the taskbar at the top, and have the window at the center of the screen rather than the top left?

    • @hexagoat4915
      @hexagoat4915 3 года назад

      same, have you solved it?

    • @redthunder6183
      @redthunder6183 3 года назад

      @@hexagoat4915 no, I have no clue what to do, but if I ever activate the videoresize in event.get, even if I don’t resize it at all, the bar pops back up again. Have you fixed it at all?

    • @pyplatformer869
      @pyplatformer869 3 года назад +1

      @@redthunder6183 pygame 1.9.6 doesn't have this issue as far as I know, I personally have only faced this problem with pygame 2.

  • @threepoint1434
    @threepoint1434 4 года назад +3

    How can one do this with images? Like, how to make an image stretch like that rectangle...? An an extra video about that would be nice. I can't find an answer to this question anywhere else.

    • @DaFluffyPotato
      @DaFluffyPotato  4 года назад +2

      I did a video on that. It’s called transformations.

    • @threepoint1434
      @threepoint1434 4 года назад +4

      @@DaFluffyPotato Yes. I know how transformations work. But when stretching the image it gets cut off. Like, after I just resized the actually window, the image also resizes like it should. The problem is; the new area where the image should stretch to, there is nothing, but black.
      It's rather tricky so explain. It's like the new black area is overlapping everything else.
      I don't know if you have a solution for this. I'll try some ideas of my own.
      I've already tried to blit the image after the screen, but that didn't work.

    • @threepoint1434
      @threepoint1434 4 года назад +2

      Never mind. I found something relevant to my question:
      import os
      import pygame
      from pygame.locals import *
      pygame.init()
      screen = pygame.display.set_mode((500, 500), HWSURFACE | DOUBLEBUF | RESIZABLE)
      pic = pygame.image.load("image.png")
      screen.blit(pygame.transform.scale(pic, (500, 500)), (0, 0))
      pygame.display.flip()
      while True:
      pygame.event.pump()
      event = pygame.event.wait()
      if event.type == QUIT:
      pygame.display.quit()
      elif event.type == VIDEORESIZE:
      screen = pygame.display.set_mode(
      event.dict['size'], HWSURFACE | DOUBLEBUF | RESIZABLE)
      screen.blit(pygame.transform.scale(pic, event.dict['size']), (0, 0))
      pygame.display.flip()
      This will cause the image to fill the window, like a background.

    • @dafluffypotatovr7362
      @dafluffypotatovr7362 4 года назад +2

      @@threepoint1434 So you're saying that the new space added to the window is black after you resize the window? That's what happens when you don't redefine the screen with the resolution of the resized window. (I believe I covered that in this video.)

  • @Klannahar
    @Klannahar 4 года назад +1

    Hi,
    Can u make smaller resolution on fullscreen mode then the actual screen resolution? I mean if u have a full HD resolution but u wana use only a half of that screen size in your project in full screen mode. Full HD is not a good resolution for pixel art, unless u using big sprites what will have a hell lot of pain to animate them.

    • @DaFluffyPotato
      @DaFluffyPotato  4 года назад +1

      You can add black borders by adjusting what’s actually drawn in the window.

    • @Klannahar
      @Klannahar 4 года назад

      @@DaFluffyPotato It is already adding black borders around the "screen" if it is smaller then the actual monitor resolution what i do not want. I wanted to try to avoid to scale up every image what i am blit-ing on screen.

  • @tanwyhang
    @tanwyhang 2 года назад

    hi dafluffy potato, can i know where did you get the white border for your videos , or did you draw it urself, because i wish to use it on my game project

  • @VictorPerez-od7zh
    @VictorPerez-od7zh 2 года назад +2

    You could also use pygame.display.get_desktop_sizes()[0] if you're using pygame 2

  • @AntoineVanGeyseghem
    @AntoineVanGeyseghem Год назад

    Wow ! :o

  • @LawrenceAaronLuther
    @LawrenceAaronLuther 7 месяцев назад

    does anyone know how to use fixed resolutions as mentioned at 0:21?

  • @mauriciopereira2263
    @mauriciopereira2263 2 года назад

    How could I change the size of a surface, text, or label by clicking a button ? I am creating a grid (similar to a minesweeper game) and I want to change the amount of rows and columns by clicking buttons with options. There is something about the main loop that I am struggling to figure out...

  • @arbitervildred8999
    @arbitervildred8999 Год назад

    but when you go into fullscreen, how do you make it to not stretch your image having specific resolutions doesn't always scale well sometimes it stretches the image
    for example a game resolution of 1728x972 would stretch bad on a wide/ultra wide monitor, even on a 1920x1080 monitor and lose the pixel art style

  • @tristandombroski9677
    @tristandombroski9677 Год назад

    working with a program that has multiple menu's (based off your menu example in another video) where would I put the if event.type == VIDEORESIZE: ? would I put it in the def main_menu() or def game()?

    • @tristandombroski9677
      @tristandombroski9677 Год назад

      might just test it out and update this.

    • @tristandombroski9677
      @tristandombroski9677 Год назад

      so oddly enough throwing that in the def main_menu loop with the other if event statements bugged my screen out saying:
      Traceback (most recent call last):
      Python\Python311\Click Ranger\ClickRanger.py", line 91, in main_menu()
      Python\Python311\Click Ranger\ClickRanger.py", line 41, in main_menu screen.fill('gray')
      UnboundLocalError: cannot access local variable 'screen' where it is not associated with a value
      going to keep moving stuff around, I thought about defining a new class like def window_resize, but then I realized what if the player resizes in a different menu like options, the game screen, ect. Should I just stick to static resolutions like you mentioned? create an options menu with buttons that change the resolution when clicked? That is do-able.

    • @tristandombroski9677
      @tristandombroski9677 Год назад

      okay, an update to that again, because I am using a screen.fill('lightgray') whenever I resize, it's all normals and my buttons are locked into place..... I don't need to handle all that other stuff I guess. I am still new-ish to this, about a month now.

  • @dominicballinger6536
    @dominicballinger6536 2 года назад

    Thanks! I've been wondering about this for a bit. One thing though, could I get pygame to keep the shape of the objects the same if I make the window the same aspect ratio of the monitor? I.E. if I make my window some values that keep a ratio of 16:9 would all the shapes stay the same when it goes to full screen?

    • @hiposter2601
      @hiposter2601 2 года назад

      hi, I also faced such a problem, here I think you can initially make sprites of a small shape, so that when you stretch the tap they are as shape as you want

    • @dominicballinger6536
      @dominicballinger6536 2 года назад +1

      @@hiposter2601 I just figured it out! So there is a pygame.FULLSCREEN parameter you can feed to the display. But then if you type in " | pygame.SCALED" it scales to your monitor very easily!

    • @hiposter2601
      @hiposter2601 2 года назад

      @@dominicballinger6536 Thank you!

  • @aarushiswinner_yo
    @aarushiswinner_yo 2 года назад

    Nice video, but my problem is the cursor, How to hide the cursor wheen in fullscreen?

  • @yurii.okereshko
    @yurii.okereshko 4 года назад +2

    Sometimes Pygame scale display surface. To avoid that put this function:
    ctypes.windll.user32.SetProcessDPIAware()
    Maybe somebody need this for 1080p projects)

  • @machinze_edits
    @machinze_edits 3 года назад

    Any idea or website where we can learn more pygame physics 🤔🤔

  • @feeltheblank7357
    @feeltheblank7357 4 года назад

    I faced a problem. Whenever I try to resize the window, the extended part becomes white and the window does not fit the frame. I am using pygame 2.0.0. Please advise.

  • @basukhadka
    @basukhadka 3 года назад +1

    can you make so that as soon as you open your pygame window it automatically fits the screen without clicking the expand button?

    • @dtimer319
      @dtimer319 2 года назад

      Just as soon as you define your screen set the resolution to that of monitor_size = [pygame.display.Info().current_w, pygame.display.Info().current_h]

  • @FreeForParrots
    @FreeForParrots 2 года назад

    Very usefull! But i can't understand how to scale buttons of your example with screen

  • @robertspakarklis3125
    @robertspakarklis3125 4 года назад +1

    Could you make a video on how to optimize pygame code?

    • @DaFluffyPotato
      @DaFluffyPotato  4 года назад +4

      I spent a decent amount of time on that topic in this video: ruclips.net/video/s3aYw54R5KY/видео.html

  • @TheOfficialKingIdea
    @TheOfficialKingIdea 2 года назад

    CAN YOU PLEASE MAKE A TUTORIAL FOR THE RECTANGLE!? Like a full explanation for how it works? I really need help with scaling the rectangle with the screen in my game. if you respond, then Thank you:)

  • @ashmitgarg8930
    @ashmitgarg8930 3 года назад

    👍🏻

  • @earthbender731
    @earthbender731 4 месяца назад

    dude this is a bit late but i noticed you have the same dell monitor 😂

  • @Dragon20C
    @Dragon20C 4 года назад

    When I type VIDEORESIZE it says
    Traceback (most recent call last):
    File "/home/user/PycharmProjects/Game/main.py", line 30, in
    if event.type == VIDEORESIZE:
    NameError: name 'VIDEORESIZE' is not defined

    • @DaFluffyPotato
      @DaFluffyPotato  4 года назад

      You need from pygame.locals import *

    • @Dragon20C
      @Dragon20C 4 года назад

      @@DaFluffyPotato alright, will see if it works! Also Thanks for the reply!

  • @ahmednader1887
    @ahmednader1887 3 года назад

    i tried to make it with 2 surface and is a lot of glitches

  • @disdis6127
    @disdis6127 4 года назад

    please make video on coding RPG in pygame

  • @gnanarajsubramanian851
    @gnanarajsubramanian851 4 года назад

    Its not properly working after adding images...

    • @DaFluffyPotato
      @DaFluffyPotato  4 года назад

      Not working in what sense? As in the images aren't getting bigger/smaller? You have to do that yourself.

  • @juicedelemon
    @juicedelemon 2 года назад

    that is really nice but damn, that is long calculation

  • @generalpilipi3201
    @generalpilipi3201 2 года назад

    Link is not working !

    • @DaFluffyPotato
      @DaFluffyPotato  2 года назад

      pastebin is apparently blocked in some countries. I switched to self-hosting stuff for my newer videos though.

  • @rotcbsu1651
    @rotcbsu1651 4 года назад +1

    notice me senpai

  • @GeJunior
    @GeJunior 4 года назад

    coloca legenda em portugues please!

    • @electricimpulsetoprogramming
      @electricimpulsetoprogramming 2 года назад

      Para de depender desse idioma lixo, se tu quiser ser bem sucedido na tua vida aprende esse idioma o quanto antes