Question

Issue: When the "Combat" function is called the mouse position basically locks in place without ever updating.

Attempted fixes: 1. I've tried moving the get mouse position along with its associated code into different areas of the Function.
2. I've also tried to have the function its self generate a new instance of "pointer".

Obviously these have all failed, the code below is just one of my latest attempts to get this to work.

Can someone explain why this is happening?

import pygame
from pygame.locals import *
pygame.init()

size = [900, 845]
screen = pygame.display.set_mode(size)

class Pointer(pygame.sprite.Sprite):

 def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load("mouse.png").convert_alpha()
    self.rect = self.image.get_rect()
    self.rect.x=5
    self.rect.y=5

all_sprites_list = pygame.sprite.Group()        
pointer = Pointer()
all_sprites_list.add(pointer)

player=5
enemy=5

def Combat():          

    while True:
        pygame.display.flip()
        if player == 0:
            print("GAME OVER")
            break
        elif enemy == 0:
            print("victory!")
            break
        else:            
            mx,my = pygame.mouse.get_pos()      
            pointer.rect.x=mx
            pointer.rect.y=my
            print(mx,my)

done = False
clock = pygame.time.Clock()

while done ==False:

    clock.tick(30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            done = True


    mx,my = pygame.mouse.get_pos()
    print(mx,my)
    screen.fill((200,200,200))
    pointer.rect.x=mx
    pointer.rect.y=my  
    all_sprites_list.draw(screen)
    Combat()

    pygame.display.flip()

pygame.quit()
Was it helpful?

Solution

The Combat() function is choking your system and not processing new events.

You should add a clock.tick() and poll events:

def Combat():          

    while True:
        clock.tick(30)
        pygame.event.poll()
        ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top