Hello, even though you might think there was a similar question, mine is pretty different than this.
I am trying to load an image from a directory, and set my screen size (automatically) to the size of the image being loaded as "background".
import pygame
import sys
from pygame.locals import *
image_resources = "C:/Users/user/Desktop/Pygame App/image_resources/"
class load:
def image(self, image):
self.image = image
return (image_resources + image)
def texture(self, texture):
self.texture = texture
return (image_resources + texture)
bg = load().image("bg_solid_black.jpg")
pygame.init()
#screen = pygame.display.set_mode((width,height),0,32)
#background = pygame.image.load(bg).convert()
#width = background.get_width()
#height = background.get_height()
The image that I loaded with my "load()" class is set to the variable "bg" and I want to use the size of whatever I load as "bg" to determine the size of the window. If you try to move
background = pygame.image.load(bg).convert()
width = background.get_width()
height = background.get_height()
On top of this:
screen = pygame.display.set_mode((width,height),0,32)
PyGame returns an error, in which it states that the display mode is not set. If I do it like this:
screen = pygame.display.set_mode((width,height),0,32)
background = pygame.image.load(bg).convert()
width = background.get_width()
height = background.get_height()
of course, this is not true, since variables "width" and "height" are not defined for the use of "pygame.display.set_mode()".
I can not seem to figure this out, I though of solving through an OO manner, but I just can not seem to figure it out. Any help?
Thanks :)
Before you use convert() on any surface, screen has to be initialized with set_mode().
You can load image and get its size before set_mode() but convert() has to be used after you initialize the display, like so:
import pygame
pygame.init()
image = pygame.image.load("file_to_load.jpg")
print(image.get_rect().size) # you can get size
screen = pygame.display.set_mode(image.get_rect().size, 0, 32)
image = image.convert() # now you can convert
The code I use is this:
import pygame
import os
import random
WIDTH = 750
HEIGHT = 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#Image Load
BG = pygame.image.load(os.path.join("assets", "background_space.png"))
Related
I want the window to be my resolution but still have the top bar appear. Just like you maximise a window.
This is what I have so far.
os.environ['SDL_VIDEO_CENTERED'] = '1'
display_info = pygame.display.Info()
self.width, self.height = display_info.current_w, display_info.current_h
self.screen = pygame.display.set_mode((self.width, self.height), pygame.NOFRAME)
but it seems to not work and only fullscreen the window to my resolution. What am I missing?
You can make the window to be in fullscreen mode using this:
pygame.display.set_mode(screen_size, FULLSCREEN)
If you have black borders around, you can make the window to fill the entire screen, leaving as few black borders as possible (depending of the relative screen and window ratio), by the use of the pygame 2.0.0 flag SCALED:
pygame.display.set_mode(screen_size, FULLSCREEN|SCALED)
If you still want to delete the remaining borders, rescale your window:
import pygame
pygame.init()
info = pygame.display.Info()
w = info.current_w
h = info.current_h
screen = pygame.display.set_mode((w, h), FULLSCREEN|SCALED)
I am using the easy-to-use Python library pgzero (which uses pygame internally) for programming games.
How can I make the game window full screen?
import pgzrun
TITLE = "Hello World"
WIDTH = 800
HEIGHT = 600
pgzrun.go()
Note: I am using the runtime helper lib pgzrun to make the game executable without an OS shell command... It implicitly imports the pgzero lib...
Edit: pgzero uses pygame internally, perhaps there is a change the window mode using the pygame API...
You can access the pygame surface which represents the game screen by screen.surface and you can change the surface in draw() by pygame.display.set_mode(). e.g.:
import pgzrun
import pygame
TITLE = "Hello World"
WIDTH = 800
HEIGHT = 600
def draw():
screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
pgzrun.go()
Or switch to fullscreen when the f key is pressed respectively return to window mode when the w key is pressed in the key down event (on_key_down):
import pgzrun
import pygame
TITLE = "Hello World"
WIDTH = 800
HEIGHT = 600
def on_key_down(key):
if key == keys.F:
screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
elif key == keys.W:
screen.surface = pygame.display.set_mode((WIDTH, HEIGHT))
pgzrun.go()
import pgzrun
import pygame
TITLE = "Hello World"
WIDTH = 800
HEIGHT = 600
def draw():
screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
pgzrun.go()
I am trying to make a game in python and pygame. I want to make a game with decent resolution, but I can't get higher resolution, any ways?
import pygame
import time
import random
import os
pygame.init()
height = 600
width = 800
window = pygame.display.set_mode((width,height))
white = 255,255,255
black = 0,0,0
red = 255,0,0
blue = 0,0,255
def update():
pygame.display.update()
def game():
stop_game = False
while not stop_game:
window.fill(white)
loaded_image = pygame.image.load("Player.png")
loaded_image = pygame.transform.scale(loaded_image,(150,150))
window.blit(loaded_image,(0,0))
update()
game()
print pygame.display.list_modes()
This will list the available display modes for you. Mine goes up to (2646, 1024).
So this program is supposed to just put a sprite on screen. But the image is not importing right. I made sure the image has no background, but in the program, part of the background turns black while the other half stays transparent. Its really weird.
heres my code:
from Tkinter import *
import pygame
from livewires import games
#Creating screen window
games.init(screen_width = 700, screen_height = 650, fps = 50)
#creating background image for the screen
screen_background = games.load_image('resized_stars background.png', transparent = False)
games.screen.background = screen_background
#Creating a sprite
spaceship_image = games.load_image('8-bit_Spaceship.png')
spaceship = games.Sprite(image = spaceship_image, x=350, y=235)
games.screen.add(spaceship)
#main loop at the end of the program just as in tkinter
games.screen.mainloop()
Why will it not show up on screen properly?
Here's what I use, and it works just fine:
First, create the screen:
screen = pygame.display.set_mode((screen width, screen height))
Then:
spaceship = pygame.image.load("direct it to the image")
An example of directing it to the image would be "C:/PythonFolder/spaceship.png"
I see you just put the name of the file.
Then, when you're ready to blit (append) it to the screen, use
screen.blit(spaceship, (x location, y location))
Then update it:
pygame.display.update()
or:
pygame.display.flip()
I have created a custom module for importing images and be able to resize it and easily animate them (basicly you say the frames that it have the animation, and it's split the image to the amount you say).
(I know that they are not perfect!)
Here is the module "load.py"
#IMPORT MODULES
try:
import pygame; pygame.init()
import os
except: pass
#------------------------------------------------------------------------------
#DEFAULT VALUES
Image_Frames = 1
Image_Size = (0,0)
#------------------------------------------------------------------------------
def image(name,frames=Image_Frames,size=Image_Size):
#Load Image
try:
img = pygame.image.load(name).convert_alpha()
except: return None
#Single / Multi Frame
if frames <= 1:
if not 0 in size: img = pygame.transform.scale(img,size)
return img
else:
#Set Variables
frame_y = 0
frames_list = []
frame_height = int(img.get_size()[1] / frames)
#Loop Frames
for frame in range(frames):
#Create a Surface and blit part of the image (Single Frame)
frame_surface = pygame.Surface((img.get_size()[0], frame_height),pygame.SRCALPHA)
frame_surface.blit(img,(0,0),(0,frame_y,img.get_size()[0],frame_height))
#If both values of size are not 0 scale it to size
if not 0 in size: img = pygame.transform.scale(frame_surface,size)
#Add it to list of Frames and prepare for next
frames_list.append(frame_surface)
frame_y += frame_height
#Return Frames
return frames_list
The main code (Not perfect, just simply to see the images):
import pygame; pygame.init()
Screen = pygame.display.set_mode((800,800))
import load
images = load.image("test.png",2,(400,300))
while True:
Screen.fill((255,0,255))
Screen.blit(images[0],(0,0))
Screen.blit(images[1],(0,400))
pygame.display.update()
And there is this a image that I use (Sorry I can't show images... :( )
https://www.dropbox.com/s/u7nghxntor2vbt1/test.png?dl=0
And this is the problem...
What's suppose to happen is that the images are separated into 2,
and the each image is scaled up to 400,300.
But then this happens when I run the code:
https://www.dropbox.com/s/idrsygynftb1oua/Sin%20t%C3%ADtulo.png?dl=0
The first one doesn't scale, and the second one makes something strange...
But that only happens when you do more than 1 'frames'.
If you change it to only do one image, it works fine (code example for main.py)
import pygame; pygame.init()
Screen = pygame.display.set_mode((800,800))
import load
images = load.image("test.png",1,(400,300))
while True:
Screen.fill((255,0,255))
Screen.blit(images,(0,0))
pygame.display.update()
Any fix? Ask anything you what if you need more info of me.
Actually fix!
Your code:
#If both values of size are not 0 scale it to size
if not 0 in size:
>> img << = pygame.transform.scale(frame_surface,size)
should be:
#If both values of size are not 0 scale it to size
if not 0 in size:
>> frame_surface << = pygame.transform.scale(frame_surface,size)