Pygame screen size not matching window size [duplicate] - python

I'm starting to explore Python and Pygame, however I am running into a problem. Everything I draw to the screen is only displayed in the top left quarter of the window. I thought it was my code but any demo programs I tried also display the same way.
Demo code from thenewboston on youtube
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,360),0,32)
background = pygame.image.load("Background.jpg").convert()
mouse_c = pygame.image.load("ball.png").convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -= mouse_c.get_height()/2
screen.blit(mouse_c, (x,y))
pygame.display.update()
In the video his displays correctly and mine looks like this
Using:
Python 2.7.4 32 bit
pygame 1.9.1 32 bit
mac 10.8.3 64 bit on macbook pro retina
I think either it has to do with the retina display or I installed something wrong. Any help would be appreciated.

The problem is, in fact, with the resolution of the retina screen. In order to get pygame to work correctly, download setresx and use it to set your resolution to any setting without HiDPI.

Related

Pygame: window resizing infrequently

I have a little problem with the pygame event system. Calling set_mode(...) makes events of type VIDEORESIZE come up very infrequently. I was able to reproduce this in a little script similar to the pygame resize example from the docs:
#!/usr/bin/python3
import sys
import pygame
from pygame.locals import *
WHITE = (255, 255, 255)
pygame.init()
flags = HWSURFACE|DOUBLEBUF|RESIZABLE
screen = pygame.display.set_mode((300, 300), flags)
screen.fill(WHITE)
pygame.display.flip()
while True:
for event in pygame.event.get():
print(event)
if event.type is QUIT:
pygame.display.quit()
pygame.quit()
sys.exit()
elif event.type is VIDEORESIZE:
# Version 1: do nothing
#pass
# Version 2: set_mode
screen = pygame.display.set_mode(event.size, flags)
screen.fill(WHITE)
pygame.display.flip()
Version 1: working as expected, all VIDEORESIZE events are shown, the white surface is not rezising.
Version 2: all events except 95% of the VIDEORESIZE events are shown, the white surface is resizing whenever a resize event comes up (the first one to three of them always pop up instantly, probably only first pygame.event.get() call).
Everything else is working just fine.
SW/HW, single/double buffering does not make any difference.
Python 3.7.3
Pygame 1.9.5
Linux Mint 19.2
Cinnamon 4.2.4
Linux 4.15.0-66
There were other posts here but they either did not call set_mode or had other 'symptoms'
Thanks for your help!
Edit: Works on Windows 10, even though there is only one resize event per drag, so no 'live resizing'. Seems to be a pygame bug, any ideas how to get around it?
For those who experience the same issue, here is a mostly functioning workaround (at least on my linux machine):
Calling set_mode() only when the user releases the mouse button prevents pygame from spamming the window manager. This way there is no 'live resizing' while dragging but the VIDEORESIZE events pop up much more frequently.
[...]
elif event.type is pygame.VIDEORESIZE:
resized = True
updateSize = event.size
elif event.type is pygame.ACTIVEEVENT and resized:
screen = pygame.display.set_mode(updateSize, flags)
If it is not working, check if there is an ACTIVEEVENT when you release the mouse button after dragging the window border. Also note, that this will cause the window size to jump to the size of the last received resize event (which can differ from the actual size if resize events are infrequent again).

why wont python pygame wont work in spyder?

I recently downloaded pygame on my school pc(I have experience with it before), but when I try to open a window it does not work and are black even though I colored the window in the code.
I even copied this code:
import pygame
background_colour = (255,255,255)
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
But it does the same thing.
Extra:
I use Spyder with python 3.6
Thanks in advance:)
Your code work in my mac. If you are in Windows, try to restart the PC. Also, try to execute your example from a command line.

pygame square following mouse cursor

When I run a pygame program, a white square (about 15px by 15px) follows my cursor around the window. This happens on the most basic pygame program and a few others, too.
My most basic pygame program:
import pygame, sys
DISPLAYSURF = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
# --- Limit to 60 frames per second
clock.tick(30)
Why is this happening? Is it my code, my system, or Pygame?
I am running python 3.7, pygame 1.9.4, and macOS 10.13.6.
EDIT:
After running the same program on Crouton (linux) there is no square.
Where is your pygame.init() at the beginning? That might be what is causing the problem. Also you do not have a background, which shouldn't be a problem, but I would try it if nothing else works.
I tried your code on my PC and it worked fine so I'm not sure what is happening but you could try the following:
Enter the command pygame.init() on the second line
Replace pygame.display.update() with pygame.display.flip()

Images distorted using pygame

Trying to build my first pygame project and wanted to import the following map. Used image:
My code is:
import pygame
pygame.init()
size = (1300, 700)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
done = False
clock = pygame.time.Clock()
map = pygame.image.load('map.jpg')
map = map.convert()
map = pygame.transform.scale(map, (466,700))
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill((255,255,255))
screen.blit(map, (430, 0))
pygame.display.flip()
clock.tick(60)
pygame.quit()
and the output looks like:
I have also tried converting the jpg to a bmp but still the same results. I cannot figure out what is distorting the image, trying with other images it appears that the outputted images is slightly wider than the original. I suspect that pygame is just reading the width wrong and then putting the pixels into an array with that width.
Any advice on fixing this would be much appreciated.
There is a bug in SDL_image (which Pygame is based on) that corrupts images in OS X 10.11.
https://bugzilla.libsdl.org/show_bug.cgi?id=3154
Currently, the only known workaround is to downgrade to SDL_image 1.2.10, but I have not been able to confirm.

Python pygame window keeps crashing

Whenever I run my code the Python Window that shows up does not respond.
Is there something wrong with my code or do I have to re-install pygame and python?
I get a black pygame window and then it turns white and says not responding?
Also I am new to this so please make this as simple as possible. I tried looking everywhere for the answer but could not get it in a way that I could understand.
Please help me out. Thanks :)
1 - Import library
import pygame
from pygame.locals import *
2 - Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
3 - Load Images
player = pygame.images.load("resources/images/dude.png")
4 - keep looping through
while 1:
# 5 - clear the screen before drawing it again
screen.fill(0)
# 6 - draw the screen elements
screen.blit(player, (100,100))
# 7 - update the screen
pygame.display.flip()
# 8 - loop through the events
for event in pygame.event.get():
# check if the event is the X button
if event.type==pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
Don't import pygame.locals. It is actually unnecessary, since you are already importing pygame.
Also, as #furas said, it should be:
player = pygame.image.load("resources/images/dude.png")
Not:
player = pygame.images.load("resources/images/dude.png")
This will clear up some of the problems in your code.
From my personal experience,if you run pygame code from IDLE it often does not respond at all.Try saving your project as a .py file and then run it with python.exe.It always works for me.
And as furas said use
player = pygame.image.load("resources/images/dude.png")
instead of
player = pygame.images.load("resources/images/dude.png")
Replace that for loop with this
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

Categories

Resources