PyGame garbles horizontal pixel line at image load - python

I am loading a png file in PyGame which looks fine before I load it.
If you look at the bottom of the image screenshot, a single horizontal pixel line has been distorted. I included the original, and the screenshot. I have tried, smoothscale and normal scale, but nothing seems to make a difference.
Does anyone have some hints for me?
My load code is as follows:
palmTree = 'palmTree.png'
palm = pygame.image.load(palmTree).convert_alpha()
palm = pygame.transform.smoothscale(palm, (palmwidth, palmheight))
screen.blit(palm, (x, y))
pygame.display.flip()
Thanks for the comments below. I believe the problem I have has nothing to do with my program. I ran another program a friend gave me and bingo ... the same problem. Could it have something to do with my Mac Retina display?

The following code shows both the palm tree unaltered as well as scaled down by 10% side by side. I don't see any strange bars with it implemented as I have it.
import pygame,sys,math
from pygame.locals import *
def Main():
pygame.init()
screen_dimensions = 640,480
screen = pygame.display.set_mode(screen_dimensions,0,32)
unaltered_palm = pygame.image.load("palm.png").convert_alpha()
palm_rect = unaltered_palm.get_rect()
scale = palm_rect[2:]
scale[0],scale[1] =int(math.ceil(scale[0]*.9)),int(math.ceil(scale[1]*.9))
altered_palm = pygame.transform.smoothscale(unaltered_palm,scale)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(unaltered_palm,(50,125))
screen.blit (altered_palm,(375,130))
pygame.display.flip()
Main()

Related

image not displaying nicely in pygame

i am trying to display this image in a fullscreen window in pygame as the texture for my floor
but when i try to actually display the image inside the pygame window, it comes up as this
for some reason there are random blobs of blue coming up in the image. i used this code to try and put in my image
width,height=pygame.display.get_surface().get_size()
floorimg=pygame.image.load("assets//image.png")
win.blit(floorimg,(0,height-100))
how can i fix this?
Seems like the problem is not the code but the image itself...
Attached is the link of the "floor" image with the transparent area colored using Windows Paint3D.
The code works perfectly fine, but you might need to get some clean pictures in the future.
I saved your image and wrote this script to draw it:
import pygame, sys
pygame.init()
window = pygame.display.set_mode((1300, 200), 0 , 32)
width,height=pygame.display.get_surface().get_size()
floorimg = pygame.image.load("floor.png")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
window.fill((0))
window.blit(floorimg,(0,height-100))
pygame.display.flip()
It seems to have similar problem to yours if the image is converted to pygame format(which I am assuming you do somewhere else in your code?)
floorimg = pygame.image.load("floor.png").convert()
But this can easily be solved by taking transparency into account while converting, which is done by convert_alpha.
floorimg = pygame.image.load("floor.png").convert_alpha()
This results in an image similar to the first one.

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))

How to use pygame.surface.scroll()?

I just found out about pygame.surface.scroll() and what I understand from the pygame documents that scroll() is for moving surface without the need to rebuild the background again to cover the old surface, just like pygame.rect.move_ip() but for surfaces.
Anyway, I don't know how to use it and the examples in the pygame documents are hard to me to understand as long as I am beginner and, after searching for long time, I couldn't found anything useful to understand how to use it.
Here is my code.
import pygame
from pygame.locals import*
screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
while True :
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT :
pygame.quit()
quit()
image.scroll(10,10)
screen.blit(image,(boxx,boxy))
pygame.display.update()
clock.tick(60)
EDIT: Your image and screen variables are backwards. That is also causing you some confusion I'm sure..
Your problem may is that you are trying to scroll an all black background. It is probably scrolling, and you just don't know it because the white box you used blit() to draw on the screen is stationary.
Try using something you can see scroll, like an image file. If you wanna move the white box, you can add a counter as a speed variable. Read this, then run it.
import pygame
from pygame.locals import*
screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
speed = 5 # larger values will move objects faster
while True :
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT :
pygame.quit()
quit()
image.scroll(10,10)
# I did modulus 720, the surface width, so it doesn't go off screen
screen.blit(image,((boxx + speed) % 720, (boxy + speed) % 720))
pygame.display.update()
clock.tick(60)
I can't say for sure the scroll function is working or not, learn to use an image as your background so you can see it moving first.

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()

Screen displays only in top left corner of window

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.

Categories