I am currently trying to display a death screen when the player dies in my game, but rather than it just popping up I want it to gradually becoming less and less transparent until the opacity is 256. I currently have this line of code below to display the image, but I was wondering how I would go about gradually increasing the opacity over a period of few seconds.
screen.blit(pygame.image.load("Images/dead.jpg"), (0, 0))
I've tried to add a for loop and tried to use convert_alpha, however I couldn't figure out a way of doing it, and any help will be greatly appreciated.
Here is what I would do. I have explained everything in the comments:
# Allow the image to have its alpha value changed
image = pygame.image.load("Images/dead.jpg").convert()
# Set the transparency to full
image_alpha = 0
# Decide how many frames you want the image to fade in over
fade_frame_number = 60
# And the frames per second
FPS = 30
FPS_Clock = pygame.time.Clock()
while True:
window.fill((255, 255, 255)) # Fill the window with a white bg
if image_alpha < 255:
image_alpha += 255 / fade_frame_number # Change the transparency variable
image.set_alpha(image_alpha) # Set the image's alpha value
window.blit(image, (0, 0)) # Display the image
pygame.display.update() # Update the screen
FPS_Clock.tick(FPS) # Wait for the next frame
In this, window is the display surface.
Related
Hello everyone I am learning the basics of pygame.
I recently ran into a problem;
I decided to load an image and gave it a random location well within the pygame window.But the problem was that sometimes it just wont appear in the window. So I drew a black pointer at the place where the image was to be loaded. Then I found out that the black pointer doesnot coincide with the image and hence the image is not appearing in the location I want it to be in.
So I want help solving this problem.
Thank you in advance.
Code:
import pygame
import random
pygame.init()
#Pygame starters
display_width = 800
display_height = 600
game_display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Blob runner")
clock = pygame.time.Clock()
targetpng = pygame.image.load("target.png")
#Quit checker
crashed = False
#COLOURS
white = (255, 255, 255)
black = (0, 0, 0)
def draw_environment(x,y):
game_display.fill(white)
#Image
game_display.blit(targetpng,(x, y))
#Black pointer
pygame.draw.circle(game_display, black, (x, y), 5 )
pygame.display.update()
x, y = random.randrange(0,display_width), random.randrange(0,display_height)
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
# print(event)
draw_environment(x,y)
clock.tick(30)
pygame.quit()
quit()
Image:
The image has a size. it may happen that the image is out of the window at the right or at the bottom. The origin of the blit puts the the top left corner of the image to the specified position.
If you wan that the image is centered at (x, y), the you have to get a pygame.Rect with the size of the image (get_rect) and set the center of the rectangle to the specified position (keyword argument). Use the rectangle to specify the position for the blit operation.
img_rect = targetpng.get_rect(center = (x, y))
game_display.blit(targetpng, img_rect)
Th image has a size, thus the random position for the image has to be in range [image_size/2, window_sizw - image_size/2].
The width and height of a pygame.Surface can be get by get_width()/get_height() or get_size(). For instance:
img_size = targetpng.get_size()
x = random.randrange(img_size[0]//2, display_width - img_size[0]//2)
y = random.randrange(img_size[1]//2, display_height - img_size[1]//2)
I now understand what went wrong with the insight provided by #Rabbid76.
The thing is I drew the picture in paint and then opened the file in gimp to convert whites in the image to alpha(the checkerboard-like background) . But the problem occurred when I did not clear of the unwanted alpha as I thought the computer will not recognise the alpha but as matter of fact it does.
So when I loaded the image, the corner of the image is not of the actual "target spot" but of the entire image, so the corner will be on the alpha. The black dot was pointing at the corner of the image all along and the image sometimes did not appear because the corner was too close to the border so the image would trespass to the un-displayed portion of the window.
So the fix is to clear of the alpha or the white background to minimum and only then convert it into alpha. Then do as #Rabbid76 says :D.
I'm using python 2.7, and I am currently working on a basic game in pyagme for my school work. I downloaded an image from google, and it has a background color and I know it's RGB. I remember that there is a method that makes the screen ignore this RGB for the picture (the picture will appear without the background color) but I don't remember it.
import pygame
WINDOW_WIDTH = 1200
WINDOW_LENGTH = 675
IMAGE = r'C:\Users\omero\Downloads\BattleField.jpg'
PISTOL_IMAGE = r'C:\Users\omero\Downloads\will.jpg'
RED = (255,0,0)
BLACK = (0,0,0)
WHITE = (255,255,255)
pygame.init()
size = (WINDOW_WIDTH, WINDOW_LENGTH)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
img = pygame.image.load(IMAGE)
screen.blit(img, (0,0))
pygame.display.flip()
pistol_img = pygame.image.load(PISTOL_IMAGE)
colorkey = pistol_img.get_at((0,0))
screen.blit(pistol_img,(50,50))
pygame.display.flip()
finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
pygame.quit()
When I run the above I get the background color with pistol_img, how do I make the screen ignore it without editing the image?
The white artifacts you can see in the picture are caused because the JPG format is a compressed format.
The compression is not lossless. This means the colors around the weapon are not exactly white (255, 255, 255). The color appear to be white for the human eye, but actually the color channels have a value lass than 255, but near to 255.
You can try to correct this manually. Ensure that format of the image has an alpha channel by pygame.Surface.convert_alpha(). Identify all the pixel, which have a red green and blue color channel above a certain threshold (e.g. 230). Change the color channels and the alpha channel of those pixels to (0, 0, 0, 0):
img = pygame.image.load(IMAGE).convert_alpha()
threshold = 230
for x in range(img.get_width()):
for y in range(img.get_height()):
color = img.get_at((x, y))
if color.r > threshold and color.g > threshold and color.b > threshold:
img.set_at((x, y), (0, 0, 0, 0))
Of course you are in danger to change pixels which you don't want to change to. If the weapon would have some very "bright" areas, then this areas my become transparent, too.
Note, an issue like this can be avoided by using a different image format like BMP or PNG.
With this formats the pixel can be stored lossless.
You can try to "photo shop" the image. Manually change the pixel around the weapon and store the image with a different format.
I want to be able to stretch a screen without needing to stretch each image individually.
I have a image I use as the screen, then when I have the final product I transform the image to the size of the screen and blit it to the screen.
But the images blit onto the main image do not transform with the main image, and comes out as its first size.
You can find the current version here:
https://gist.github.com/gideonjvisser/eca7506f483c51edf93c/
Here's the script:
import pygame
from pygame.locals import Color
chosen_resolution = (400,200)
screen = pygame.display.set_mode(chosen_resolution)
screen_rect = screen.get_rect()
in_game_resolution = (800,600)
screen_image = pygame.Surface(in_game_resolution)
screen_image_rect = screen_image.get_rect()
image_on_in_game = pygame.image.load("image.png").convert_alpha()
image_on_in_game = pygame.transform.scale(image_on_in_game, (469,669)) #to fit on in_game_resolution
print screen_rect
print screen_image_rect
while(True):
screen_image.fill(Color('white'))
screen_image.blit(image_on_in_game, (0,0))
screen_image = pygame.transform.scale(screen_image, chosen_resolution)
screen.blit(screen_image, (0,0))
pygame.display.flip()
This code almost works. The issue is that, in your while loop, you overwrite your static resolution intermediate surface with the scaled version. Try replacing the while loop with this:
while True:
screen_image.fill(Color('white'))
screen_image.blit(image_on_in_game, (0,0))
screen.blit(pygame.transform.scale(screen_image, chosen_resolution), (0,0))
pygame.display.flip()
Now the pre-scaled buffer should remain the right size and actually be scaled to fit the real screen's size.
I'm doing a graphics test using PyGame to simulate the Dragon Curve being unfolded. I already made one successful version that keeps track of all the points as they rotate around each other, but obviously, this begins to slow down pretty substantially after a few iterations. In order to speed this up, I want to simply store the drawn segments into an image variable, and continually save a segment of the screen to a variable and draw those moving rather than keeping track of a lot of points. How can I do either of the following?
Draw to an off-screen image variable that then gets drawn to the screen in the correct place
Save a section of the visible display into an image variable
I tried reading through some of the PyGame documentation, but I didn't have any success.
Thanks!
Creating an additional surface object, and drawing to it is the solution. This surface object can then be drawn onto the display's surface object as shown below.
More information on the PyGame Surface object can be found here
import pygame, sys
SCREEN_SIZE = (600, 400)
BG_COLOR = (0, 0, 0)
LINE_COLOR = (0, 255, 0)
pygame.init()
clock = pygame.time.Clock() # to keep the framerate down
image1 = pygame.Surface((50, 50))
image2 = pygame.Surface((50, 50))
image1.set_colorkey((0, 0, 0)) # The default background color is black
image2.set_colorkey((0, 0, 0)) # and I want drawings with transparency
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
screen.fill(BG_COLOR)
# Draw to two different images off-screen
pygame.draw.line(image1, LINE_COLOR, (0, 0), (49, 49))
pygame.draw.line(image2, LINE_COLOR, (49, 0), (0, 49))
# Optimize the images after they're drawn
image1.convert()
image2.convert()
# Get the area in the middle of the visible screen where our images would fit
draw_area = image1.get_rect().move(SCREEN_SIZE[0] / 2 - 25,
SCREEN_SIZE[1] / 2 - 25)
# Draw our two off-screen images to the visible screen
screen.blit(image1, draw_area)
screen.blit(image2, draw_area)
# Display changes to the visible screen
pygame.display.flip()
# Keep the window from closing as soon as it's finished drawing
# Close the window gracefully upon hitting the close button
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
clock.tick(30)
I have a program where I am using mouse controls to move objects. When the objects move, the program resets the screen to solid white so that there are no trails of the object using:
screen.fill(255,255,255)
What I am trying to do now is have the background a created background I made in a method.
black = (0,0,0)
brown = (218,134,10)
white = (255,255,255)
pygame.draw.rect(screen,white,(0,0,1000,600))
first =pygame.draw.rect(screen,brown,(150,150,50,400),0)
second = pygame.draw.rect(screen,brown,(450,150,50,400),0)
third = pygame.draw.rect(screen,brown,(750,150,50,400),0)
first_out = pygame.draw.rect(screen,black,(150,150,50,400),2)
second_out = pygame.draw.rect(screen,black,(450,150,50,400),2)
third_out= pygame.draw.rect(screen,black,(750,150,50,400),2)
How can I make my background the background that refreshes every time an object is moved?
Rather than drawing directly on the screen surface, you should create a new surface, let's call it background_surface.
By doing so, you will only have to draw the background once (at the very beginning of your application) and any blitting afterwards will be done a lot quicker, since no pygame.draw.xxx is being done.
To fill the screen surface with background_surface, you'll just use screen.blit().
A quick snippet:
WIDTH, HEIGHT = 800, 600
background_surface = pygame.Surface((WIDTH, HEIGHT))
# do all the drawings on the background surface
background_surface.fill((255, 255, 255))
while application_running:
# processinput
# update objects
# draw everything
screen.blit(background_surface, (0, 0))
pygame.display.update()