When i try to blit an image in pygame the image blits smaller than the actual image, my simplified code is
import pygame
pygame.init()
screen = pygame.display.set_mode((1176,674),0,32)
background = pygame.image.load("picture.jpg").convert()
while True:
screen.blit(background, (0,0))
pygame.display.flip()
The resulting image is about half the size it should be
It is entirely possible that when you view it you have zoomed in. Or there could be formatting irregularities. Either way, there are two simple solutions. Solution number 1 is to just scale up the image in an image editor, if you don't have one, GIMP is free online. A better way is to scale it in the code. You can just double the size of an image you loaded with this modification to your code, solving your problem. Use pygame.transform:
import pygame
pygame.init()
screen = pygame.display.set_mode((1176,674),0,32)
background = pygame.image.load("start_screen.jpg").convert()
background = pygame.transform.scale(background, (1176,674))
while True:
screen.blit(background, (0,0))
pygame.display.flip()
this should work
I found that the issue came from my MacBooks retina screen resolution, once i downloaded a program to adjust the resolution the program worked properly
Related
im writing a game right now with 32x32 textures, i have a problem because my screen window is too small, is there any solution? Can i "zoom" my whole game?
Now it looks like this and exactly what i want is to make whole screen bigger and zoom to see only like 5% of map.
Of course, if i make my screen bigger it not gonna fix my problem.
Maybe i should add another camera or something?
I think I made myself quite clear.
Thanks!
First, you can definitely zoom as you are saying.
screen.blit(pygame.transform.scale(display, (int(width), int(height)))
You can resize the screen:
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.RESIZABLE)
def resize(event):
global WINDOWWIDTH, WINDOWHEIGHT
if event.type == pygame.VIDEORESIZE:
WINDOWWIDTH = event.size[0]
WINDOWHEIGHT = event.size[1]
return pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), pygame.RESIZABLE)
And have the display the actual size you want to draw to:
display = pygame.Surface((300,200))
However, scaling surfaces is really bad as pixel art isn't great at scaling. An alternative would be a camera or switching off of pygame which would make everything you are trying to do way less complicated. I would recommend rubato for a start. It will do all this work for you, and you can decide how big you want your actual grid (display) to be with one variable.
Good luck! Feel free to comment if you want any extra help.
One way you can fix this is by shrinking the size of the screen itself:
#you can adjust the width and height in game until it seems the right fit
screen=pygame.display.set_mode((WIDTH,HEIGHT))
And if you want, you can set the window to Fullscreen:
screen=pygame.display.set_mode((WIDTH,HEIGHT),pygame.FULLSCREEN)
Hello fellow programmers,
I wrote a little python program which is use to launch random games on a retrograming distribution, and I use pygame to display the image of the game before launching it
I use a background and my issue is that the background image is clean but when displaying it and the cover of the game over it, it appears with a strange ugly gradient effect as you can see there : https://imgur.com/a/BnNdoqn
It appears mostly in the corner and the cover itself is entirely unaffected.
Here is my pygame code displaying both images :
log('showPic %s' %file)
# INITS
pygame.init()
pygame.mouse.set_visible(0)
backgroundPicture = pygame.image.load(backgroundFile)
picture = pygame.image.load(file)
# # CREATE FULLSCREEN DISPLAY. X = 1920- Y = 1080
fullscreen = pygame.display.set_mode((1920,1080), FULLSCREEN)
fullscreen.blit(backgroundPicture, (0,0))
# # PASTE PICTURE ON FULLSCREEN
x = (1920 - picture.get_width()) /2
y = (1080 - picture.get_height()) /2
fullscreen.blit(picture, (x,y))
# # SHOW FULLSCREEN
pygame.display.flip()
# # WAIT 5 SECONDS (need import time)
time.sleep(5)
# # EXIT PYGAME (Not needed but recommanded)
pygame.display.quit()
pygame.quit()
backgroundPicture is the background image and picture is the cover of the game, I combined the too like it appears in second capture.
So mainly I don't know much at all about display, images, graphical libraries and all that.
I think that this might be related to transparency or alpha layer or compression format of the image but I have no knowledge at all about that either.
The code is launched on a raspberry pi with a linux distribution, don't know much more about it.
Also strangely, one of my users said the strange gradient effect seems to disappear after ten or so launches of the script, but I couldn't reproduce that.
So what am I missing to get rid of that ugly effect ?
Here is the background image here if its characteristic might be related to the problem :
Thank you for your help !
The effect you are seeing is called "banding", see Wikipedia article. It is caused by not having enough bit-depth to represent fine gradations of colour and is most noticeable in large, untextured areas.
There are not many things you can do about it. Your options are basically:
to go to a 16-bit setup instead of 8-bit, if pygame can do that, or
add a small amount of random noise, or dithering to break it up.
I am currently trying to develop a computer game with pygame - and I'd like to display a background image for the game (2D-jump-and-run).
However I noticed a significant frame-rate drop when displaying a full-screen background image, above a certain window-size:
Windowsize | Time to blit the image | (~ FPS)
1280x720: 0.029s (~32 FPS)
1366x768: 0.031s (~29 FPS)
1600x900: 0.042s (~22 FPS)
1920x1080: 0.062s (~15 FPS)
Is there a different, more effective way of blitting the image? I wouldn't want to restrict the game to such small window sizes as 1366x768 and lower...
I tried using an image which was smaller concerning the KB-size, as well as trying to blit 4 images with 1/4 the size to cover the screen, but the times remains the same.
The game runs on the pygame screen, so I can't use any functions of e.g. Tkinter (as they would need a Tkinter surface).
Currently im using the following function of pygame:
# Once at the beginning
screen = pygame.display.set_mode((screenWidth_X, screenHeight_Y))
# Every frame
screen.blit(background_Image, (0,0))
Any help/suggestions would be greatly appreciated!
Per Ted Klein Bergman's comment, using .convert() and .convert_alphais a huuuuge performance boost.
# From originally:
1920x1080: 0.062s (~15 FPS)
# To:
1920x1080: 0.002s (~140 FPS)
I want to darken current screen a bit before poping up a new surface. I know there is no function in pygame to do this and that I will have to process the image to darken it. But as long as I know the only way to get current displaying surface in pygame is by saving it to disk as a file which slows down the game. Is there any other way to do this with pygame? Like saving the image to a value in memory so that I can process it without saving it somewhere.
Thanks in advance,
Stam
You don't need to save anything to a file.
When you read an image to a file, it is a Surface object. You them blit this object to the screen. But these Surface objects have the same methods and properties than the object working as the screen - (which is also a Surface): you can draw primitives, and blit other images to them - all in memory.
So, once you read your image, just make a copy of it, draw a filled rectangle with a solid transparent color on it to darken it, and then blit it to the screen. Repeat the process increasing the transparency level and pasting it on the screen again if you want a fade in effect.
import pygame
screen = pygame.display.set_mode((640,480))
img = pygame.image.load("MYIMAGE.PNG")
for opacity in range(255, 0, -15):
work_img = img.copy()
pygame.draw.rect(work_img, (255,0, 0, opacity), (0,0, 640,480))
screen.blit(work_img, (0,0))
pygame.display.flip()
pygame.time.delay(100)
I want to create a bunch of simple geometric shapes (colored rectangles, triangles, squares ...) using pygame and then later analyze their relations and features. I first tried turtle but apparently that is only a graphing library and cannot keep track of the shapes it creates and I wonder if the same holds true for Pygame. To illustrate the point, say I have this script:
# Import a library of functions called 'pygame'
import pygame
from math import pi
# Initialize the game engine
pygame.init()
# Define the colors we will use in RGB format
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
# Set the height and width of the screen
size = [800, 600]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Example code for the draw module")
#Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()
while not done:
# This limits the while loop to a max of 10 times per second.
# Leave this out and we will use all CPU we can.
clock.tick(10)
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
screen.fill(WHITE)
# Draw a rectangle outline
pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)
# Draw a solid rectangle
pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])
# Draw an ellipse outline, using a rectangle as the outside boundaries
pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2)
# Draw a circle
pygame.draw.circle(screen, BLUE, [60, 250], 40)
# Go ahead and update the screen with what we've drawn.
# This MUST happen after all the other drawing commands.
pygame.display.flip()
# Be IDLE friendly
pygame.quit()
It creates this image:
Now, suppose I save the image created by Pygame. Is there a way Pygame would be able to detect the shapes, colors and coordinates from the image?
PyGame is a gaming library - it helps with making graphics and audio and controllers for games. It doesn't have support to detect objects in a preexisting image.
What you want is OpenCV (It has Python bindings) - this is made to "understand" things about an image.
One popular math algorithm used to detect shapes (or edges) of any sort are Hough Transforms. You can read more about it here - http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html
OpenCV has Hough transform functions inside it which are very useful.
You could attempt to make your own Hough transform code and use it ... but libraries make it easier.
Yes, It can, but pygame is also good for making games but unfortunately, you can't convert them to IOS or Android, in the past, there was a program called PGS4A which allowed you to convert pygame projects to android but sadly, the program has been discontinued and now, there is no way. On this case, my sggestion would be that if you ever wanted to do this, download Android Studio from this link "http://developer.android.com/sdk/index.html#top" and google on how to use libgdx with Android Studio, this guy has an extremely helpful tutorial which has a lot of parts, but if your goal is to make commercial applications, I would highly recommend you to check this tutorial "https://www.youtube.com/watch?v=_pwJv1QRSPM" extremely helpful. Good luck with your goals and hoped this helped you on making your decision, but python is a good programming language, it will give you the basic idea on how programming is.