 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
RyanF109
Joined: Mon Jan 02, 2012 7:53 pm Posts: 11
|
Posted: Mon Oct 22, 2012 3:55 pm Post subject: Python |
|
|
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 |
|
 |
RyanF109
Joined: Mon Jan 02, 2012 7:53 pm Posts: 11
|
Posted: Mon Oct 22, 2012 10:54 pm Post subject: |
|
|
| I fixed it. It was an indentation error. I'm not used to indentation being so important! |
|
| Back to top |
|
 |
lok1950 LXF regular

Joined: Tue May 31, 2005 6:31 am Posts: 958 Location: Ottawa
|
Posted: Mon Oct 22, 2012 11:44 pm Post subject: |
|
|
If you are using gedit or kwrite you can set syntax checking for a variety of languages.
Enjoy the Choice  |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|