Why don't my buttons work?

Why don't my buttons work?

Postby spamynator_1 » Tue Sep 24, 2013 7:58 pm

My apologize if im spamming this website with my newbness, but i have been looking over my code all day to find out why the hell these damn buttons dont work. THey worked yesterday and i cant for the life of me find out what i did to them.
Code: Select all
import sys
import os
import pygame as pg
import pygame.mixer
import json
from pygame.locals import *

SCREEN_SIZE = SCREEN_WIDTH,SCREEN_HEIGHT = (1024,768)

RED = (255,0,0)
ORANGE = (255,127,0)
YELLOW = (255,255,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
INDIGO = (75,0,130)
VIOLET = (143,0,255)

Q = 1#OPEN vARIABLE

clock = pg.time.Clock()

class Control(object):
    def __init__(self):
        self.screen = pg.display.get_surface()
        self.done = False
        self.state = "START"
        #Save File Shit
        self.locationx = 200
        self.locationy = 200
        self.mapid = (1)
        self.items = (0,1)
        #Movement Control
        self.char = 1
        self.chariable = 1
        self.charde = 1
        #Movement Control End
        self.surface
        self.clock = pg.time.Clock()
        self.fps = 60
        self.enter_coords = [0,400]
        self.keys = pg.key.get_pressed()
       
    def event_loop(self):
        for event in pg.event.get():
            self.keys = pg.key.get_pressed()
            if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
                self.done = True

            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_RETURN and self.state == "START":
                    self.state = "MENU"

                elif self.state == "MENU":
                    if event.type == pg.MOUSEBUTTONDOWN:
                        if event.button == 1:
                            if BUTTON_NEW_rect.collidepoint(event.pos):
                                self.state = "PLAY"                           
                            elif BUTTON_CONTINUE_rect.collidepoint(event.pos):
                                run_it.load
                                self.state = "CONTINUE"
               
                   #MOVEMENT   
                elif event.key == pg.K_w:
                    self.locationy -= 6
                    self.char = 4
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
               
                elif event.key == pg.K_s:
                    self.locationy += 6
                    self.char = 1
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
                   
                elif event.key == pg.K_a:
                    self.locationx -= 6
                    self.char = 2
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
                       
                elif event.key == pg.K_d:
                    self.locationx += 6
                    self.char = 3
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2

    def surface(self):
        global START_PAGEq
        START_PAGEq = pg.image.load(os.path.join('images', 'startpageq.png'))
        self.surface = pg.Surface.convert(START_PAGEq)

    def save(self):
        if self.state == "PLAY":
            print("Saving")
            savex = open('corn.dik','w+')
            savex.write(str(self.locationx))
           
            savey = open('stuffing.dik','w+')
            savey.write(str(self.locationy))
           
            savei = open('turkey.dik','w+')
            savei.write(str(self.items))
           
            savem = open('gravy.dik','w+')
            savem.write(str(self.mapid))

    def load(self):
        if self.state == "CONTINUE":
            print("Loading")
            osavex = open('corn.dik','r')
            self.locationx = osavex.read()

            osavey = open('stuffing.dik','r')
            self.locationy = osavey.read()

            osavei = open('turkey.dik','r')
            self.items = osavei.read()

            osavem = open('gravy.dik','r')
            self.mapid = osavem.read()
            self.state = "PLAY"
                       
    def main_loop(self):
        while not self.done:
            self.event_loop()
            self.update()
            pg.display.update()
            self.clock.tick(self.fps)

    def update(self):
        print (self.fps)
        if self.state == "START":
            self.screen.blit(START_PAGE,(0,0))
            self.screen.blit(PRESS_ENTER,self.enter_coords)
            self.enter_coords[0] = (self.enter_coords[0]+2)
           
        elif self.state == "MENU":
            self.screen.blit(START_PAGE,(0,0))
            self.screen.blit(BUTTON_NEW, BUTTON_NEW_rect)
            self.screen.blit(BUTTON_CONTINUE, BUTTON_CONTINUE_rect)
           
        elif self.state == "PLAY":
            run_it.save()
            self.screen.blit(self.surface,(0,0))
            if self.chariable > 20:
                self.chariable = 1
            #S
            elif self.char == 1:
                if self.charde == 1:
                    self.screen.blit(CHAR1,(self.locationx,self.locationy))
                    self.chariable += 1
                elif self.charde == 2:
                    self.screen.blit(CHAR12,(self.locationx,self.locationy))
                    self.chariable += 1
                elif self.charde == 3:
                    self.screen.blit(CHAR13,(self.locationx,self.locationy))
                    self.chariable += 1
            #A
            elif self.char == 2:
                if self.charde == 1:
                    self.screen.blit(CHAR2,(self.locationx,self.locationy))
                    self.chariable += 1
                elif self.charde == 2:
                    self.screen.blit(CHAR22,(self.locationx,self.locationy))
                    self.chariable += 1
                elif self.charde == 3:
                    self.screen.blit(CHAR23,(self.locationx,self.locationy))
                    self.chariable += 1
            #D
            elif self.char == 3:
                if self.charde == 1:
                    self.screen.blit(CHAR3,(self.locationx,self.locationy))
                    self.chariable += 1
                elif self.charde == 2:
                    self.screen.blit(CHAR32,(self.locationx,self.locationy))
                    self.chariable += 1
                elif self.charde == 3:
                    self.screen.blit(CHAR33,(self.locationx,self.locationy))
                    self.chariable += 1
            #W
            elif self.char == 4:
                if self.charde == 1:
                    self.screen.blit(CHAR4,(self.locationx,self.locationy))
                    self.chariable += 1
                elif self.charde == 2:
                    self.screen.blit(CHAR42,(self.locationx,self.locationy))
                    self.chariable += 1
                elif self.charde == 3:
                    self.screen.blit(CHAR43,(self.locationx,self.locationy))
                    self.chariable += 1
       
if __name__ == "__main__":
    os.environ["SDL_VIDEO_CENTERED"] = '1'
    pg.init()
    pg.display.set_mode(SCREEN_SIZE)
    pg.key.set_repeat(1,50)

    START_PAGE = pg.image.load(os.path.join('images', 'startpage.png'))       
    #start of button shit
    BUTTON_NEW = pg.image.load(os.path.join('images', 'buttonnew.png'))
    BUTTON_NEW_rect = BUTTON_NEW.get_rect(center=(200,500))
   
    BUTTON_CONTINUE = pg.image.load(os.path.join('images', 'buttoncontinue.png'))
    BUTTON_CONTINUE_rect = BUTTON_CONTINUE.get_rect(center=(824,500))
    #end of button shit
   
    mx,my = pg.mouse.get_pos()

    #Character Images load(lol)
    CHAR1 = pg.image.load(os.path.join('images', 'char1.png'))
    CHAR12 = pg.image.load(os.path.join('images', 'char1-2.png'))
    CHAR13 = pg.image.load(os.path.join('images', 'char1-3.png'))
    CHAR2 = pg.image.load(os.path.join('images', 'char2.png'))
    CHAR22 = pg.image.load(os.path.join('images', 'char2-2.png'))
    CHAR23 = pg.image.load(os.path.join('images', 'char2-3.png'))
    CHAR3 = pg.image.load(os.path.join('images', 'char3.png'))
    CHAR32 = pg.image.load(os.path.join('images', 'char3-2.png'))
    CHAR33 = pg.image.load(os.path.join('images', 'char3-3.png'))
    CHAR4 = pg.image.load(os.path.join('images', 'char4.png'))
    CHAR42 = pg.image.load(os.path.join('images', 'char4-2.png'))
    CHAR43 = pg.image.load(os.path.join('images', 'char4-3.png'))
   
    PRESS_ENTER = pg.image.load(os.path.join('images', 'pressenter.png'))
    MENU_SOUND = pg.mixer.Sound(os.path.join('sounds', 'startsound.wav'))
    BUTTON_NOISE = pg.mixer.Sound(os.path.join('sounds', 'buttonnoise.wav'))
    MENU_SOUND.play
   
    run_it = Control()
    run_it.surface()
    run_it.main_loop()
    pg.quit()
    sys.exit()




This as of now is a menu with 2 buttons that lead to a character with walking anim and controls. One thing i would like to note is that startpageq is just there in lack of a map ATM
Last edited by micseydel on Tue Oct 15, 2013 10:16 pm, edited 1 time in total.
Reason: Got rid of all caps title.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: WHY DONT MY BUTTONS WORK.

Postby ochichinyezaboombwa » Tue Sep 24, 2013 8:07 pm

You need to keep versions of your code to be able to compare what was yesterday to today's version; this is called "Version control"; most popular software to do that is svn or cvs.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: WHY DONT MY BUTTONS WORK.

Postby spamynator_1 » Wed Sep 25, 2013 8:15 pm

Thank you for the advice. =D

Any on my buttons?
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: WHY DONT MY BUTTONS WORK.

Postby spamynator_1 » Tue Oct 15, 2013 7:12 pm

I got an earlier version that had functional buttons, and made sure that everything regarding the buttons in the 2 versions was exactly the same and the new version still doesnt work. i have been searching passively for about a month looking for why the hell these buttons do not work and i cant find any reason. PLEASE HELP
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: Why don't my buttons work?

Postby Mekire » Tue Oct 15, 2013 11:33 pm

Code: Select all
elif event.type == pg.KEYDOWN:
    if event.key == pg.K_RETURN and self.state == "START":
        self.state = "MENU"

    elif self.state == "MENU":
        if event.type == pg.MOUSEBUTTONDOWN:
            if event.button == 1:

You are checking for MOUSEBUTTONDOWN events inside a block that only runs on KEYDOWN events. Therefore the part where you are trying to get mouse input never runs. Also, you might consider the reason that you haven't recieved much help is that you haven't supplied us with a testable example. We don't have any of your graphics or resources so it is very hard to test anything for you.

-Mek
User avatar
Mekire
 
Posts: 977
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Why don't my buttons work?

Postby spamynator_1 » Wed Oct 16, 2013 2:35 pm

What do you mean, how could i make it so it works with mousbutton events instead of keydown events?
And i am sorry about not posting my rescources, but it is very inconvenient to post 20 pngs every time i need coding help.
I do appreciate everybody's help even though i tend to give limited information. I have a version where the buttons work, but it is very old. This is the version that the button works in.

Code: Select all
import sys
import os
import pygame as pg
import pickle
import pygame.mixer
from pygame.locals import *

SCREEN_SIZE = SCREEN_WIDTH,SCREEN_HEIGHT = (640,480)

RED = (255,0,0)
ORANGE = (255,127,0)
YELLOW = (255,255,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
INDIGO = (75,0,130)
VIOLET = (143,0,255)

class Control(object):
    def __init__(self):
        self.screen = pg.display.get_surface()
        self.done = False
        self.state = "START"
        self.clock = pg.time.Clock()
        self.fps = 60
        self.enter_coords = [0,400]
        self.keys = pg.key.get_pressed()
       
    def event_loop(self):
        for event in pg.event.get():
            self.keys = pg.key.get_pressed()
            if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
                self.done = True

            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_RETURN and self.state == "START":
                    self.state = "MENU"

            elif self.state == "MENU":
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        if BUTTON_NEW_rect.collidepoint(mx,my):
                            self.done = True
                   
            elif self.state == "NEW":
                crud = "1-0,0    0"
                sys(exit)
                with open("bruce", "wb") as f:
                    pickle.dump(crud, f)

    def main_loop(self):
        while not self.done:
            self.event_loop()
            self.update()
            pg.display.update()
            self.clock.tick(self.fps)

    def update(self):
        if self.state == "START":
            self.screen.blit(START_PAGE,(0,0))
            self.screen.blit(PRESS_ENTER,self.enter_coords)
            self.enter_coords[0] = (self.enter_coords[0]+2)
        elif self.state == "MENU":
            self.screen.blit(START_PAGE,(0,0))
            self.screen.blit(BUTTON_NEW, BUTTON_NEW_rect)
            self.screen.blit(BUTTON_CONTINUE, BUTTON_CONTINUE_rect)
        elif self.state == "NEW":
            self.screen.blit(START_PAGE, (0,0))
           
if __name__ == "__main__":
    os.environ["SDL_VIDEO_CENTERED"] = '1'
    pg.init()
    pg.display.set_mode(SCREEN_SIZE)

    START_PAGE = pg.image.load(os.path.join('images', 'startpage.png'))
   
    #start of button shit
    BUTTON_NEW = pg.image.load(os.path.join('images', 'buttonnew.png'))
    BUTTON_NEW_rect = BUTTON_NEW.get_rect(center=(160,200))
    BUTTON_CONTINUE = pg.image.load(os.path.join('images', 'buttoncontinue.png'))
    BUTTON_CONTINUE_rect = BUTTON_CONTINUE.get_rect(center=(480,200))
    #end of button shit
    mx,my = pg.mouse.get_pos()
    PRESS_ENTER = pg.image.load(os.path.join('images', 'pressenter.png'))
    MENU_SOUND = pg.mixer.Sound(os.path.join('sounds', 'startsound.wav'))
    BUTTON_NOISE = pg.mixer.Sound(os.path.join('sounds', 'buttonnoise.wav'))
    MENU_SOUND.play

    run_it = Control()
    run_it.main_loop()
    pg.quit()
    sys.exit()
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: Why don't my buttons work?

Postby Mekire » Wed Oct 16, 2013 3:03 pm

You need a structure like this basically:
Code: Select all
for event in pg.event.get():
    if event.type == pg.QUIT:
        self.done = True
    elif event.type == pg.KEYDOWN:
        #Everything pertaining to keypresses
    elif event.type == pg.MOUSEBUTTONDOWN:       
        #Everything pertaining to mouseclicks

spamynator_1 wrote:i am sorry about not posting my rescources, but it is very inconvenient to post 20 pngs every time i need coding help.

This is another reason you need to use a git repo. Learning git is a bit daunting at first (I'm no expert myself), but it will save you so much time and pain in the future.

-Mek
User avatar
Mekire
 
Posts: 977
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Why don't my buttons work?

Postby spamynator_1 » Wed Oct 16, 2013 6:53 pm

Thank you very much! i got it to work, i needed to have it ordered differently.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am


Return to Game Development

Who is online

Users browsing this forum: No registered users and 3 guests