This question already has answers here:
How do I maximize the display screen in PyGame?
(6 answers)
Closed 6 days ago.
How do I "maximize" a window in pygame, with "maximize" I mean this:
Not this:
Do you have any idea of how can this be done?
You can maximize the Pygame window using the set_mode() method and passing the pygame.FULLSCREEN argument:
import pygame
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# your code here
pygame.quit()
This will make the window display in full screen. If you want to exit full screen mode, just press the "Esc" key or call the set_mode() method again, this time without the pygame.FULLSCREEN argument.
This question already has answers here:
The fullscreen mode in pygame is entirely black
(1 answer)
Switching to Pygame Fullscreen Mode working only one time
(1 answer)
Closed 2 years ago.
I am developing a game and I went into a problem which I could not solve. I want to have a pygame screen which adjusts in every screen, because you can see more of the game background when you have a very large screen and when you have a small screen, you will only see a small piece of the game. I dont know how to fix this. (If it is possible, it would be nice when I can scale every object together)
Code:
pygame.init()
os.environ['SDL_VIDEO_WINDOW_POS'] = "0,0"
display_info = pygame.display.Info()
MONITOR_SIZE = width, height = pyautogui.size()
pygame.display.set_caption("Stickman fight")
screen = pygame.display.set_mode(MONITOR_SIZE, pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.FULLSCREEN, 32)
bgs = [pygame.transform.scale2x(pygame.image.load("src/Img/Bg1.jpg").convert_alpha())]
This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 2 years ago.
I recently started to learn to use the pygame module and I've run into my first blocker. I an trying to paste an image of a circle in a grid that I created on top of my solid background. For some reason when I blit the image and update the screen nothing appears and I am not sure why as I am pretty sure that my code is correct. I've pasted my code below. Maybe you guys will see what I am missing.
from pygame import *
def main():
init()
display.set_caption("Othello")
screen = display.set_mode((480,480))
screen.fill((0,128,0))
draw.line(screen,(0,0,0),(60,0),(60,480))
draw.line(screen,(0,0,0),(120,0),(120,480))
draw.line(screen,(0,0,0),(180,0),(180,480))
draw.line(screen,(0,0,0),(240,0),(240,480))
draw.line(screen,(0,0,0),(300,0),(300,480))
draw.line(screen,(0,0,0),(360,0),(360,480))
draw.line(screen,(0,0,0),(420,0),(420,480))
draw.line(screen,(0,0,0),(0,60),(480,60))
draw.line(screen,(0,0,0),(0,120),(480,120))
draw.line(screen,(0,0,0),(0,180),(480,180))
draw.line(screen,(0,0,0),(0,240),(480,240))
draw.line(screen,(0,0,0),(0,300),(480,300))
draw.line(screen,(0,0,0),(0,360),(480,360))
draw.line(screen,(0,0,0),(0,420),(480,420))
display.flip()
image_white = image.load("White_Side.jpg")
image_black = image.load("Black_Side.jif")
screen.blit(image_white,(0,0))
screen.blit(image_black,(60,0))
display.update()
running = True
while running:
for pygame_event in event.get():
if pygame_event.type == QUIT:
running = False
if __name__=="__main__":
main()
This question already has answers here:
How can I create a text input box with Pygame?
(5 answers)
Closed 3 years ago.
All I need right now is basic text fields and buttons for input in pygames. A text field as some simple structure that I can read into variables, and a button to call a function.
First, I browsed around and found it was not a straight-forward process to create a text field in pygames. Eventually, by cobbling together this wall of code, I became the proud father of one, somewhat broken text field. Then I looked into buttons, and found to some horror that implementing them is even more complicated.
Surely PyGame has some sort of buttons module built in right? No.
Excuse me? I'm not trying to write a whole program just for one button, I just need the interface for a deeper simulation. That's all.
So then I looked into Tkinter, which has very easy-to-understand commands for GUI input. But no, that was also not meant to be.
I don't believe you can embed tkinter in to pygame.
So then I tried PGU, but found a stunning lack of any straight-forward examples of how to actually use it for what I need (simple text fields and buttons). When I tried looking for one, I found this piece of wisdom.
Are there any good, modern widget toolkits for Pygame? No. Every year someone makes a new Pygame UI library and then abandons it after a few versions
So if that was true, how is anyone supposed to get anything done with this language? What exactly is the best practice for a simple textfield and simple button in a pygame environment?
yes - pygame is barebones "we control the pixels" - and has been around a lot of time, and itself looked like abandoned for a lot of time (it was just in late 2016 that it would install clean on Python 3, for example). So, in the meantime pygame looked abandoned people moved away.
For the time being, you have to try your luck mining a usable toolkit from here:
https://www.pygame.org/wiki/gui
The PGU toolkit listed there, as you noted, seems quite complete - bar examples on how to build simple widgets to interact with an existing Pygame codebase. All the examples assume one wants to just have an stand alne gui application for form filling, which, I agree, is quite inapropriate: if one wants this kind of application, just use Tkiner, Qt or create a web app.
It turns out, however, that using a standalone widget in an existing pygame window is quite simple. Despite all the library examples calling the .run method, which is the spoiler part, since nce you call run the PGU app takes over and there is no way to run your code back, save for callbacks. However, the run method itself consist of a very simple infinite loop, calling the app's loop method. You just have to put a call to app.loop in all frames of your own pygame loop and have the widgets working.
It will swallow pygame events, however - if you want to treat event while you are displaying widgets, you should dispatch all events to app.event and call app.update instead of calling app.loop. It is trivial once you look at the source code for the app class at https://github.com/parogers/pgu/blob/master/pgu/gui/app.py
So, a complete example for using a text-input can be:
import pygame
from pgu import gui
SIZE = W, H = 800, 600
def bouncing_rect(screen):
surf = pygame.Surface((50, 50))
rect = surf.get_rect()
surf.fill((255, 0, 0))
vx, vy = 10, 10
while True:
screen.blit(surf, rect)
new_surface = yield rect
if new_surface:
surf = new_surface
rect.width = surf.get_width()
rect.height = surf.get_height()
rect.x += vx
rect.y += vy
if rect.right > W or rect.left < 0:
vx = -vx
rect.x += vx * 2
if rect.bottom > H or rect.top < 0:
vy = -vy
rect.y += vy * 2
def main():
screen = pygame.display.set_mode((SIZE))
app = gui.Desktop()
txt = gui.Input()
app.init(widget=txt, screen=screen, area=pygame.Rect(50, 50, 200,25))
bouncing = bouncing_rect(screen)
previous_value = ""
font = pygame.font.SysFont(name="Sans", size=30)
while True:
screen.fill((0,0,0))
rect = next(bouncing)
app.loop()
if txt.value != previous_value:
previous_value = txt.value
rect = bouncing.send(font.render(txt.value, True, (255, 255, 0)))
pygame.display.update([rect])
app.repaint()
pygame.display.flip()
pygame.time.delay(30)
try:
main()
finally:
pygame.quit()
(I just made the bouncing object as generator function using the "send" method, since there are very few examples of this pattern, but it could be a class just the same - if it were a common pygame.Sprite child class, I'd just call "update" instead of using next/send. )
This question already has answers here:
Making the background move sideways in pygame
(2 answers)
How to scroll the background surface in PyGame?
(1 answer)
How to move the background image with keys in pygame?
(1 answer)
Pygame : Two layered scrolling background, can you help me?
(1 answer)
Closed 2 years ago.
I'm trying to convert a simple game written in pygame in pyglet.
I almost figure out everything, but now I'm facing this situation,
I have a background sprite like this example:
In my pygame code I use to write this to let it scrolls smoothly in the background:
base_shift = self.graphic.IMAGES['base'].get_width() - self.COMMON_CONST['screenWidth']
while True:
base_x = -((-base_x + 3) % base_shift)
self.screen.blit(self.graphic.IMAGES['base'], (base_x, y))
Basically the image is longer than the actual screen and once it passes the delta value between image X and screen X, the image is set to 0 again.
I'm trying to do the same in pyglet inside the def update(self, dt): function but I have no idea exactly how to do it. I mean I understand the meaning of dt but still.. To move the image now I do something like this:
def update(self, dt):
self.base.x -= 15 * dt
Of course this will continue to move and it does not reset his position.
How can I replicate the behavior that I have done in pygame?
thanks
UPDATE
I try this code:
def update(dt):
self.base.x = -((-(self.base.x - 15 * dt)) % base_shift)
It seems to work but it's not smooth when is back to 0 is not perfect aligned.
UPDATE/2 TEMPORARY SOLUTION
I finally went to another completely different solution. I split the image in a single pattern something like this:
and then I repeat it many times to fill the screen width plus 2. Then I move all of them and when the first goes completely out of the screen size, I remove from the array and add a new one.
I don't know if there is a better solution so far is ok.