Help

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Help

Post by R_I_P1337 on Thu Aug 21, 2008 5:35 am
([msg=10182]see Help[/msg])

Could anyone tell me what is wrong with this program. Here is the function that keeps getting error messages.

Code: Select all
while run:
   for event in pygame.event.get():
       
      if event.type == QUIT:
              return
        if event.type == KEYDOWN:
         run = handle_key( event.key, player )


Here is the error message I get:
Code: Select all
File "bouncy.py", line 47
    return
SyntaxError: 'return' outside function


If I put the return anywhere else it comes up with it does not match any other indentation level.

Any help would be appreciated :)

R.I.P
Last edited by R_I_P1337 on Fri Aug 29, 2008 3:26 am, edited 1 time in total.
R_I_P1337
New User
New User
 
Posts: 17
Joined: Thu Jun 19, 2008 2:21 pm
Blog: View Blog (0)


Re: Help

Post by AbaxHacker on Thu Aug 21, 2008 6:51 pm
([msg=10220]see Re: Help[/msg])

I have done some perl/python unless return needs to pass a varible, try making it post a message, it also seems it thinks you want to return out of a function not the game.
AbaxHacker
New User
New User
 
Posts: 17
Joined: Tue Aug 19, 2008 6:31 pm
Blog: View Blog (0)


Re: Help

Post by R_I_P1337 on Sun Aug 24, 2008 10:12 am
([msg=10364]see Re: Help[/msg])

It needs to pass a varible
R_I_P1337
New User
New User
 
Posts: 17
Joined: Thu Jun 19, 2008 2:21 pm
Blog: View Blog (0)


Re: Help

Post by tgoe on Mon Oct 06, 2008 11:45 pm
([msg=13295]see Re: Help[/msg])

FWIW, I'm 99% sure this is just a case of mixing tabs and spaces while indenting.
The above code should be:

Code: Select all
while run:
    for event in pygame.event.get():
        if event.type == QUIT:
            return
        if event.type == KEYDOWN:
            run = handle_key( event.key, player )
User avatar
tgoe
Contributor
Contributor
 
Posts: 643
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Help

Post by R_I_P1337 on Sun Oct 19, 2008 8:43 am
([msg=13897]see Re: Help[/msg])

That was obvious im so stupid :oops: thanks alot

Edit: no that still hasnt worked :evil:
R_I_P1337
New User
New User
 
Posts: 17
Joined: Thu Jun 19, 2008 2:21 pm
Blog: View Blog (0)


Re: Help

Post by tgoe on Sat Nov 15, 2008 8:05 pm
([msg=15069]see Re: Help[/msg])

Make sure all the code in the file is using either tabs or spaces.
Has the error message changed?
User avatar
tgoe
Contributor
Contributor
 
Posts: 643
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Help

Post by Qahwah on Sun Nov 16, 2008 2:44 am
([msg=15082]see Re: Help[/msg])

Uh, just from the snippet you posted, your problem isn't an indentation one. Return statements only belong inside functions, and unless you omitted the def function(): line, that isn't one. You need to either move that while statement inside a function, or replace the return with something else, like exit() or quit().

Can you post the whole code? From what I'm seeing it does have indentation errors, and you need to fix them like tgoe suggested, but that may or may not be what's causing this specific problem.
Qahwah
New User
New User
 
Posts: 3
Joined: Sun Nov 16, 2008 2:19 am
Blog: View Blog (0)


Re: Help

Post by R_I_P1337 on Wed Nov 19, 2008 1:06 pm
([msg=15212]see Re: Help[/msg])

Here is the whole of the code:
Code: Select all
import math
import random
import sys
import time

import pygame
from   pygame.locals import *

def main():
    pygame.init()
    pygame.display.set_caption( 'Bouncy Robot Escape')

    max_x       =640
    max_y       =480
   
    screen      = pygame.display.set_mode(( max_x, max_y ))
    background  = pygame.Surface( screen.get_size() ).convert()
   
    player      =Player( 'robot.png', max_x, max_y )
    door        =Door(   'door.png',  max_x, max_y )
   
    balls       = []
    ball_images =[ 'ball_blue.png' , 'ball_red.png' , 'ball_yellow.png' ]
    rand_ball   = random.random()
   
    for i in range( random.randit( 3,7 ) ):
   ball_image = ball_images[ rand_ball.\
   randit( 0, len(ball_images) -1 ) ]
   balls.append( Ball( ball_image, max_x, max_y ) )
   
    loop( screen, background, player, balls, door )
   
def loop( screen, background, player, balls, door ):
      run           = 1
      player_turn   = 1
      start_time    = time.time()
      

while run:
    for event in pygame.event.get():
        if event.type == QUIT:
            return
        if event.type == KEYDOWN:
            run = handle_key( event.key, player )

draw_background( background, screen )

if door.visible:
   door.draw( background )
elif time.time() - start_time > 1.0:
   door.visible = 1

for ball in balls:
   ball.move()
   ball.draw( background )

if player_turn == 1:
   player.move()
   player_turn = 0
else:
   player_turn = 1

player.draw( background )

handle_collisions( screen, player, balls, door )
screen.blit( background, ( 0, 0 ))
pygame.display.flip()

def draw_background( background, surface ):
   background.fill(( 255, 255, 255 ))
   surface.blit( background, background.get_rect() )

def handle_key( key, player ):
   key_events = {
         K_UP:   player.move_up,
         K_DOWN:   player.move_down,
         K_LEFT:   player.move_left,
         K_RIGHT:player.move_right,
         K_SPACE:palyer.enable_shield
      }

   if key_events.has_key( key ):
      key_events[ key ]()
   elif key == K_ESCAPE:
      return 0

   return 1

def handle_collisions( screen, player, balls, door ):
   for ball_count in range( len(balls) ):
      ball = balls[ball_count]

      if ball.collision( player ):
         if player.shield:
            ball.bounce()

         else:

            game_over( screen )

for other_ball in balls[ ball_count: ]:
   if ball.collision( other_ball ):
      ball.bounce()
      other_ball.bounce()

if door.visible and ball.collision( door ):
   ball.bouce()

if door.visible and door.collision( player ):
   win_game( screen )

class GameObject:
   def place_random( self ):
      self.rect.x = random.randit( 0, self.max_x )
      self.rect.y = random.randit( 0, self.max_y )

   def draw( self, surface ):
      surface.blit( self.image, seld.rect )

   def calc_radius( self ):
      half_width  = self.rect.width /2
      half_height = self.rect.height /2

      return int( math.hypot( half_height, half_width ) )

   def center_point( self ):
      x_offset = int( self.rect.width /2 )
      y_offset = int( self.rect.height /2 )
      return [x_offset + self.rect.x, y_offset + self.rect.y ]

   def collision( self, other ):
      my_center    = self.center_point()
      other_center = other.cener_point()

      delta_x      =my_center[0] - other_center[0]
      delta_y        =my_center[1] - other_center[1]
      distance     = int( math.hypot( delta_x, delta_y ) )

      if distance < self.radius or distance < other.radius:
         return distance

      return 0

class Player( GameObject ):
   def __init__ ( self, image, max_x, max_y, ):
      self.image      = pygame.image.load( image ).convert_alpha()
      self.sheild_img = pygame.image.load( 'force_feild.png' ).\
            convert_alpha()
            
      self.rect = self.image.get_rect()
      self.max_x = max_x - self.rect.width
      self.max_y = max_y - self.rect.height
      self.x_vel = 0
      self.y_vel = 0
      self.sheild = 1
      self.radius = self.calc_radius()


      self.place_random()


   def enable_shield( self ):
      self.x_vel  = 0
      self.y_vel  = 0
      self.shield = 1

   def move( self ):
       if self.x_vel or self.y_vel:
          self.shield = 0

       if ( self.x_vel and 0 < self.rect.x < self.max_x ):
      self.rect.x += self.x_vel

       if ( self.y_vel and 0 < self.rect.y < self.max_y ):
      self.rect.y += self.y_vel

   def move_up( self ):
            self.y_vel = -1

   def move_down( self ):
       self.y_vel = +1

   def move_left( self ):
            self.x_vel = -1

   def move_right( self ):                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
       self.x_vel = +1

   def draw( self, surface ):
       surface.blit( self.image, self.rect )
       if self.shield:
          surface.blit( self.shield_img, self.rect )

class Ball( GameObject ):
   def __init__( self, image, max_x, max_y ):
      self.image  = pygame.image.load( image ).convert_alpha()
      self.rect   = self.image.get_rect()
      self.max_x  = max_x - self.rect.width
      self.max_y  = max_y - self.rect.height
      self.x_vel  = random.randit( -1, 1 )
      self.y_vel  = random.randit( -1, 1 )
      self.radius = self.calc_radius()


      self.place_random()

   def move( self ):
      x = self.rect.x + self.x_vel


      if x < 0:
         x      = 0
         self.x_vel = self.x_vel
      elif x > self.max_x:
         x          = self.max_x
         self.x_vel = -self.x_vel

      y = self.rect.y + self.y_vel


      if y < 0:
         y       = 0
         self.y_vel = -self.y_vel
      elif y > self .max_y:
         y          = self.max_y
         self.y_vel = -self.y_vel

      self.rect.x = x
      self.rect.y = y


   def bounce( self ):
      self.x_vel = -self.x_vel
      self.y_vel = -self.y_vel

class Door( GameObject ):
   def__init__( self, iamge, max_x, max_y )
   self.image   = pygame.image.load( image ).convert_alpha()
   self.rect    = self.image.get.rect()
   self.max_x   = max_x - self.rect.width
   self.max_y   = max_y - self.rect.height
   self.visible = 0
   self.radius  = se;f.calc_radius()


   self.place_random()

   def game_over( screen ):
      write( screen, "BONK! Game over. So sorry." )
      time.sleep( 2 )
      sus.exit()


   def win_game( screen ):
      write( screen, "YOU WIN! Game over. What a let down." )
      time.sleep( 2 )
      sys.exit()

   def write( screen, message ):
      font              = pygame.font.Font( None, 36 )
      text              = font.render( message, 1, (0, 0, 255))
      text_rect         = text.get_rect()
      text_rect.centerx = screen.get_rect().centerx
      screen.blit( text_rect )
      pygame.display.flip()


if __name__ == '__main__':main()
R_I_P1337
New User
New User
 
Posts: 17
Joined: Thu Jun 19, 2008 2:21 pm
Blog: View Blog (0)


Re: Help

Post by Qahwah on Thu Nov 20, 2008 11:11 pm
([msg=15261]see Re: Help[/msg])

Woah. Yeah, that code has some problems.

tgoe's right, this is an indent error. That whole block of code needs to be indented so that it's inside the loop() function. The code still won't run, mind - there are a bunch of indentation errors throughout, and you need to fix all of them for it to work.

Moreover, the formatting of the program is pretty screwy. You call some functions before they're defined (I'm looking at draw_background in line 46) and just overall a lot of stuff is defined in the wrong places. Functions and classes go at the top of the program, after the import statements but before everything else. Also, you reference variables that aren't declared anywhere (again with draw_background() - it uses the background variable, which you declare in main(), but you don't call it in main). There are a bunch of typos that are screwing up your code - field misspelled as feild, images misspelled misspelled as iamges, randint misspelled as 'randit' more times than I can count. I was going to repost your code with comments on all the lines that needed to be fixed, but honestly there are just too many of them for that to be practical. All I can say is, review your basic knowledge of Python, and proofread, man, proofread. You shouldn't be writing your code in one big 200-line chunk, you should be writing it piece by piece, and testing as you go.

I'd recommend scrapping all that code, and starting fresh. Start with something small and build on it until you have a full-fledged game. If you do decide to fix that program (which will probably actually take longer) and you have any specific questions, you can still ask, though there are other resources you should check first.
Qahwah
New User
New User
 
Posts: 3
Joined: Sun Nov 16, 2008 2:19 am
Blog: View Blog (0)


Re: Help

Post by R_I_P1337 on Fri Nov 21, 2008 1:45 pm
([msg=15282]see Re: Help[/msg])

Yeah, thats a really big problem I have. Ive haven't got any patience and think I can do things before I actually can. I think I can walk before i can even crawl as someone once said. Ive been told I need to read what ive actually put and not what i think of put, I am learning slowly though. Thanks for the advice really appreciated.
R_I_P1337
New User
New User
 
Posts: 17
Joined: Thu Jun 19, 2008 2:21 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests