It's known that if you move a sprite without filling the screen before it leaves a trail, however, I want to leave a cool trail behind my while moving other stuff (which means I cannot simply stop filling the screen.
Thanks in advance for any help
One solution would be to create another transparent surface (called alpha_surf here) with the size of the screen onto which you blit the objects with trails. It needs to be a per-pixel alpha surface which you can create by passing the pygame.SRCALPHA special flag.
Blit the objects and reduce the alpha of all pixels on the alpha_surf each frame by filling it with a transparent white and also pass the pygame.BLEND_RGBA_MULT flag so that only the alpha channel is affected.
import pygame as pg
from pygame.math import Vector2
class Player(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(*groups)
self.image = pg.Surface((50, 50), pg.SRCALPHA)
pg.draw.circle(self.image, pg.Color('dodgerblue'), (25, 25), 25)
self.rect = self.image.get_rect(center=pos)
self.vel = Vector2(0, 0)
self.pos = Vector2(pos)
def update(self):
self.pos += self.vel
self.rect.center = self.pos
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
# Blit objects with trails onto this surface instead of the screen.
alpha_surf = pg.Surface(screen.get_size(), pg.SRCALPHA)
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
player = Player((150, 150), all_sprites)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
elif event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player.vel.x = 5
elif event.key == pg.K_a:
player.vel.x = -5
elif event.key == pg.K_w:
player.vel.y = -5
elif event.key == pg.K_s:
player.vel.y = 5
elif event.type == pg.KEYUP:
if event.key == pg.K_d and player.vel.x > 0:
player.vel.x = 0
elif event.key == pg.K_a and player.vel.x < 0:
player.vel.x = 0
elif event.key == pg.K_w:
player.vel.y = 0
elif event.key == pg.K_s:
player.vel.y = 0
# Reduce the alpha of all pixels on this surface each frame.
# Control the fade speed with the alpha value.
alpha_surf.fill((255, 255, 255, 220), special_flags=pg.BLEND_RGBA_MULT)
all_sprites.update()
screen.fill((20, 50, 80)) # Clear the screen.
all_sprites.draw(alpha_surf) # Draw the objects onto the alpha_surf.
screen.blit(alpha_surf, (0, 0)) # Blit the alpha_surf onto the screen.
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()
Alternatively, you could create several versions of the sprite image with different alpha values and also store the previous positions of the sprite. Then just blit the images with lower alpha at the previous positions.
You can also blit other images or particles instead of the self.image if you want to create a different kind of trail, for example smoke.
Here's another variant with a separate, different image for the trail which gets blitted before the self.images of the sprites are blitted, so that it'll appear below them:
import pygame as pg
from pygame.math import Vector2
class Player(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(*groups)
self.image = pg.Surface((50, 70), pg.SRCALPHA)
self.image.fill(pg.Color('sienna1'))
self.rect = self.image.get_rect(center=pos)
# A separate image for the trail (just a single-color circle).
self.trail_image = pg.Surface((40, 40), pg.SRCALPHA)
pg.draw.circle(self.trail_image, pg.Color('dodgerblue'), (20, 20), 20)
self.trail_rect = self.trail_image.get_rect()
self.vel = Vector2(0, 0)
self.pos = Vector2(pos)
def update(self):
self.pos += self.vel
self.rect.center = self.pos
# Update the rect of the trail as well, because we'll blit it there.
self.trail_rect.center = self.rect.center
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
# Blit objects with trails onto this surface instead of the screen.
alpha_surf = pg.Surface(screen.get_size(), pg.SRCALPHA)
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
sprites_with_trails = pg.sprite.Group()
player = Player((150, 150), all_sprites, sprites_with_trails)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
elif event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player.vel.x = 5
elif event.key == pg.K_a:
player.vel.x = -5
elif event.key == pg.K_w:
player.vel.y = -5
elif event.key == pg.K_s:
player.vel.y = 5
elif event.type == pg.KEYUP:
if event.key == pg.K_d and player.vel.x > 0:
player.vel.x = 0
elif event.key == pg.K_a and player.vel.x < 0:
player.vel.x = 0
elif event.key == pg.K_w:
player.vel.y = 0
elif event.key == pg.K_s:
player.vel.y = 0
# Reduce the alpha of all pixels on this surface each frame.
# Control the fade speed with the alpha value.
alpha_surf.fill((255, 255, 255, 244), special_flags=pg.BLEND_RGBA_MULT)
all_sprites.update()
screen.fill((20, 50, 80)) # Clear the screen.
# Blit the trails onto the alpha_surf.
for sprite in sprites_with_trails:
alpha_surf.blit(sprite.trail_image, sprite.trail_rect)
screen.blit(alpha_surf, (0, 0)) # Blit the alpha_surf onto the screen.
all_sprites.draw(screen) # Draw the objects onto the alpha_surf.
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()
I'm creating a little Pong-like program using Python and Pygame. I will edit it to add a point system and other features soon, but I want to address a problem first.
In this Pong program, you get three lives, and if you lose on the last life, it should display a game over image. It works perfectly fine with the first three lives, but it does not produce the game over image, and produces this instead:
Traceback (most recent call last):
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 258, in <module>
gameplay1()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 84, in gameplay1
change_level1()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 241, in change_level1
gameplay2()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 161, in gameplay2
change_level2()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 245, in change_level2
gameplay3()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 238, in gameplay3
game_over()
File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 255, in game_over
screen.blit(game_over1(300,250))
TypeError: 'pygame.Surface' object is not callable
My code consists of lots of def functions, but the others work perfectly fine. This is my code:
import pygame, time, sys, random
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption ("Pong Squash")
def gameplay1():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
game_over_display = "game_over.png"
lives = "lives.png"
points = "points.png"
lives_remaining = "lives_remaining.png"
game_over1 = pygame.image.load(game_over_display).convert()
lives1 = pygame.image.load(lives).convert()
points1 = pygame.image.load(points).convert()
lives_remaining1 = pygame.image.load(lives_remaining).convert()
font1 = pygame.font.Font(None, 40)
white = 255,255,255
black = 0, 0, 0
green = 0, 250, 0
yellow = 255, 255, 0
lives_count = font1.render(('3'), True, white)
position_x = 175
position_y = 375
velocity_x1 = 0
velocity_y1 = 0
position1 = 275
position2 = 150
velocity1 = 2
velocity2 = 2
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == pygame.K_SPACE:
if waiting:
waiting = False
reset_ball()
if event.key == pygame.K_LEFT:
velocity_x1 = (velocity_x1 - 3)
elif event.key == pygame. K_RIGHT:
velocity_x1 = (velocity_x1 + 3)
elif event.type == KEYUP:
if event.key == K_LEFT or event.key == K_RIGHT:
velocity_x1 = 0
screen.fill((0, 0, 0))
color = 255
width = 0
position = position_x, position_y, 250, 50
position_x += velocity_x1
position_y += velocity_y1
position1 += velocity1
position2 += velocity2
player = pygame.draw.rect(screen, (color, color, color), position, width)
ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 25, 25), width)
screen.blit(lives1, (450, 0))
screen.blit(points1,(0, 0))
screen.blit(lives_count,(560,5))
pygame.display.update()
if player.colliderect(ball):
velocity1 = - velocity1
velocity2 = - velocity2
if position_x > 350 or position_x < 0:
velocity_x1 = 0
elif position1 > 575 or position1 < 0:
velocity1 = - velocity1
elif position2 < 0:
velocity2 = - velocity2
velocity2 = velocity2 + 0.5
elif position2 > 475:
change_level1()
def gameplay2():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
game_over_display = "game_over.png"
lives = "lives.png"
points = "points.png"
lives_remaining = "lives_remaining.png"
game_over1 = pygame.image.load(game_over_display).convert()
lives1 = pygame.image.load(lives).convert()
points1 = pygame.image.load(points).convert()
lives_remaining1 = pygame.image.load(lives_remaining).convert()
font1 = pygame.font.Font(None, 40)
white = 255,255,255
black = 0, 0, 0
green = 0, 250, 0
yellow = 255, 255, 0
lives_count = font1.render(('2'), True, white)
position_x = 175
position_y = 375
velocity_x1 = 0
velocity_y1 = 0
position1 = 275
position2 = 150
velocity1 = 2
velocity2 = 2
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == pygame.K_SPACE:
if waiting:
waiting = False
reset_ball()
if event.key == pygame.K_LEFT:
velocity_x1 = (velocity_x1 - 3)
elif event.key == pygame. K_RIGHT:
velocity_x1 = (velocity_x1 + 3)
elif event.type == KEYUP:
if event.key == K_LEFT or event.key == K_RIGHT:
velocity_x1 = 0
screen.fill((0, 0, 0))
color = 255
width = 0
position = position_x, position_y, 250, 50
position_x += velocity_x1
position_y += velocity_y1
position1 += velocity1
position2 += velocity2
player = pygame.draw.rect(screen, (color, color, color), position, width)
ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 25, 25), width)
screen.blit(lives1, (450, 0))
screen.blit(points1,(0, 0))
screen.blit(lives_count,(560,5))
pygame.display.update()
if player.colliderect(ball):
velocity1 = - velocity1
velocity2 = - velocity2
if position_x > 350 or position_x < 0:
velocity_x1 = 0
elif position1 > 575 or position1 < 0:
velocity1 = - velocity1
elif position2 < 0:
velocity2 = - velocity2
velocity2 = velocity2 + 0.5
elif position2 > 475:
change_level2()
def gameplay3():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
game_over_display = "game_over.png"
lives = "lives.png"
points = "points.png"
lives_remaining = "lives_remaining.png"
game_over1 = pygame.image.load(game_over_display).convert()
lives1 = pygame.image.load(lives).convert()
points1 = pygame.image.load(points).convert()
lives_remaining1 = pygame.image.load(lives_remaining).convert()
font1 = pygame.font.Font(None, 40)
white = 255,255,255
black = 0, 0, 0
green = 0, 250, 0
yellow = 255, 255, 0
lives_count = font1.render(('1'), True, white)
position_x = 175
position_y = 375
velocity_x1 = 0
velocity_y1 = 0
position1 = 275
position2 = 150
velocity1 = 2
velocity2 = 2
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == pygame.K_SPACE:
if waiting:
waiting = False
reset_ball()
if event.key == pygame.K_LEFT:
velocity_x1 = (velocity_x1 - 3)
elif event.key == pygame. K_RIGHT:
velocity_x1 = (velocity_x1 + 3)
elif event.type == KEYUP:
if event.key == K_LEFT or event.key == K_RIGHT:
velocity_x1 = 0
screen.fill((0, 0, 0))
color = 255
width = 0
position = position_x, position_y, 250, 50
position_x += velocity_x1
position_y += velocity_y1
position1 += velocity1
position2 += velocity2
player = pygame.draw.rect(screen, (color, color, color), position, width)
ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 25, 25), width)
screen.blit(lives1, (450, 0))
screen.blit(points1,(0, 0))
screen.blit(lives_count,(560,5))
pygame.display.update()
if player.colliderect(ball):
velocity1 = - velocity1
velocity2 = - velocity2
if position_x > 350 or position_x < 0:
velocity_x1 = 0
elif position1 > 575 or position1 < 0:
velocity1 = - velocity1
elif position2 < 0:
velocity2 = - velocity2
velocity2 = velocity2 + 0.5
elif position2 > 475:
game_over()
def change_level1():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
gameplay2()
pygame.display.update()
def change_level2():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
gameplay3()
pygame.display.update()
def reset_ball():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
velocity1 = 2
velocity2 = 2
pygame.display.update()
def game_over():
global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
screen.fill((0,0,0))
screen.blit(game_over1(300,250))
pygame.display.update()
gameplay1()
Sorry, this is so long. Please help! I'm new to Pygame!
screen.blit(game_over1(300,250))
docs: pygame.Surface
screen.blit(game_over1, (300,250))