1
votes

I have troubles with events in Pygame again (Pygame seems to "avoid" loop

I can't seem to get indented conditions to be detected in my event loop:

for event in pygame.event.get(): 
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
    if event.type == MOUSEBUTTONDOWN:
        print 'mb'
        if event.type == KEYDOWN: #NOT DETECTED
            print 'keydown'
            if event.key == K_e:
                print 'key_e_pressed'

    if event.type == variables.set_ennemies_dest:
        print 'moves

I made sure that I was using event.type and event.key as appropriate, that my indentation was correct. I removed all my code from there and replaced it by print statements to make sure the problem didn't come from somewhere else.

Any help would be very welcome !

If the above look fine this is my full game loop:

import pygame, sys, variables
from pygame.locals import *
from classes import *
from instances import *
from functions import *

pygame.init()
clock = pygame.time.Clock()
pygame.time.set_timer(variables.set_ennemies_dest, 10000) #for ennemi movement
#game loop    
while True:
    clock.tick(60)
    variables.screen.fill((0,0,0)) #make background black
    for event in pygame.event.get(): #setting up quit
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            get_offset(hero,event) # sets the movement offset for the iteration
            print 'mb'
            if event.type == KEYDOWN:
                print 'keydown'
                if event.key == K_e:
                    for i in variables.item_list:
                        i.use(hero)

        if event.type == variables.set_ennemies_dest:
            print 'moves'
            for o in variables.char_list: 
                if isinstance(o, Ranger): #moves characters
                    o.set_rand_dest()



    for o in variables.char_list: 
        if isinstance(o, Ranger): #moves characters
            o.move()
    #offset checks
    group_collision_check(variables.all_sprites_list,hero) #edits the offest based on hero collision
    group_offset(variables.building_list) #new building position using offset
    group_push(variables.item_list,hero)
    scroll_map.offset() #offsets grass background map
    group_offset(variables.ennemi_list)

    variables.screen.blit(scroll_map.image, scroll_map.rect) # blits the grass map to new pos

    variables.building_list.draw(variables.screen) #blits the buildings to new pos

    #blitting characters
    variables.screen.blit(hero.image, hero.rect) #blits hero to screen center
    for o in variables.ennemi_list:
        variables.screen.blit(o.image, o.rect)

    variables.item_list.draw(variables.screen)  

    pygame.display.update()
    variables.offset_time -= 1 #removes on step from the offset counter to keep track of when
    #offset needs to be reset to 0, i.e. position is reached
1

1 Answers

1
votes
for event in pygame.event.get(): 
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
    if event.type == MOUSEBUTTONDOWN:
        print 'mb'
        if event.type == KEYDOWN: # This will NEVER be true. If the event is a mouse button down event, it will never be a keydown event. Solution? Unindent!
            print 'keydown'
            if event.key == K_e: # which means this can also never be true.
                print 'key_e_pressed'

    if event.type == variables.set_ennemies_dest: # have a feeling this is a typo "ennemies dest"
        print 'moves

Added comments to your code to solve what you're running into. In a nut shell, some of your if statements can never be true the way you currently have them structured.

Edit:

Every single event in the event.get() list is going to be a separate entity. So it can't be both a mousebuttondown event AND a keydown event. Each one will be their own event. If you want to test for both conditions you'll probably have to rely on a bool toggle to test when both events are occurring. Something like this:

mouse_toggle = false 
key_toggle = false

for event in pygame.event.get(): 
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
    else:
        if event.type == MOUSEBUTTONDOWN:
            mouse_toggle = true
        if event.type = KEYDOWN:
            if event.key == K_e:
                key_toggle = true
        if event.type = MOUSEBUTTONUP:
            mouse_toggle = false
        if event.type = KEYUP:
            if event.key == K_e:
                key_toggle = false
        if mouse_toggle and key_toggle:
            #do combo of doom