Screen displays only in top left corner of window - 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

Screen not showing on Mac using pygame

I'm doing a tutorial for using pygame to make games, and I'm having a problem getting a screen to show up. Here is what I have so far:
import pygame
# Initialize the pygame
pygame.init()
# Create the screen, height = 800, width = 600
screen = pygame.display.set_mode((800, 600))
# Game Loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
I have pygame installed, and have no errors or warnings in PyCharm. When I run this (both in PyCharm and my terminal), I get no screen showing, and all that happens is the spaceship of the python runner is bouncing in my dock. When I run this exact code on my pc, it shows a black screen, which is the desired outcome.
Can anyone help, or am I doing something wrong?
Thank you for your assistance.
I have no problem running your code on a Mac. However, the window showing up is a grey one, not black.
In order to force the display of the window, you can add this after the screen creation:
pygame.display.update()
And to try to figure out the problem,you may want to add a text in the window bar, that may generate a message pointing to your problem.
pygame.display.set_caption("Hello world")

pygame on MacOs won't show any surfaces

I'm following a very basic pygame tutorial, which I've done before. I'm suspecting there's a version issue, though I'm at a loss how to move forward.
macOS 10.14.4
Python 3.7.2
pip 18.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
code:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800,600))
surf = pygame.Surface((75,25))
surf.fill((125,125,125))
# What does this rect do?
rect = surf.get_rect()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
screen.blit(surf, (400,300))
pygame.display.flip()
The only question I have there is what is that rect assignment for? I can't blit the rect, so it appears to have no purpose.
I've gone through a couple other tutorials, copy-pasted code and I get the same result with each one: a white screen with no rectangles. It's as if the display code works, but any other surface just don't appear.
No errors in the console. I'm really not sure how to debug at this point.
There is nothing to debug here. rect = surf.get_rect() is not used, maybe like martineau said in the comment will be used later in the tutorial.
What the code is doing is to draw a small gray rectangle on a gray surface (that's why you do not see anything).
surf = pygame.Surface((75,25))
This creates a small gray surface, which is blit later in the main loop at coordinates (top-left corner) 400, 300:
screen.blit(surf, (400,300))
As I said, the reason you do not see it is because surf il filled with gray (this line):
surf.fill((125,125,125))
and your background is gray too. I do not know if this is system dependent, on my linux the background is black and the rectangle is clearly visible.
So try to change the color of the rectangle to something clearer or darker, or fill the display surface with black before the main loop by doing:
screen.fill((0, 0, 0))

Force a fullscreen resolution that is not contained in display.list_modes()

I'm making a simulation for school and I'm trying to make pygame create a fullscreen display in my native resolution. However, I have a QHD screen (2560x1440), and it isn't working properly. As far as I can tell, pygame is rendering a screen at the correct resolution, but expanding it so it is scaled as if it were 1080p, so about 300-400 pixels are cut off around the edges. This causes, for example, a circle rendered at (200,200) to be completely invisible. After some research, I learned that this is because pygame doesn't officially support my resolution (it is not listed in pygame.display.list_modes()). Is there any way to force it to work? I would prefer if I could use my actual resolution instead of upscaled 1080p.
Here is the code that initializes the window:
import pygame
from pygame.locals import *
pygame.init()
w = pygame.display.Info().current_w
h = pygame.display.Info().current_h
S = pygame.display.set_mode((w,h), pygame.FULLSCREEN)
If you are using Windows, make sure in your display settings you have scaling set to 100%. This will make your text and everything smaller if you don't have it at that currently but I think Pygame windows get affected by this number for some reason.
See the below code snippet for making sure your window scales properly. Also see here.
import pygame
from ctypes import windll
def run():
# Force windows to ignore the display scaling value.
windll.user32.SetProcessDPIAware()
pygame.display.init()
# Make the screen the highest resolution that will fit the screen at 100% scaling.
screen = pygame.display.set_mode(pygame.display.list_modes()[0])
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
run()

Pygame Freezes on Exit/Quit

I have done research about this problem, on internet, but there was no solution for my case... The window freezes when i try to close it with (X) button.
And as I said I haven't came across any solution on other posts, so I came here to ask for help. Thank you.
#!/usr/bin/python
import sys
import pygame
# initialize
pygame.init()
# colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
# window size
w_size = [700,500]
main_screen = pygame.display.set_mode(w_size)
# Window info
pygame.display.set_caption("Cancer Cell")
# manage screen update time
clock=pygame.time.Clock()
#background image
bg_img = pygame.image.load("/img/bg_img.png").convert()
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# GAME LOGIC - BEGINNING
# -------- DRAWINGS - BEGINNING --------
main_screen.blit(bg_img, [0,0])
# -------- DRAWINGS - END ----------
# update screen
pygame.display.flip()
clock.tick(20) # 20 FPS limit for loop.
pygame.quit()
Your code work perfectly well on my platform.
My platform:
Scientific Linux 7 (RHEL 7)
Python: 3.4.3
Pygame Version: 1.9.2b8
Window Manager: Gnome3
There is also probably bug in your code.
The "/img/bg_img.png" suggest absolute path, it possible, but very uncommon :).
Works pretty nicely for me. See the demo below:
Not only does your code work as expected on linux Mate 17, python 2.7.6 but it also works with sys.exit() instead of pygame.quit() or nothing at all i.e. no sys.exit() or pygame.quit()
So it turns out that the IDLE is what was causing the problem...
I tried to run the code from commandline and it worked perfectly!
The IDLE that I (was) using is called Wing101.

Pygame display.info giving wrong resolution size

I'm building a small game with pygame. I want the window of the game to be size of the monitors resolution. My computers screen's resolution is 1920x1080 and display.info says the window size is also 1920x1080 but when I run it, it creates a window roughly one and half times the size of my screen.
import pygame, sys
def main():
#set up pygame, main clock
pygame.init()
clock = pygame.time.Clock()
#creates an object with the computers display information
#current_h, current_w gives the monitors height and width
displayInfo = pygame.display.Info()
#set up the window
windowWidth = displayInfo.current_w
windowHeight = displayInfo.current_h
window = pygame.display.set_mode ((windowWidth, windowHeight), 0, 32)
pygame.display.set_caption('game')
#gameLoop
while True:
window.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#draw the window onto the screen
pygame.display.flip()
clock.tick(60)
main()
I've been having the same problem, and I managed to find the answer and posted it here. The answer I found is as follows:
I managed to find a commit on the Pygame BitBucket page here that explains the issue and gives an example on how to fix it.
What is happening is that some display environments can be configured to stretch windows so they don't look small on high PPI (Pixels Per Inch) displays. This stretching is what causes displays on larger resolutions to display larger than they actually are.
They provide an example code on the page I linked to showing how to fix this issue.
They fix the issue by importing ctypes and calling this:
ctypes.windll.user32.SetProcessDPIAware()
They also express that this is a Windows only solution and is available within base Python since Python 2.4. Before that it will need to be installed.
With that said, to make this work, put this bit of code anywhere before pygame.display.set_mode()
import ctypes
ctypes.windll.user32.SetProcessDPIAware()
#
# # # Anywhere Before
#
pygame.display.set_mode(resolution)
I hope this helps you and anyone else who finds they have this same issue.

Categories