Image import issue with pygame - python

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

Related

How can I make the picture run perfectly on the screen?

I want to make the background perfect for the running screen, but the vertical axis of the picture doesn't stretch even if I keep changing it in the code.
How can I get it right?
import pygame
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 600
pygame.init()
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("pygame test")
screen= pygame.image.load("그림.png")
SCREEN.blit(screen,(0,100))
SCREEN.blit(player, player_Rect)
pygame.display.flip()
if i add screen=pygame.transform.scale(500,600) this code
print black screen
pygame.transform.scale() takes two arguments, the source surface and a tuple with the size of the new and scaled surface:
screen=pygame.transform.scale(500,600)
screen = pygame.transform.scale(screen, (500, 600))
Scale the surface immediately after loading it:
screen = pygame.transform.scale(pygame.image.load("그림.png").convert_alpha(), (500, 600))

How open a GIF image in with turtle?

I tried to animate my GIF image but it does not work, this is my code :
import turtle
screen = turtle.Screen()
t1 = turtle.Turtle()
screen.addshape('black.gif')
t1.shape('black.gif')
turtle.done()
Make sure that your python code and your gif is in the same folder and then try this code:
import turtle
screen = turtle.Screen()
screen.setup(500, 500) # You can change the dimensions(500, 500) to any number
t = turtle.Turlte()
screen.addshape("name_of_the_gif.gif") #don't forget they have to be in the same file
t.shape("name_of_the_gif.gif")# make the same gif as the screen.addshape

pygame not displaying my image

i started learning pygame and i followed some tutorials to make simple hello world project and it works but when i do it my self trying to display my image on the window nothing happen!
this is my code
__author__ = 'mohammed'
import sys
import pygame
import color
# -----------setup------------------------
pygame.init() # start pygame
screensize = (800, 600) # variable that we will use to declare screen size
screen = pygame.display.set_mode(screensize) # set the screen size
pad = pygame.image.load('2.png')
x = 100
y = 100
# -----------setup------------------------
# -------------------main loop------------
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(pad, (x, y))
screen.fill(red)
pygame.display.update()
i am importing my file that contain colors and their rgb :
red = (255, 0, 0)
It looks like you're filling the screen after the image is drawn, covering the image. Try switching the order of the rows:
screen.blit(pad, (x, y))
and
screen.fill(red)

How to detect resizeable window state in pygame and de-maximize it in Linux?

I have an application built in python with use of pygame that initially displays a login screen at a set size which is not RESIZABLE and then when user logs into the game the saved settings are being used to transform the window size. The window is turned into RESIZABLE after login. If user logs out the window is changed back to the initial size without RESIZABLE flag. Everything is OK as long as the user logs out from normal window, but when user hits the maximize button and then logs out in some distros the window still stays maximized and the login screen is being painted in a top left corner of the window.
And here comes the question, is there a way of detecting whether the window has been maximized so I can de-maximize it before sizing down?
I couldn't find anything that would help me with this in the pygame docs or anywhere online. I have found a way of getting a "handle" to the window by using:
pygame.display.get_wm_info()['window']
but not sure where to take it from here.
The way I set the sizes:
self.screen = pygame.display.set_mode((800, 480)) #login screen
self.screen = pygame.display.set_mode(user_saved_size, pygame.RESIZABLE) #game screen
get_wm_info()['wmwindow'] gives you windowID in Windows Manager (X.org) but it is "outside" of PyGame. Maybe with python library Xlib you could do something.
EDIT:
I tried example in Setting the window dimensions of a running application to change terminal size and it works but it don't change PyGame window size. I tried xlib to get PyGame window caption and it works but I could not set PyGame window caption.It seems PyGame doesn't respect new caption.
I use this code to test PyGame window caption - it can get caption but it can't set caption.
import sys
import pygame
from pygame.locals import *
import Xlib
import Xlib.display
WIDTH, HEIGHT = 1500, 300
pygame.init()
screen = pygame.display.set_mode((800,600),0,32)
print "wm_info:", pygame.display.get_wm_info()
print " window:", pygame.display.get_wm_info()['window']
print "fswindow:", pygame.display.get_wm_info()['fswindow']
print "wmwindow:", pygame.display.get_wm_info()['fswindow']
display = Xlib.display.Display()
root = display.screen().root
#windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
#print "Xlib windowID:", windowID
#window = display.create_resource_object('window', windowID)
window = display.create_resource_object('window', pygame.display.get_wm_info()['window'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib window get_wm_name():", window.get_wm_name()
window = display.create_resource_object('window', pygame.display.get_wm_info()['fswindow'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib fswindow get_wm_name():", window.get_wm_name()
window = display.create_resource_object('window', pygame.display.get_wm_info()['wmwindow'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib wmwindow get_wm_name():", window.get_wm_name()
print
print "Xlib wmwindow set_wm_name(hello world of xlib)"
window.set_wm_name("hello world of xlib")
display.sync()
print "Xlib wmwindow get_wm_name():", window.get_wm_name()
# --------------
fpsClock = pygame.time.Clock()
RUNNING = True
while RUNNING:
for event in pygame.event.get():
if event.type==QUIT:
RUNNING = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
RUNNING = False
fpsClock.tick(25)
# --------------
pygame.quit()
sys.exit()
I use this code to change window size - it works in terminal and DreamPie (python shell):
# https://unix.stackexchange.com/questions/5999/setting-the-window-dimensions-of-a-running-application
WIDTH, HEIGHT = 1500, 300
import Xlib
import Xlib.display
display = Xlib.display.Display()
root = display.screen().root
windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
window = display.create_resource_object('window', windowID)
window.configure(width = WIDTH, height = HEIGHT)
display.sync()
#windowIDs = root.get_full_property(display.intern_atom('_NET_CLIENT_LIST'), Xlib.X.AnyPropertyType).value
#for windowID in windowIDs:
# window = display.create_resource_object('window', windowID)
# name = window.get_wm_name() # Title
# pid = window.get_full_property(display.intern_atom('_NET_WM_PID'), Xlib.X.AnyPropertyType) # PID

PyGame - Getting the size of a loaded image

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

Categories