How do I make images appear/disappear in pygame?
Generally there's two ways of doing this.
The more common way is to re-paint the entire screen on each iteration of the main loop.
For example:
### Main Loop
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.MOUSEBUTTONUP ):
mouse_position = pygame.mouse.get_pos() # Location of mouse-click
player_moves.append ( PlayerMove( mouse_position ) ) # Make a new move
# Re-paint the screen
window.fill( OCEAN_BLUE_COLOUR ) # clear the screen
# Paint each of the players turns
for m in player_moves:
m.draw( window ) # paints a hit or miss icon
pygame.display.flip()
Alternatively, instead of re-painting everything, only change the items that have updated, or when events happen. This is close to the "dirty-rectangles" method of updating.
# Initially paint the screen
window.fill( OCEAN_BLUE_COLOUR ) # clear the screen
### Main Loop
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.MOUSEBUTTONUP ):
mouse_position = pygame.mouse.get_pos() # Location of mouse-click
move = playerMove( mouse_position )
move.draw( window )
pygame.display.flip()
The difficulty of the second method, is that the program needs to clean up after the movement of on-screen images (otherwise they will leave a trail). Obviously in a battleship game, no on-screen elements move - but things like re-drawing scores or starting a new game will need to somehow erase the background. I'm not sure if this will also re-paint the window after it has been occluded by another window.
If you are a beginner programmer, I would use the first method. It's much simpler, and a lot of games are written this way.
screen.blit(image) in pygame, image disappears
You need to define the NoteList
outside of the while loop if the notes should persist, otherwise you create a new empty list each iteration.
NoteList = []
going = True
while going:
for e in pygame.event.get():
if e.type == pygame.QUIT:
going = False
elif e.type == pygame.midi.MIDIIN:
print(str(e.data1))
NoteList.append(int(e.data1-20))
screen.fill(white)
for h in NoteList:
screen.blit(EthnoteIMG, (int(h), 100))
pygame.display.update()
You can use the enumerate
function to shift the position:
for g, h in enumerate(NoteList):
screen.blit(EthnoteIMG, (g*12, int(h)))
Make images appear randomly and disappear after certain time
use variables with time of start showing
and end showing
.
It is popular method to control different elements in game.
import pygame
import random
# - init -
pygame.init()
screen = pygame.display.set_mode((800, 600))
# - objects -
display_apple = False
start_showing = None
end_showing = None
current_time = pygame.time.get_ticks()
# show first time
start_showing = current_time + random.randint(1,5)*1000
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# - updates -
current_time = pygame.time.get_ticks()
if display_apple:
# is it time to hide ?
if end_showing and current_time >= end_showing:
# hide it
display_apple = False
end_showinge = False
# set time when to show
start_showing = current_time + random.randint(1,5)*1000
else:
# is it time to show ?
if start_showing and current_time >= start_showing:
# show it
display_apple = True
start_showing = False
# set time when to hide
end_showing = current_time + random.randint(1,5)*1000
# - draws -
screen.fill((0,0,0))
if display_apple:
pygame.draw.rect(screen, (255,0,0), (0,0,100,100))
pygame.display.flip()
# - FPS -
clock.tick(30)
# - end -
pygame.quit()
Related Topics
How to Determine the Language of a Piece of Text
Find First Element in a Sequence That Matches a Predicate
How to Start and Stop a Thread
How to Convert a Time.Struct_Time Object into a Datetime Object
When Would the -E, --Editable Option Be Useful with Pip Install
How to Convert a String Date into Datetime Format in Python
Python: Can Executable Zip Files Include Data Files
Launch a Completely Independent Process
Splitting a List Based on a Delimiter Word
How to Convert a Numpy Array to (And Display) an Image
Spark Dataframe Distinguish Columns with Duplicated Name
How to Redirect Stdout and Stderr to Logger in Python
Is It Safe to Replace a Self Object by Another Object of the Same Type in a Method
How to Set Up a Virtual Environment for Python in Visual Studio Code