Python

The place to post if you need help or advice

Moderators: ChrisThornett, LXF moderators

Python

Postby RyanF109 » Mon Oct 22, 2012 2:55 pm

I have kept all of my coding academy tutorials and today I started the process of working my way through them.

I decided to play around with python and I am trying to create code that generates a random colour for the screen fill every time you click the mouse. But after spending a while fixing indentation errors now when I run it, it just quits with no error, as if one of the returns are being executed. Can anybody see any obvious errors in my code?

Code: Select all
#/usr/bin/python

#import modules
import pygame
import os,random

#define colour settings class
class colourset():

   def __init__(self):
      """ colour settings """


   def gencolour(self):

      self.r = random.randint(0,255)
      self.g = random.randint(0,255)
      self.b = random.randint(0,255)
      return self

   def colourscreen(self,colour):
      self.screensize = (800,600)
          self.screen = pygame.display.set_mode(self.screensize)
          pygame.display.set_caption('Random Colour')

          self.backdrop = pygame.Surface(self.screensize)
          self.backdrop.fill((colour.r,colour.g,colour.b))

      #refresh the screen by drawing everything again
      #redrawthe background
      self.screen.blit(self.backdrop, (0, 0))

      #flip the buffer
      pygame.display.flip()

def main():
   """the main game logic"""

   #Initialize Everything
   pygame.init()

   colourinst = colourset()
   colour = colourinst.gencolour()
   colourinst.colourscreen(colour)
   clock = pygame.time.Clock()

   # control loop

   while 1:
      #adjust this timer to make the game harder or easier
      clock.tick(130)

      #check what events Pygame has caught
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
         return
          elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
         return
          elif event.type == pygame.MOUSEBUTTONDOWN:
         #button means mouse click
         colourinst = colourset()
         colour = colourinst.gencolour()
         colourinst.colourscreen(colour)
         clock = pygame.time.Clock()

   if __name__ == '__main__':
          main()
          pygame.quit()
[/code]
RyanF109
 
Posts: 16
Joined: Mon Jan 02, 2012 7:53 pm

Postby RyanF109 » Mon Oct 22, 2012 9:54 pm

I fixed it. It was an indentation error. I'm not used to indentation being so important!
RyanF109
 
Posts: 16
Joined: Mon Jan 02, 2012 7:53 pm

Postby lok1950 » Mon Oct 22, 2012 10:44 pm

If you are using gedit or kwrite you can set syntax checking for a variety of languages.

Enjoy the Choice :)
User avatar
lok1950
LXF regular
 
Posts: 1044
Joined: Tue May 31, 2005 5:31 am
Location: Ottawa


Return to Help!

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron