Home > Software engineering >  Change running style of a program in pygame
Change running style of a program in pygame

Time:10-25

I am trying to create a space invader game. in which I have to display different waves 1,2,3 I have written the code

wave1="bgk.png"
if wave1!=wave:
    screen.blit(pygame.image.load(wave),(615,480))
    wave="bgk.png"
    time.sleep(3)
    screen.blit(pygame.image.load(wave),(615,480))

Here 'wave' changes after a certain number of scores are reached but

The problem is that the first image doesn't load before the 3-second break. and after 3 seconds it loads for about 0.1(negligible) amount of time you can see if you look very closely I don't know what is causing this issue please help

The full code is here

#imports
import pygame
import random
import time
import math
#collition detection #multi enmy #sound
#essentials and functions
pygame.init()
screen = pygame.display.set_mode((1920,1080))
pygame.display.set_caption("Space Invaders")

#lifehiscore.txt
life=3
f=open("hiscore.txt",'r')
hiscore=int(f.read())
def lifes():
    global score
    X=1550
    Y=0
    for i in range(life):
        screen.blit(playerimg,(X,Y))
        X =125
        Y =0
    if life==0:
        if score>hiscore:
            f=open("hiscore.txt",'w')
            f.write(str(score))
        exit()

#score
_0=pygame.image.load("0.png")
_1=pygame.image.load("1.png")
_2=pygame.image.load("2.png")
_3=pygame.image.load("3.png")
_4=pygame.image.load("4.png")
_5=pygame.image.load("5.png")
_6=pygame.image.load("6.png")
_7=pygame.image.load("7.png")
_8=pygame.image.load("8.png")
_9=pygame.image.load("9.png")
def scores():
    global score
    score=str(score)
    score_list=[]
    for i in range(len(str(score))):
        if str(score[i])==str(0):
            score_list.append(_0)
        elif str(score[i])==str(1):
            score_list.append(_1)
        elif str(score[i])==str(2):
            score_list.append(_2)
        elif str(score[i])==str(3):
            score_list.append(_3)
        elif str(score[i])==str(4):
            score_list.append(_4)
        elif str(score[i])==str(5):
            score_list.append(_5)
        elif str(score[i])==str(6):
            score_list.append(_6)
        elif str(score[i])==str(7):
            score_list.append(_7)
        elif str(score[i])==str(8):
            score_list.append(_8)
        elif str(score[i])==str(9):
            score_list.append(_9)
    
    X=300
    Y=0
    for i in range(len(score_list)):
        screen.blit(score_list[i],(X,Y))
        X =50
    score=int(score)

def hiscores():
    global hiscore
    hiscore=str(hiscore)
    hiscore_list=[]
    for i in range(len(str(hiscore))):
        if str(hiscore[i])==str(0):
            hiscore_list.append(_0)
        elif str(hiscore[i])==str(1):
            hiscore_list.append(_1)
        elif str(hiscore[i])==str(2):
            hiscore_list.append(_2)
        elif str(hiscore[i])==str(3):
            hiscore_list.append(_3)
        elif str(hiscore[i])==str(4):
            hiscore_list.append(_4)
        elif str(hiscore[i])==str(5):
            hiscore_list.append(_5)
        elif str(hiscore[i])==str(6):
            hiscore_list.append(_6)
        elif str(hiscore[i])==str(7):
            hiscore_list.append(_7)
        elif str(hiscore[i])==str(8):
            hiscore_list.append(_8)
        elif str(hiscore[i])==str(9):
            hiscore_list.append(_9)
    
    X=1000
    Y=0
    for i in range(len(hiscore_list)):
        screen.blit(hiscore_list[i],(X,Y))
        X =50
    hiscore=int(hiscore)

#player player.png
score=0
nscore=0
playerimg=pygame.image.load("player.png")
playerX=896
playerY=908
playerX_change=0
def player():
    global playerX
    global playerY
    global playerX_change
    playerX =playerX_change
    if playerX<=50:
        playerX=50
    if playerX >=1742:
        playerX = 1742
    screen.blit(playerimg, (playerX,playerY))

#bullet
bulletimg=pygame.image.load("bullet.png")
bulletY_change=-50
bulletX=playerX
bulletY=playerY
bullet_state="rest"
def bullet(bulletX,bulletY):
    global bullet_state 
    bulletY-=bulletY_change
    bullet_state="fire"
    if bulletY<=0:
        bullet_state="rest"
    screen.blit(bulletimg,(bulletX,bulletY))

#collision
def collision(x,y,X,Y):
    global score
    global enimyY
    global enimyX
    global enimyX_change
    global bullet_state
    global enimyimg
    global nscore
    distance=math.sqrt( (math.pow(x-X,2))   (math.pow(y-Y, 2)) )
    if distance<145:
        score =1
        nscore =1
        screen.blit(pygame.image.load("D_effect.png"),(X,Y))
        a=enimyimg.index(enimyimg[i])
        enimyimg.pop(a)
        enimyY.pop(a)
        enimyX.pop(a)
        bullet_state="rest"
        a=random.randint(1,3)
        x=random.randint(0,5)
        if a%2==0:
            enimyimg.append(enimylist[x])
            enimyY.append(xlist[x])
            enimyX.append(0)
        else:
            enimyimg.append(enimylist[x])
            enimyY.append(xlist[x])
            enimyX.append(0)

def iscollision():
    global enimyX
    global enimyY
    if len(enimyY)==1:
        pass
    else:
        for i in range(len(enimyX)):
            i-=1
            distance=math.sqrt( (math.pow(enimyX[i]-enimyX[i 1],2))   (math.pow(enimyY[i]-enimyY[i 1], 2)) )
            if distance<100:
                enimyX[i] =100

#enimy
enimylist=[pygame.image.load("enimy1.png"),pygame.image.load("enimy2.png"),pygame.image.load("enimy3.png"),
pygame.image.load("enimy1m.png"),pygame.image.load("enimy2m.png"),pygame.image.load("enimy3m.png")]
xlist=[250,350,450,550,650,750]
enimyimg=[pygame.image.load("enimy1.png")]
enimyX=[0]
enimyY=[250]


no_of_enemy=1
def enimy_quantity():
    global no_of_enemy
    global enimyimg
    global enimyX
    global enimyY
    global score
    global nscore
    global wave
    # enimyY=[]
    # enimyimg=[]
    # enimyY=[]
    if nscore==10:
        enimyX=[]
        enimyY=[]
        enimyimg=[]
        wave="wave1.png"
        for i in range(0,2):
            x=random.randint(0,5)
            enimyimg.append(enimylist[x])
            enimyX.append(0)
            enimyY.append(xlist[x])
            nscore =1
    elif nscore==27:
        enimyX=[]
        enimyY=[]
        enimyimg=[]
        wave="wave2.png"
        for i in range(0,3):
            x=random.randint(0,5)
            enimyimg.append(enimylist[x])
            enimyX.append(0)
            enimyY.append(xlist[x])
            nscore =1
    elif nscore==55:
        enimyX=[]
        enimyY=[]
        enimyimg=[]
        wave="wave3.png"
        for i in range(0,4):
            x=random.randint(0,5)
            enimyimg.append(enimylist[x])
            enimyX.append(0)
            enimyY.append(xlist[x])
            nscore =1
    elif nscore==109:
        enimyX=[]
        enimyY=[]
        enimyimg=[]

        wave="wave4.png"
        for i in range(0,5):
            x=random.randint(0,5)
            enimyimg.append(enimylist[x])
            enimyX.append(0)
            enimyY.append(xlist[x])
            nscore =1
    elif nscore==164:
        enimyX=[]
        enimyY=[]
        enimyimg=[]
        wave="wave5.png"
        for i in range(0,6):
            x=random.randint(0,5)
            enimyimg.append(enimylist[x])
            enimyX.append(0)
            enimyY.append(xlist[x])
            nscore =1


enimyX_change=10
def enimy():
    global enimyX_change
    global enimyY
    global enimyX
    global enimyimg
    global life
    for i in range(len(enimyimg)):
        enimyX[i] =enimyX_change
        if enimyX[i] < 0:
            a=enimyimg.index(enimyimg[i])
            enimyimg.pop(a)
            enimyY.pop(a)
            enimyX.pop(a)
            bullet_state="rest"
            a=random.randint(1,3)
            x=random.randint(0,5)
            if a%2==0:
                enimyimg.append(enimylist[x])
                enimyY.append(xlist[x])
                enimyX.append(0)
            else:
                enimyimg.append(enimylist[x])
                enimyY.append(xlist[x])
                enimyX.append(0)
        elif enimyX[i] > 1742:
            life-=1
            a=enimyimg.index(enimyimg[i])
            enimyimg.pop(a)
            enimyY.pop(a)
            enimyX.pop(a)
            bullet_state="rest"
            a=random.randint(1,3)
            x=random.randint(0,5)
            if a%2==0:
                enimyimg.append(enimylist[x])
                enimyY.append(xlist[x])
                enimyX.append(0)
            else:
                enimyimg.append(enimylist[x])
                enimyY.append(xlist[x])
                enimyX.append(0)
        screen.blit(enimyimg[i], (enimyX[i], enimyY[i]))

wave="bgk.png"
#game loop
running=True
while running:
    screen.blit(pygame.image.load("bg.png"),(0,0))
    
    wave1="bgk.png"
    if wave1!=wave:
        screen.blit(pygame.image.load(wave),(615,480))
        wave="bgk.png"
        time.sleep(3)
        screen.blit(pygame.image.load(wave),(615,480))
    else:
        pass
    
    for event in pygame.event.get():
    
        if event.type==pygame.QUIT:
            running=False

        if event.type == pygame.KEYDOWN:
            
            if event.key == pygame.K_LEFT:
                playerX_change = -20
            
            if event.key == pygame.K_RIGHT:
                playerX_change = 20
            
            if event.key == pygame.K_a:
                playerX_change = -20
            
            if event.key == pygame.K_d:
                playerX_change = 20
            
            if event.key == pygame.K_SPACE:
                if bullet_state=="rest":
                    bulletX=(playerX 64)
                    bulletY=(playerY 0)
                    bullet(bulletX,bulletY)

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                playerX_change = -2
            
            if event.key == pygame.K_RIGHT:
                playerX_change = 2
            
            if event.key == pygame.K_a:
                playerX_change = -2
            
            if event.key == pygame.K_d:
                playerX_change = 2
    
    player()
    scores()

#bullet
    if bullet_state=="rest":
        bulletX=playerX
        bulletY=playerY
    if bullet_state=="fire":
        bullet(bulletX,bulletY)
        bulletY =bulletY_change

#enimy
    enimy()
    enimy_quantity()
    iscollision()
    lifes()
    hiscores()
#collision
    for i in range(len(enimyimg)):
        collision(bulletX, bulletY, enimyX[i], enimyY[i])

    pygame.display.update()

I am very new to the coding culture I don't know how to use StackOverflow i might have made some silly mistakes and i am very sorry for that

CodePudding user response:

pygame.image.load() is a very time consuming operation. It has to read the file from the volume and interpret the image data. Load all images before application loop and use them in the loop

Do not "delay", "wait" or "sleep in the application loop (see How to wait some time in pygame?). Use pygame.time.get_ticks() to measure the time. pygame.time.get_ticks() returns the number of milliseconds since pygame.init() was called.

Add a variable current_bg that stated the current background image. Change the variable after a certain point in time:

bg_image = pygame.image.load("bg.png")
bgk_image = pygame.image.load("bgk.png")
current_bg = bg_image

running=True
while running:
    current_time = pygame.time.get_ticks()
    if current_timev > 3000: # 3000 milliseconds == 3 seconds
        current_bg = bgk_image

    screen.blit(current_bg, (0,0))
    
    # [...]
  • Related