import pygame
from pygame.locals import *
class Box:
def __init__(self, screen, size, velocities, background, boxcolor):
self.screen = screen
screensize = self.screen.get_size()
self.screenwidth = screensize[0]
self.screenheight = screensize[1]
# Position of Box on the Screen
# Box will start roughly in the middle of the screen.
self.x = screensize[0]/2
self.y = screensize[1]/2
self.width = size[0]
self.height = size[1]
# Velocity of the box
self.vx = velocities[0]
self.vy = velocities[1]
self.bgcolor = background
self.boxcolor = boxcolor
self.rect = pygame.rect.Rect(self.x, self.y, self.width, self.height)
def draw(self):
# Erase the previous box
pygame.draw.rect( self.screen, self.bgcolor, self.rect )
# Update position or reverse direction
# Check for collision with the sides:
nx, ny = self.x + self.vx, self.y + self.vy
bound_x = nx + self.width
bound_y = ny + self.height
if( (bound_x >= self.screenwidth) or
(nx <= 0) ):
self.vx *= -1 * 0.9 # Bounces decrease velocity slightly
else:
self.x = nx
if( (bound_y >= self.screenheight) or
(ny <= 0 ) ):
self.vy *= -1 * 0.9
else:
self.y = ny
# Draw the new box
self.rect = pygame.rect.Rect(nx, ny, self.width, self.height)
pygame.draw.rect( self.screen, self.boxcolor, self.rect )
def setV(self,x,y):
self.vx = x
self.vy = y
def setBackgroundColor(self, color):
self.bgcolor=color
def setBoxColor(self, color):
self.boxcolor=color
__init__ in Classes (Python)
This is a class of a box that bounces around the screen.
According to the code, you can assign the class by:
box1 = box.Box( screen, (32,32), (1,1), THECOLORS["black"], THECOLORS["white"] )
First of all, I don't understand what __init__ does in the class.
Secondly, how is the order of the parameters for the above code work? Does it find the order of the functions in the class and parse them automatically?
Yes, I know near nothing about classes :D
Thanks in advance for any help!
(BTW code is from this page http://www.penzilla.net/tutorials/python/pygame/ )
when you do:
box.Box( screen, (32,32), (1,1), THECOLORS["black"], THECOLORS["white"] )
python calls the __init__ function on the class, it is like a constructor in C++ (if you have knowledge of C++)
the order of the paramerters are the same as the order defined in the function
class Box:
def __init__(self, screen, size, velocities, background, boxcolor):
so it expects the following:
screen
size
velocities
background
boxcolour
in that order, which seems to be whats passed into it here:
box.Box( screen, (32,32), (1,1), THECOLORS["black"], THECOLORS["white"] )
box.Box( screen, (32,32), (1,1), THECOLORS["black"], THECOLORS["white"] )
python calls the __init__ function on the class, it is like a constructor in C++ (if you have knowledge of C++)
the order of the paramerters are the same as the order defined in the function
class Box:
def __init__(self, screen, size, velocities, background, boxcolor):
so it expects the following:
screen
size
velocities
background
boxcolour
in that order, which seems to be whats passed into it here:
box.Box( screen, (32,32), (1,1), THECOLORS["black"], THECOLORS["white"] )
Its just a function thats called by python when you create an instance of the class, in it you can set up variables to default values and stuff like that.
Check out http://www.penzilla.net/tutorials/python/classes/ for more info :)
Check out http://www.penzilla.net/tutorials/python/classes/ for more info :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement