image not displaying nicely in pygame - python

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.

Related

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.

Make the background of an image transparent in Pygame with convert_alpha

I am trying to make the background of an image transparent in a Pygame script. Now the background in my game is black, instead of transparent. I read somewhere else that I could use convert_alpha, but it doesn't seem to work.
Here is (the relevant part of) my code:
import PIL
gameDisplay = pygame.display.set_mode((display_width, display_height))
img = pygame.image.load('snakehead1.bmp').convert_alpha(gameDisplay)
What am I doing wrong? Thanks in advance!
To make an image transparent you first need an image with alpha values. Be sure that it meets this criteria! I noticed that my images doesn't save the alpha values when saving as bmp. Apparently, the bmp format do not have native transparency support.
If it does contain alpha you should be able to use the method convert_alpha() to return a Surface with per-pixel alpha. You don't need to pass anything to this method; if no arguments are given the new surface will be optimized for blitting to the current display.
Here's an example code demonstrating this:
import pygame
pygame.init()
screen = pygame.display.set_mode((100, 100))
image = pygame.image.load("temp.png").convert_alpha()
while True:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
screen.blit(image, (0, 0))
pygame.display.update()
And my image ("temp.png"):
If it doesn't contain alpha there are two easy fixes.
Save the image with a different file format, like png.
Use colorkeys. This works if you only need to remove the background. It is as easy as putting the line of code image.set_colorkey((0, 0, 0)), which will make all black colors transparent.

PyGame garbles horizontal pixel line at image load

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

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.

Using Surface.copy() sometimes loses transparency

For some (but not all!) images, copying a surface using surface.copy() loses the transparency. So I've got two questions?
Why does copy lose the transparency? The docs sound like everything about the new surface should be the same, but that's obviously not happening.
Why does this happen with some images and not others?
Here is an example "bad" image -- when copied, the transparency is lost
Here is an example "good" image -- when copied, the transparency is not lost.
And here is the code that you can run to see the difference:
import pygame
def test():
screen = pygame.display.set_mode((320, 240))
bad_original = pygame.image.load('bad-image.gif')
bad_copied = bad_original.copy()
good_original = pygame.image.load('good-image.gif')
good_copied = good_original.copy()
while True:
for event in pygame.event.get():
if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE or
event.type == pygame.QUIT):
pygame.quit()
screen.fill((150, 150, 150))
screen.blit(bad_original, (0,0))
screen.blit(bad_copied, (100, 0))
screen.blit(good_original, (0,100))
screen.blit(good_copied, (100, 100))
pygame.display.flip()
if __name__ == '__main__':
test()
And heck, for completion, here's what a screenshot of running the above code looks like.
Please note that I'm not looking for workarounds; I just want to know what I am not understanding about surface.copy, or anything you think I may not understand about working with Pygame surfaces.
I'm using Python 3.3 and Pygame 1.9.2pre on a Windows 7 machine.
You need to use .convert_alpha()
Try:
pygame.image.load('my_image.gif').convert_alpha()
See:
http://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert_alpha
"Creates a new copy of the surface with the desired pixel format. The new surface will be in a format suited for quick blitting to the given format with per pixel alpha. If no surface is given, the new surface will be optimized for blitting to the current display.
Unlike the Surface.convert() method, the pixel format for the new image will not be exactly the same as the requested source, but it will be optimized for fast alpha blitting to the destination."
In pygame anytime you load and image, or create a surface, with the intent of displaying it you should .convert() it if it has no transparency, or .convert_alpha() it if it has transparency. This yields both a big speedup AND solves the mystery of, 'Why is my transparency doing that?'.

Categories