How To Have A Logo Flash Before The Game Starts? - python

Is there any way that before my game starts i could show a logo of some sort? For example when Sonic The Hedgehog first starts you seethe SEGA logo before the actual game starts! If this is possible it would be great!
Thanks!

at the begining of your game, just blit your logo.
import pygame
import time
import os, sys
print 'Splash load...'
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((500,80),pygame.NOFRAME)
background = pygame.Surface(screen.get_size())
background.fill((2,24,244))
screen.blit(background, (0,0))
screen.blit(pygame.font.Font('pala.ttf', 72).render('Loading...', 1, (255,255,255)), (90,10))
pygame.display.update()
time.sleep(5)
its as easy as that

Related

MoviePy preview showing besides screen blit

I've got a problem.
I'm creating the Game Menu which i want to be:
background as video file (using MoviePy)
buttons on the video
The problem is video is showing, but the button1 not.
Please help...
from moviepy.editor import *
import pygame
from pygame import mixer
import os
import cv2
print(os.getcwd())
# Initialization the PyGame
pygame.init()
# Create the screen
WIN = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# Menu Background
background = pygame.image.load('Assets/background.png')
button1 = pygame.image.load('Assets/button.png')
# Background SOUND
mixer.music.load('Assets/mainmenumusic.mp3')
mixer.music.set_volume(0.2)
mixer.music.play(-1)
# Game TITLE and ICON
pygame.display.set_caption("Game Launcher") # title of the game
icon = pygame.image.load('Assets/icon.png') # define the icon variable
pygame.display.set_icon(icon) # set the window icon to the icon variable
# FPS Lock
FPS = 60
clip = VideoFileClip('Assets/particles.mp4', audio=False)
# Game loop
clock = pygame.time.Clock()
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
WIN.blit(button1, (960, 540))
clip.preview(fullscreen=True)
pygame.display.update()
The button is not showing because you blit it before the background video so the video covers it. You can just try with:
clip.preview(fullscreen=True)
WIN.blit(button1, (960, 540))

Pygame fading error when it is in a function

I am making an RPG in Pygame and I am having an error that is ruining the point of a fade. What is happening is that when I try to put the fading code in a function, so I don't have to have it dozens of times, it completely skips over the fading part and just flashes.
This is the working code
import pygame
import time
delay = time.sleep
white = 255,255,255
black = 0,0,0
pygame.display.set_caption("Liam's RPG")
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
menuIntro = False
while not menuIntro:
menuIntro = True
logo = pygame.image.load("logo.png").convert()
logo.set_alpha(0)
for alpha in range(255):
screen.fill(black)
screen.blit(logo, (100,35))
logo.set_alpha(alpha)
clock.tick(200)
pygame.display.update()
delay(1.5)
This is the code that causes the fade to skip and mess up
import pygame
import time
delay = time.sleep
white = 255,255,255
black = 0,0,0
pygame.display.set_caption("Liam's RPG")
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
menuIntro = False
sprite = ""
fade_x = 0
fade_y = 0
clocking = clock.tick(10)
def fading(sprite,clocking,fade_x,fade_y):
logo = pygame.image.load(sprite + ".png").convert()
logo.set_alpha(0)
for alpha in range(255):
screen.fill(black)
screen.blit(logo, (fade_x,fade_y))
logo.set_alpha(alpha)
clocking
while not menuIntro:
menuIntro = True
sprite = 'logo'
fade_x = 100
fade_y = 35
clocking = clock.tick(200)
fading(sprite,clocking,fade_x,fade_y)
pygame.display.update()
delay(1.5)
Here is the logo thing that I am trying to fade in, it needs to be in the same directory as the file
In correct code you have tick and update inside for alpha loop. In incorrect code you have tick and update outside this loop - and it doesn't work as you expect.

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 do I add more than 2 sprites into an animation in pygame?

#Importing the pygame functions
import pygame
import sys
import os
from pygame.locals import *
#Allows for the editing of a window
pygame.init()
#Sets screen size
window = pygame.display.set_mode((800,600),0,32)
#Names the window
pygame.display.set_caption("TEST")
#Types of colors (red,green,blue)
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,0)
red = (255,0,0)
purple = (255,0,255)
lightblue = (0,255,255)
white = (255,255,255)
pink = (255,125,125)
clock = pygame.time.Clock()
L1="bolt_strike_0001.PNG"
L1=pygame.image.load(L1).convert_alpha()
L2="bolt_strike_0002.PNG"
L2=pygame.image.load(L2).convert_alpha()
L3="bolt_strike_0003.PNG"
L3=pygame.image.load(L3).convert_alpha()
L4="bolt_strike_0004.PNG"
L4=pygame.image.load(L4).convert_alpha()
lightingCurrentImage = 1
#Loop
gameLoop = True
while gameLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameLoop=False #Allows the user to exit the loop/game
window.fill(black) #used to fill the creen with the certian color variables
if (lightingCurrentImage==1):
window.blit(L1, (0,0))
if (lightingCurrentImage==2):
window.blit(L2, (0,0))
if (lightingCurrentImage==3):
window.blit(L3, (0,0))
if (lightingCurrentImage==4):
window.blit(L4, (0,0))
if (lightingCurrentImage==2):
lightingCurrentImage=1
if (lightingCurrentImage==3):
lightingCurrentImage=2
if (lightingCurrentImage==4):
lightingCurrentImage=3
else:
lightingCurrentImage+=3;
pygame.display.flip() #must flip the image o the color is visable
clock.tick(5)
pygame.quit() #quit the pygame interface
exit(0)
I'm having problems stitching together 10 images of a lightning bolt animation in pygame. What I have at the moment works but its not what I want it to look like. What happens when I run this is the lightning bolt creates the animation sequence once then disappears and never restarts the sequence again. If I set lightingCurrentImage+=3 to lightingCurrentImage+=2 it appears and stays on the screen but doesn't ever disappear. Please help me to see what the problem is if you can. Thanks! (I want the lightning bolt to begin and go all the way through the animation then disappear. Then begin again and repeat).
First create list of images then you can use it this way:
bold_imgs = []
bold_imgs.append( pygame.image.load("bolt_strike_0001.PNG").convert_alpha() )
bold_imgs.append( pygame.image.load("bolt_strike_0002.PNG").convert_alpha() )
bold_imgs.append( pygame.image.load("bolt_strike_0003.PNG").convert_alpha() )
bold_imgs.append( pygame.image.load("bolt_strike_0004.PNG").convert_alpha() )
lightingCurrentImage = 0
while True:
# here ... your code with events
window.fill(black)
window.blit( bold_imgs[ lightingCurrentImage ], (0,0))
lightingCurrentImage += 1
if lightingCurrentImage = len( bold_imgs ):
lightingCurrentImage = 0
pygame.display.flip()
clock.tick(5)
You can use tick(25) to get faster but smoother animation.
Human eye needs at least 25 images per second to see it as smooth animation.

Why is sprite image flashing/filckering?

How can i prevent the sprite image from flickering uncontrollably? (image name is plumbers). when you run the program the only image flickering is the sprite.
import pygame
import os, sys
import itertools
import pygame
from pygame.sprite import Sprite
cloud_background = pygame.image.load('clouds.bmp')
brick_tile = pygame.image.load('brick_tile.png')
plumbers = pygame.image.load('Mario_sideways_sprite_2xL.png')
pink = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
running = 1
def setup_background():
screen.fill((pink))
screen.blit(cloud_background,(0,0))
brick_width, brick_height = brick_tile.get_width(), brick_tile.get_height()
for x,y in itertools.product(range(0,640,brick_width),
range(390,480,brick_height)):
# print(x,y)
screen.blit(brick_tile, (x,y))
def show_sprites():
screen.blit(plumbers,(50,337))
pygame.display.flip()
while running:
show_sprites()
setup_background()
pygame.display.flip()
event = pygame.event.poll()
if event.type == pygame.QUIT: sys.exit()
I don't really know pygame, but whilst you wait for answer from someone with more experience in this area. I can offer this advice which might help.
Your setting up the background from scratch each time which is a processor intensive process. The best approach is to instead actually only re-render the part of the background required. I.e the parts where your plumber sprite occupied before.
Normally you do this by creating two variables called old X, old Y. This the rendering process is spend up.
Currently your render the entire screen every loop cycle.

Categories