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)
Related
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.
Like for example my game screen is 600 by 600, and my image is 600 by 1500. How can I make it so that my background moves to another part of the image when my sprite goes off screen, so it seems like I'm moving through but I'm really not?
Sorry if I seem confusing.
Thanks.
From the pygame documentation on blit, you can pass in an area of your background image that you want to draw.
My pygame is a bit rusty, but you should be able to do the following:
blit(background_image, destination_surface, ((left_x_offset, 0), (right_x_offset, 0))
The first argument is your background image (the 600 x 1500 one), the second argument is the surface you're drawing everything to, and the third argument is the area of your background image that you want to draw to the destination surface.
Since you want to move the background in relation to the character, I would set left_x_offset to the position of the character minus half the width of your screen, and right_x_offset to the position of the character plus half the screen size.
I have been working on a game using the pygame module for python and am having an issue getting it to display properly on other computers. When I first starting writing I chose a resolution of (1400,1000). There was no real reason for this choice, I was/am new to programming and it seemed like a nice round number. The issue I am having is that on many laptops the screen is bigger than the max resolution. I can run it in windowed mode but then the screen runs off to the right and the bottom and there are things that the user cannot see. I then tried to run it in full screen mode, but then I get the error message "pygame.error No video mode large enough for 1400 , 1000"
The most obvious solution is to reduce my screen resolution I suppose, but all my GUI menu buttons are blitted to the screen assumming a resolution of (1400,1000) and changing everything would be extremely tedious.
The most curious thing happens when I connect a USB moniter with a resolution high enough to display it properly; The game will display properly on the connected moniter, and will also resize itself to display properly on the laptop moniter as well!. I can then disconnect the other moniter and go about playing the game no problem!
If I could somehow "trick" the computer into thinking there is a moniter connected with a proper resolution everything would work perfect. Not a perfect or elegant solution I admit, but I loathe having to go back and redo my entire GUI...
size = (1400, 1000)
screen = pygame.display.set_mode(size,pygame.FULLSCREEN)
Read up on pygame.org/docs
pygame.display.set_mode() will use your current screen resolution automatically, no matter what computer you are on as long as you don't pass in any parameters.
As to why 1400 by 1000 wasn't working for you, that is not a common screen resolution (not even sure if it exists). Some common ones include:
1280 by 720
1600 by 900
1920 by 1200
"The most obvious solution is to reduce my screen resolution I suppose, but all my GUI menu buttons are blitted to the screen assumming a resolution of (1400,1000) and changing everything would be extremely tedious."
A way to avoid this issue is to work in terms of your screen area. For example, lets say I work in a resultion of 1280 by 800.
width = 1280
height = 800
screen = pygame.display.set_mode([width, height])
I want my position to be in the middle of the screen. Instead of saying
position = 1280 / 2 # Hardcoded
do
position = width/2
Now no matter what you change your screen width to, your position will be preserved. I recommend looking into http://www.pygame.org/docs/ref/rect.html for information about how to track locations in pygame in a nice way.
About the flags: I don't know much about these so I won't say much, other than that I never use them and don't have any issues doing anything.
I hope this helps
```
from win32api import GetSystemMetrics
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
size = (width, height)
```
I would try to incorporate this into into your code
I am working on a Tank game as a school project and I am trying to make it as user-friendly as possible (with things like customizable keybindings, display settings). However, I don't want the game window to lose it proportions so I figured I would add borders to the sides of the game window in order to support wide screens.
I've attached an imageto illustrate what I'm looking for:
So for this to work, I would need a way to make "screen layers". The base layer is the whole screen with some graphics on the sides added to it and a font displaying score. And then a second layer would be rendered in the middle of the screen which will be the game window and its width and height will be obviously smaller than the base layer.
At the moment, the game is rendered from the top left corner of the screen until all the tiles have been rendered out. So that is why I figured it would be the best to make the tiles render out on a separate surface positioned in the middle of the screen surface.
Would really appreciate some tips on how I can make this possible! :) I don't have that much experience in Classes and pygame, so it would be awesome if you could give a fleshed out description as well! :D
Just draw your different stuff on different surfaces.
For example, create a Surface for your game window (let's call it game_surf), and draw your tanks and bullets etc. on that new Surface instead of the pygame main window (which is just another Surface).
# the main window
screen = pygame.display.init()
# just a regular surface
game_surf = pygame.surface.Surface(width, height)
...
# in your main loop
# or however you draw your sprites
your_sprite_group.draw(game_surf)
Then blit it onto the screen
screen.blit(game_surf, the_position)
Simplest way: Create a group for each 'layer'.
import pygame
front_sprites = pygame.sprite.Group()
middle_sprites = pygame.sprite.Group()
background_sprites = pygame.sprite.Group()
Then, just draw them all onto the screen in the following order:
background_sprites.draw(screen)
middle_sprites.draw(screen)
front_sprites.draw(screen)
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