0
\$\begingroup\$

I'm trying to make when the rock hits the spaceship or player it should decrease the health from the health bar but when I run it, it doesn't work. The rock decreases the health randomly. How can I fix that?

# 1 - Import library
import pygame
from pygame.locals import *
import math
import random
# 2 - Initialize the game
pygame.init()
pygame.display.set_caption('Rescue Mission!')
clock = pygame.time.Clock()
width, height = 840, 680
screen=pygame.display.set_mode((width, height))
keys = [False, False, False, False]
playerpos=[380,620]
acc=[0,0]
laserbeams=[]
objecttimer=100
objecttimer1=0
rocks=[[640,100]]
healthvalue=194
lifeboostertimer= 200
lifeboostertimer1=0
lifebooster=[[640,100]]

# 3 - getting images from files 
player = pygame.image.load('spaceship.png')
background = pygame.image.load("background.png")
background = pygame.transform.scale(background, (840, 680))
laserbeam = pygame.image.load("laserbeam2.png")
rockpic1 = pygame.image.load("ro.png")
rockpic=rockpic1
healthbar = pygame.image.load("healthbar.png")
health = pygame.image.load("health.png")
lifeboost = pygame.image.load("lifeboost.png")
lifeboost1 = lifeboost

# 4 - keep looping through
while 1:
    objecttimer-=1
    lifeboostertimer-=1
    # 5 - clear the screen before drawing it again
    screen.fill(0)

    # 6 - putting the images of the game on screen



    screen.blit(background,(0,0))
    #screen.blit(lifeboost, (100,100))

    position = pygame.mouse.get_pos()
    angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26))
    playerrot = pygame.transform.rotate(player, 360-angle*57.29)
    playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2)
    screen.blit(playerrot, playerpos1)

    for bullet in laserbeams:
        index=0
        velx=math.cos(bullet[0])*10
        vely=math.sin(bullet[0])*10
        bullet[1]+=velx
        bullet[2]+=vely
        if bullet[1]<-64 or bullet[1]>840 or bullet[2]<-64 or bullet[2]>680:
            laserbeams.pop(index)
        index+=1
        for projectile in laserbeams:
            laserbeam1 = pygame.transform.rotate(laserbeam, 360-projectile[0]*57.29)
            screen.blit(laserbeam1, (projectile[1], projectile[2]))
    # Draw Rocks
    if objecttimer==0:
        rocks.append([840, random.randint(50,430)])
        objecttimer=90-(objecttimer1*2)
        if objecttimer1>=35:
            objecttimer1=35
        else:
            objecttimer1+=6
    index=0
    for rock in rocks:
        if rock[0]<-1:
            rocks.pop(index)
        rock[0]-=7
        # - Attack castle(change to Attack spaceship later on)
        rockrect=pygame.Rect(rockpic.get_rect)
        playerrect = pygame.Rect(player.get_rect)
        rockrect.top=rock[1]
        rockrect.left=rock[0]

        if rockrect.colliderect(playerrect):
            healthvalue -= random.randint(5,20)

            rocks.pop(index)
        # Check for collisions
        index1=0
        #index3=0
        for bullet in laserbeams:
            bullrect=pygame.Rect(laserbeam.get_rect())
            bullrect.left=bullet[1]
            bullrect.top=bullet[2]

            if rockrect.colliderect(bullrect):
                acc[0]+=1
                rocks.pop(index)

                laserbeams.pop(index1)

            index1+=1
        index += 1


    # Next Rock

        #index+=1
    #for rock in rocks:
        #screen.blit(rockpic,rock)

    # Draw 1-UP image
    if lifeboostertimer==0:
        lifebooster.append([840, random.randint(50,430)])

        lifeboostertimer=1000-(lifeboostertimer1*1)
        if lifeboostertimer1>=15:
            lifeboostertimer1=15

        else:
            lifeboostertimer1+=3
    index2=0

    # Makes object move
    for lifeboost in lifebooster:
        if lifeboost[0]<-1:
            lifebooster.pop(index2)
        #speed of 1-UP
        lifeboost[0]-=7


        screen.blit(lifeboost1,lifeboost)


        lifeboosterrect=pygame.Rect(lifeboost1.get_rect())
        lifeboosterrect.top=lifeboost[1]
        lifeboosterrect.left=lifeboost[0]
        #if lifeboosterrect.left<1:
            #healthvalue += random.randint(5,20)

        # Check for collisions (if hit for lifebooster give more health)
        #index=0

        #index2=0
        index3=0


        for bullet in laserbeams:
            bullrect=pygame.Rect(laserbeam.get_rect())
            bullrect.left=bullet[1]
            bullrect.top=bullet[2]

            if lifeboosterrect.colliderect(bullrect):
                acc[0]+=1
                lifebooster.pop(index2)
                laserbeams.pop(index3)
                print ("rock poped")
                healthvalue += random.randint(5,20)
            index3+=1

   # - Next 1-UP image
    #index2+=1   #can probably take this line and next line out
    #for lifeboost in lifebooster:
        screen.blit(lifeboost1,lifeboost)
    index+=1
    for rock in rocks:
        screen.blit(rockpic,rock)

    # 6.4 - Draw clock
    font = pygame.font.Font(None, 24)
    survivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)+":"+str((90000-pygame.time.get_ticks())/1000%60).zfill(2), True, (0,0,0))
    textRect = survivedtext.get_rect()
    textRect.topright=[635,5]
    screen.blit(survivedtext, textRect)

    # 6.5 - Draw health bar
    screen.blit(healthbar, (5,5))
    for health1 in range(healthvalue):
        screen.blit(health, (health1+8,8))

    # 7 - update the screen
    pygame.display.flip()
    clock.tick(60)
    # 8 - loop through the events
    for event in pygame.event.get():
        #print (event)
        # check if the event is the X button 
        if event.type==pygame.QUIT:
            pygame.quit() 
            exit(0) 

           # if it is quit the game
        if event.type == pygame.KEYDOWN:
            if event.key==K_UP:
                keys[0]=True

            elif event.key==K_LEFT:
                keys[1]=True

            elif event.key==K_DOWN:
                 keys[2]=True

            elif event.key==K_RIGHT:
                keys[3]=True
        if event.type == pygame.KEYUP:
            if event.key==pygame.K_UP:
                keys[0]=False
            elif event.key==pygame.K_LEFT:
                keys[1]=False
            elif event.key==pygame.K_DOWN:
                keys[2]=False
            elif event.key==pygame.K_RIGHT:
                keys[3]=False
        if event.type==pygame.MOUSEBUTTONDOWN:
            position=pygame.mouse.get_pos()
            acc[1]+=1
            laserbeams.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32])


    # 9 - Move player
    if keys[0]:
        playerpos[1]-=5
    elif keys[2]:
        playerpos[1]+=5
    if keys[1]:
        playerpos[0]-=5
    elif keys[3]:
        playerpos[0]+=5
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.