My game character only moves when the mouse moves on the screen while it needs a mouse

Viewed 61

I just start learning pygame, the problem is : the game character that I'm making is moving when mouse is moving in screen even though I don't need to use mouse and I need help with it. I added some code for bullets this part is not important for me now because the space ship is not moving without moving mouse in screen .

import pygame
from pygame.locals import*

#define fps
clock = pygame.time.Clock()
fps=60


screen_width = 600
screen_height = 800

screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption(' space invaders')

#colors
red=(255,0,0)
green=(0,255,0)


#load image
bg = pygame.image.load("img/bg.png")

def draw_bg():
    screen.blit(bg,(0,0))




#create spaceship class
class Spaceship(pygame.sprite.Sprite):
    def __init__(self,x,y,health):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("img/spaceship.png")
        self.rect = self.image.get_rect()
        self.rect.center = [x, y]
        self.health_start = health
        self.health_remaining = health
        self.last_shot = pygame.time.get_ticks()

    def update(self):
        #set movement speed
        speed=8

        #get key press
        key=pygame.key.get_pressed()
        if key[pygame.K_LEFT] and self.rect.left>0:
            self.rect.x-=speed

        if key[pygame.K_RIGHT] and self.rect.right<screen_width:
            self.rect.x+=speed

        time_now=pygame.time.get_ticks()

        #shoot

        if key[pygame.K_SPACE]:
            bullet = Bullet(self.rect.centerx,self.rect.top)

            bullet_group.add(bullet)
            
        pygame.mouse.get_pos()



        #draw health bar
        pygame.draw.rect(screen,red,(self.rect.x,(self.rect.bottom+10),self.rect.width,15))
        
        if self.health_remaining>0:
            pygame.draw.rect(screen,green,(self.rect.x,(self.rect.bottom+10),int(self.rect.width*(self.health_remaining / self.health_start)),15))



#create Bullets class
class Bullets(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load("img/bullet.png")
        self.rect = self.image.get_rect()
        self.rect.center=[x,y]
    
    def update(self):
        self.rect.y-=5




# sprite groups   
spaceship_group = pygame.sprite.Group()
bullet_group=pygame.sprite.Group()




#create player
spaceship=Spaceship(int(screen_width / 2),screen_height-100 ,3)
spaceship_group.add(spaceship)


run = True
while run:
    clock.tick(fps)
    draw_bg()

    #events 
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False

        #update spaceship
        spaceship.update()

        update bullets
        bullet_group.update()

        
        spaceship_group.draw(screen)
        bullet_group.draw(screen)


        pygame.display.update()
        
pygame.quit



 
1 Answers

Looks like an indentation problem.

When your code is indented inside the for event in pygame.event.get() loop, it only runs when pygame receives events. You notice this through MOUSEMOTION events.

run = True
while run:
    clock.tick(fps)
    draw_bg()

    #events 
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False

    # <---- moved this one indentation level, out of the evnet loop
    #update spaceship
    spaceship.update()

    #update bullets
    bullet_group.update()
   
    spaceship_group.draw(screen)
    bullet_group.draw(screen)

    pygame.display.update()
Related