Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Python

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help!
View previous topic :: View next topic  
Author Message
RyanF109



Joined: Mon Jan 02, 2012 7:53 pm
Posts: 16

PostPosted: Mon Oct 22, 2012 3:55 pm    Post subject: Python Reply with quote

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:

#/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]
Back to top
View user's profile Send private message
RyanF109



Joined: Mon Jan 02, 2012 7:53 pm
Posts: 16

PostPosted: Mon Oct 22, 2012 10:54 pm    Post subject: Reply with quote

I fixed it. It was an indentation error. I'm not used to indentation being so important!
Back to top
View user's profile Send private message
lok1950
LXF regular


Joined: Tue May 31, 2005 6:31 am
Posts: 1016
Location: Ottawa

PostPosted: Mon Oct 22, 2012 11:44 pm    Post subject: Reply with quote

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

Enjoy the Choice Smile
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help! All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast