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

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

Related

Why is my Python turtle screen asymmetrical?

I created a pong game where I noticed the paddles are not placed equally at the screen edges.
I created an 800 pixel wide screen, and placed paddles at xcor = 380 and xcor = -380 but on the screen left paddle shows some gap but right paddle doesn't. Is my screen unsymmetrical? How do I fix it?
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("PONG")
screen.tracer(0)
l_paddle = Paddle()
l_paddle.create_paddle((-380, 0))
r_paddle = Paddle()
r_paddle.create_paddle((380, 0))
screenshot of screen
When we specify a window size to setup(), we're talking about total pixels used on the screen. Since there is chrome around the window (edges, title bar, etc.) the actual area we have to work with is slightly smaller. Trying to place two turtles at exactly the left and right edge, ignoring chrome:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
WIDTH, HEIGHT = 600, 400
screen = Screen()
screen.setup(WIDTH, HEIGHT)
l_paddle = Turtle('square')
l_paddle.fillcolor('white')
l_paddle.setx(CURSOR_SIZE/2 - WIDTH/2)
r_paddle = Turtle('square')
r_paddle.fillcolor('white')
r_paddle.setx(WIDTH/2 - CURSOR_SIZE/2)
screen.exitonclick()
We get a result similar to yours:
If we compensate for the internal and external chrome elements:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
BORDER_SIZE = 2 # inside the window
CHROME_SIZE = 9 # around the window
WIDTH, HEIGHT = 600, 400
screen = Screen()
screen.setup(WIDTH, HEIGHT)
l_paddle = Turtle('square')
l_paddle.fillcolor('white')
l_paddle.setx(CURSOR_SIZE/2 - WIDTH/2 + BORDER_SIZE)
r_paddle = Turtle('square')
r_paddle.fillcolor('white')
r_paddle.setx(WIDTH/2 - CURSOR_SIZE/2 - BORDER_SIZE - CHROME_SIZE)
screen.exitonclick()
We can get a more precise result:
The problem here is that the amount of chrome is system dependent but turtle doesn't tell us how much to compensate. You might be able to find out from the underlying tkinter code.
My recommendation is you estimate the best you can, assume it's not accurate on all systems, and stay away from the edges so it's less of an issue. The error can be greater in the Y dimension when the title bar is part of the chrome.

Is there a way to make a windowed fullscreen mode in pygame?

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)

Why does this pygame program freeze?

Below is a program using pygame which updates the histogram as values change.
However after a few seconds of running, the program freezes. Can someone point out the error?
import random
import pygame
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
FRAME_RATE = 50
BACKGROUND_COLOR = pygame.Color("white")
BAR_COLOR = pygame.Color("Black")
BUCKET_CNT = 20
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
screen.fill(BACKGROUND_COLOR)
buckets = BUCKET_CNT*[0]
bar_w = SCREEN_WIDTH / BUCKET_CNT
clock = pygame.time.Clock()
background = pygame.Surface(screen.get_size())
background.fill(BACKGROUND_COLOR)
while True:
clock.tick(FRAME_RATE)
random.seed()
idx = random.randrange(BUCKET_CNT)
buckets[idx] += 1
# Create rectangles representing bars in the histogram.
bars = [pygame.Rect(i*bar_w,
SCREEN_HEIGHT - buckets[i],
bar_w, buckets[i]) for i in range(BUCKET_CNT)]
# Draw bars on the background
[pygame.draw.rect(background, BAR_COLOR, b, 5) for b in bars]
# Blit the background
screen.blit(background, (0, 0))
# Show "stuff" on the screen
pygame.display.flip()
EDIT
These are very good suggestions. I've changed my code using to follow them, however the code still freezes. Here is how the code looks now:
import random
import pygame
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
FRAME_RATE = 50
BACKGROUND_COLOR = pygame.Color("white")
BAR_COLOR = pygame.Color("Black")
BUCKET_CNT = 20
GROWTH_RATE = 10
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
screen.fill(BACKGROUND_COLOR)
buckets = BUCKET_CNT*[0]
bar_w = SCREEN_WIDTH / BUCKET_CNT
clock = pygame.time.Clock()
background = pygame.Surface(screen.get_size())
background.fill(BACKGROUND_COLOR)
# Create rectangles representing bars in the histogram.
bars = [pygame.Rect(i*bar_w,
SCREEN_HEIGHT - buckets[i],
bar_w, buckets[i]) for i in range(BUCKET_CNT)]
random.seed()
while True:
clock.tick(FRAME_RATE)
idx = random.randrange(BUCKET_CNT)
buckets[idx] += 1
bars[idx].inflate_ip(0, GROWTH_RATE)
# Draw bars on the background
pygame.draw.rect(background, BAR_COLOR, bars[idx])
# Blit the background
screen.blit(background, (0, 0))
# Show "stuff" on the screen
pygame.display.flip()
I want to apologize for taking everybody's time. As it turns out there is no issue with the code. My work machine was simply overloaded with various processes. It is running a multi-threaded test and a virtual machine (which is currently compiling a very large code base). That all explains why my program was freezing. Thank you DJMcMayhem for trying out the code. A special shout out to Alex Van Leiw. Thanks to you I learned a few new things about pygame today.

Image import issue with pygame

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

How to put limits on resizing a window in pygame

I have a window in pygame set up like this:
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.RESIZABLE)
As you can see, it is resizable, and that aspect is working perfectly, but if it is too small, then you can not see everything, and so I would like to set up a limit, of for example, you can not resize the screen to have a width os less then 600, or a height of less then 400, is there a way to do that in pygame?
Thank you!
You can use the pygame.VIDEORESIZE event to check the new windows size on a resize.
What you do is on the event, you check the new windows size values, correct them according to your limits and then recreate the screen object with those values.
Here is a basic script:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,480), HWSURFACE|DOUBLEBUF|RESIZABLE)
while True:
pygame.event.pump()
event = pygame.event.wait()
if event.type == QUIT: pygame.display.quit()
else if event.type == VIDEORESIZE:
width, height = event.size
if width < 600:
width = 600
if height < 400:
height = 400
screen = pygame.display.set_mode((width,height), HWSURFACE|DOUBLEBUF|RESIZABLE)
EDIT: Depending on how your game graphics are drawn, you may want to resize them according to the windows resize (haven't tested that, just going after this example: http://www.pygame.org/wiki/WindowResizing)

Categories