I have python 3.0 and im trying to get this code to work but I don't believe it is recognizing all the attributes it's supposed to.

import pygame, sys, random 
from pygame.locals import * 
#*******************************************SETUPVAR**************************************************
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0)
GREEN = (0, 255, 0) 
BLUE = (0, 0, 255) 
ORANGE = (255,180,0)
YELLOW=(230,230,0)
#***********************************************DEFINE*************************************************
def newscreen():
    pygame.init() 
    mainClock = pygame.time.Clock()
    windowSurface = pygame.display.set_mode((700,600), 0, 32) 
    pygame.display.set_caption('test')
    BLACK = (0, 0, 0) 
    WHITE = (255, 255, 255) 
    RED = (255, 0, 0) 
    GREEN = (0, 255, 0) 
    BLUE = (0, 0, 255) 
    ORANGE = (255,180,0)
    YELLOW=(230,230,0)
    basicFont = pygame.font.SysFont(None, 48)
    Font1 = pygame.font.SysFont('ActionIsShaded', 25)
    windowSurface.fill(BLACK)
    text = basicFont.render ('test,False,WHITE,BLUE)
    textRect = text.get_rect()
    textRect.centerx = windowSurface.get_rect().centerx
    textRect.centery = windowSurface.get_rect().centery-250
    windowSurface.blit(text,textRect)
    pygame.display.update()
    return windowSurface
    return event

def menu():
    windowSurface = newscreen()
    event = newscreen()
    pygame.init()
    basicFont = pygame.font.SysFont(None, 48)
    Font1 = pygame.font.SysFont('ActionIsShaded', 25)
    1 = basicFont.render('1', False, WHITE, RED) 
    2 = basicFont.render('2',False,WHITE,GREEN)
    3 = basicFont.render('3',False,WHITE,ORANGE)
    4 = basicFont.render('4',False,WHITE,YELLOW)
    3Rect = vl.get_rect()
    3Rect.centerx = windowSurface.get_rect().centerx+200
    3Rect.centery = windowSurface.get_rect().centery-100
    2Rect = games.get_rect()
    2Rect.centerx = windowSurface.get_rect().centerx-200
    2Rect.centery = windowSurface.get_rect().centery-100
    1Rect = text.get_rect() 
    1Rect.centerx = windowSurface.get_rect().centerx +200
    1Rect.centery = windowSurface.get_rect().centery+150
    1Rect.size = (100,100)
    4Rect = end.get_rect()
    4Rect.centerx = windowSurface.get_rect().centerx-200
    4Rect.centery = windowSurface.get_rect().centery+150
    windowSurface.blit(1, 1Rect) 
    windowSurface.blit(2,2Rect)
    windowSurface.blit(3,3Rect)
    windowSurface.blit(4,4Rect)
    pygame.display.update()
    #keepgoing = True
    while True: 
    # check for events 
        for event in pygame.event.get(): 
            if event.type == QUIT: 
                pygame.quit() 
                sys.exit() 
            if event.type == KEYUP: 
                if event.key == K_ESCAPE: 
                    pygame.quit() 
                    sys.exit()  
            if event.key == K_RIGHT or event.key == ord('d'): 
                print('yes') 
            if event.key == ord('x'): 
                print('correct')
            if event.type == MOUSEBUTTONUP:
                M = pygame.mouse.get_pos
                if M == textRect.collidepoint:
                    newscreen()
    
#***********************************************START***************************************************
newscreen()
menu()


# run the game loop 
while True: 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            pygame.quit() 
            sys.exit()

Edited 6 Years Ago by wolfeater017: n/a

Hate to say it but there are a lot of problems with your code, but you can only learn from mistakes. First I'm going to address the syntax errors, which should be easy to fix from looking at what the red error text says in your console.

Line 27

text = basicFont.render ('test,False,WHITE,BLUE)

test is a string and has to be enclosed in single or double quotes.

text = basicFont.render ('test',False,WHITE,BLUE) #FIXED

Line 42

1 = basicFont.render('1', False, WHITE, RED)

1 is a variable and variables can not start or be solely a number, change it to something that describes what that varible is going to be used for.

fontSurface1 = basicFont.render('1', False, WHITE, RED) #fontSurface1 works as now you know this is a font surface

**Line line 42 to 58 all have the same problem with a number as the starting character for the variable, which all have to be changed.

Line 33

return windowSurface
return event

A method can not have more than one return statement, both of these variables can be returned as a tuple though.

return windowSurface, event

Line 46

3Rect = vl.get_rect()

If vl is a class that you have written can you please post the code so that we can help with debugging of the rest of the program. Also for games and end.

If you have any more questions just ask.

what if I keep getting this back

Traceback (most recent call last):
  File "C:\Python31\pysoccer.py", line 102, in <module>
    menu()
  File "C:\Python31\pysoccer.py", line 91, in menu
    if event.key == K_RIGHT or event.key == ord('d'):
AttributeError: event member not defined

See this comment is tbone2sk's reply above.

Line 33
Python Syntax (Toggle Plain Text)
return windowSurface
return event
A method can not have more than one return statement, both of these variables can be returned as a tuple though.

The first culprit to check on is to see if "event" is returned because the second return will never execute. Add something like this to statements that expect to receive "execute" from a function.

def menu():
    windowSurface = newscreen()
    event = newscreen()
    print event, type(event)

If you get "None" then something is wrong. I would suggest that you break this program up into small segments/functions and test each one individually before you do any more coding.

Edited 6 Years Ago by woooee: n/a

woooee is correct in seeing if you even need to return event in the newscreen method, but your problem is not only there but in your event loop. Your program will be more efficient if you use this event method to handle key inputs.

def keyPressed(inputKey):
    keysPressed = pygame.key.get_pressed()
    if keysPressed[inputKey]:
        return True
    else:
        return False

Then in your program change the event loop to something more like this.

for event in pygame.event.get(): 
            if event.type == QUIT: 
                pygame.quit() 
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                M = pygame.mouse.get_pos
                if M == textRect.collidepoint:
                    newscreen()
        if keyPressed(K_ESCAPE): 
                pygame.quit() 
                sys.exit()  
        elif keyPressed(K_RIGHT) or keyPressed(K_d): 
            print('yes') 
        elif keyPressed(K_x): 
            print('correct')

Your program still has many problems after this.

I turned the menu function into a class and it seems to be working better but I really don't no what a class is

This article has been dead for over six months. Start a new discussion instead.