Okay, I'm back. I couldn't find a tutorial on blitting an image as a button so this is what I came up with. Unfortunately, it doesn't work. At least, not the way I planned. It keeps raising an error when I try to use my program.
The error is:
Traceback (most recent call last):
File "N:/Aztlan/aztlan.py", line 8, in <module>
from button import Button as b
File "N:\Aztlan\button.py", line 39
self._index = 0
^
SyntaxError: invalid syntax
I just want to iterate through a list of Button class objects but it's giving me a hard time.
my Button class (in button.py)
import pygame as pg
class Button:
"""
Creates a button
"""
_buttList = []
def __init__(self, x, y, w, h, imgAddr, name):
"""
x and y are ints and
indicate where the top right corner of the button will be
w is an int that
represents how wide the button will be
h is an int that
represents the height of the button
imgAddr is a string that holds the image address on it
it is used to create a pygame image object
name is a string indicating button function
"""
# inits self._image to pygame image object
self._image = pg.image.load(imgAddr)
#pygame x y coordinates
self._x = x
self._y = y
#width and height of button
self._w = w
self._h = h
self._buttList = []
self._buttList.append((self._x, self._y, self._w, self._h)
self._index = 0
def draw(self, surf):
"""
draws a button the window
:param surf:
surf is the pygame surface window the button is drawn to
"""
# draws button to screen
surf.blit(self._image, ((self._x, self._y), (self._w, self._h)))
def overButton(self, pos):
"""
checks where the mouse is
:param pos:
pygame mouse object that indicates where the mouse is
"""
return self._x <= pos[0] and self._x + self._w > pos[0] and \
self._y <= pos[1] and self._y + self._h > pos[1]
def __iter__(self):
return self
def __next__(self):
try:
result = self._buttList[self._index]
except IndexError:
raise StopIteration
self._index += 1
return result
def __getitem__(self, index):
"""
:param index:
int that finds proper index
"""
return Button._buttList[index]
my MainMenu class (in screens.py)
"""
Contains classes to handle the various screens of the game
"""
__author__ = "RidiculousName"
__date__ = "4/22/17"
#imports Button class
from button import Button as b
import pygame as pg
class MainMenu:
"""
Class that handles the main menu screen
"""
def __init__(self, surf):
"""
surf is a pygame.surface object that
the buttons, images, etc. are blitted to
isClick is a pygame mouse object that
determines whether the mouse has been clicked
"""
# where main menu images are located
self._i = "images/mainMenu/"
self._surf = surf
self._surf.fill((0,0,0)) #fill surface with white
#list of main menu buttons
# x value, y value, width, height, image path, surface, button type
self._mmList = [
b(150, 300, 200, 80, self._i + "mmResume.png",
"Resume"),
b(150, 400, 200, 80, self._i + "mmStart.png",
"Start"),
b(150, 500, 200, 80, self._i + "mmLoad.png",
"Load"),
b(150, 600, 200, 80, self._i + "mmCreate.png",
"Create"),
b(150, 700, 200, 80, self._i + "mmExit.png",
"Exit"),
b(600, 100, 400, 160, self._i + "mmTitle.png",
"Title")
]
# blits buttons to screen
for ei in self._mmList:
b.draw(ei,surf)
def butts(self, isClick):
if isClick[0] == True:
rat = pg.mouse.get_pos()
for ei in self._mmList:
for item in ei:
# if button-X + button Width > mouse-X > button-X
# if button-Y + button Width > mouse-Y > button-Y
print(ei[1])
print(type(ei[1]))
print(self._mmList)
if ei[0] + ei[2] > rat[0] > ei[0] \
and ei[1] + ei[3] > rat[1] > ei[1]:
if ei[-1] == "Exit":
return pg.QUIT
my main (put it all together) file: (in aztlan.py)
"""
Combines classes to run the game
"""
__author__ = "RidiculousName"
__Date__ = "4/22/17"
import pygame as pg
from button import Button as b
from screens import *
def main():
""" set up the game and run the main game loop """
pg.init() # prepare pygame module for use
surfW = 1600 # window width
surfH = 900 # window height
# create window
surf = pg.display.set_mode((surfW, surfH))
#set window caption
pg.display.set_caption("Aztlan")
while True:
#opens first screen
mm = MainMenu(surf)
mm.butts(pg.mouse.get_pressed())
ev = pg.event.poll() # look for any event
if ev.type == pg.QUIT: # window close button clicked?
break
pg.display.flip()
pg.quit()
if __name__ == "__main__":
main()