School Alpha Program *NEED HELP*
Author |
Message |
matt4521
|
Posted: Tue Sep 18, 2012 9:35 am Post subject: School Alpha Program *NEED HELP* |
|
|
Me and my partner have been working on this error for hours.... when calling the allsprites update function it gives us a argument error. If someone could point us in the right direction or even just boldly tell us where we are going wrong that will greatly be appreciated!!
*********************************************************************
Traceback (most recent call last):
File "F:\Alpha\alpha program.py", line 137, in <module>
allsprites.update()
File "C:\Python32\lib\site-packages\pygame\sprite.py", line 462, in update
s.update(*args)
TypeError: update() takes exactly 2 arguments (1 given)
import pygame,sys,os, time
from pygame.locals import *
*********************************************************************
Python: |
def initPygame():
pygame.init()#initailizes pygame
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'#opens window in the center of the screen
pygame.mouse.set_visible(0)#mouse is invisible
class backgroundClass(pygame.sprite.Sprite):
def __init__(self):
#declares backgroundfile as variable
backgroundfile = "background.png"
#loads background file
self.image = pygame.image.load(backgroundfile).convert()
def draw(self):
#copys inmage to the screen
screen.blit(self.image,(0,0))
class blackcircleClass(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
blackcirclefile = "sprite1.png"
self.image = pygame.image.load(blackcirclefile).convert_alpha()
self.rect = self.image.get_rect()
def update(self):
position = pygame.mouse.get_pos()
self.rect.center = position
'''class hitClass(pygame.sprite.Sprite):
def __init__(self):
self.coll = False
self.font = pygame.font.SysFont(None,36)
def update(self):
text = self.font.render("OMG COLLISION!%s" %self.coll, True, (0, 0 ,0))
textRect = text.get_rect()
screenrect = screen.get_rect()
textRect.centerx = 290
screen.blit(text,textRect)'''
class redcircleClass(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
redcirclefile = ("sprite2.png")
self.screen = pygame.display.get_surface().get_rect()
self.image = pygame.image.load(redcirclefile).convert_alpha()
self.old = (0, 0, 0, 0)
self.rect = self.image.get_rect()
self.rect.x = position[0]
self.rect.y = position[1]
def update(self, amount):
# Make a copy of the current rectangle for use in erasing
self.old = self.rect
# Move the rectangle by the specified amount
self.rect = self.rect.move(amount)
# Check to see if we are off the screen
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.x > (self.screen.width - self.rect.width):
self.rect.x = self.screen.width - self.rect.width
if self.rect.y < 0:
self.rect.y = 0
elif self.rect.y > (self.screen.height - self.rect.height):
self.rect.y = self.screen.height - self.rect.height
def eventHandling():
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
redcircle.update([-10, 0])
elif event.key == pygame.K_w:
redcircle.update([0, -10])
elif event.key == pygame.K_d:
redcircle.update([10, 0])
elif event.key == pygame.K_s:
redcircle.update([0, 10])
size =(640,512)
screen = pygame.display.set_mode((size))
framespersecond = 20
print ("FPS: ", framespersecond)
clock = pygame.time.Clock()
#text = HitClass()
background = backgroundClass()
blackcircle = blackcircleClass()
redcircle = redcircleClass((screen.get_rect().x, screen.get_rect().y))
allsprites = pygame.sprite.RenderPlain(blackcircle, redcircle)
initPygame()
while(True):
clock.tick(framespersecond)
eventHandling()
background.draw()
allsprites.update()
allsprites.draw(screen)
pygame.display.update()
|
Mod Edit:
Pleas wrap you code in either of the following in order to preserve whitespace to highlight the syntax.
code: |
[syntax="python"] ... code ... [/syntax]
[code] ... code ... [/code ]
|
Description: |
|
Filesize: |
577 Bytes |
Viewed: |
5119 Time(s) |
|
Description: |
|
Filesize: |
590 Bytes |
Viewed: |
5119 Time(s) |
|
Description: |
|
Filesize: |
11.07 KB |
Viewed: |
5119 Time(s) |
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Wed Sep 19, 2012 8:12 pm Post subject: Re: School Alpha Program *NEED HELP* |
|
|
I'm going to guess that pygame.sprite.RenderPlain is a sprite group.
http://www.pygame.org/docs/ref/sprite.html#Group.update
I'm also going to guess that redcircle.update([0, 10]), which requires 2 arguments (self, move) are not being supplied when you call allsprites.update(). Suggestion: your input section shouldn't call update() but instead a new method called move(x, y) which will call whatever movement code. You can improve on this design a velocity variable. Improve on that with acceleration, and so on until you've a amateur physics engine.
|
|
|
|
|
|
matt4521
|
Posted: Thu Sep 20, 2012 9:47 am Post subject: Re: School Alpha Program *NEED HELP* |
|
|
OK so we have made a few changes and things have started to work... but now were attempting to print Text to the screen and we can't seem to get it working.... this is our updated code to date...
Python: |
import pygame,sys,os, time
from pygame.locals import *
def initPygame():
pygame.init()#initailizes pygame
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'#opens window in the center of the screen
pygame.mouse.set_visible(0)#mouse is invisible
class Direction:
#class for easy reference to directions
right = 1
left = 2
up = 3
down = 4
class backgroundClass:
def __init__(self):
#declares backgroundfile as variable
backgroundfile = "background.png"
#loads background file
self.image = pygame.image.load(backgroundfile).convert()
def draw(self):
#copys inmage to the screen
screen.blit(self.image,(0,0))
class blackcircleClass(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
blackcirclefile = "sprite1.png"
self.image = pygame.image.load(blackcirclefile).convert_alpha()
self.rect = self.image.get_rect()
def update(self):
position = pygame.mouse.get_pos()
self.rect.center = position
class hitClass(pygame.sprite.Sprite):
def __init__(self):
pygame.font.init()
return None
self.font = pygame.font.Font(None,36)
self.collide = False
def update(self):
text = pygame.font.render = ("Collision:", self.collide)
textRect = text.get_rect()
textRect.centerx = 290
screen.blit(text,textRect.centerx)
def collide(self):
hit = redcircle.rect.collidepoint(blackcircle.rect.centerx, blackcircle.rect.centery)
if hit == True:
self.collide = True
print ("hit")
return hit
class redcircleClass(pygame.sprite.Sprite):
def __init__(self, screenRect, x = 0, y = 0, direction = Direction.right):
pygame.sprite.Sprite.__init__(self)
self.direction = direction
redcirclefile = "sprite2.png"
self.image= pygame.image.load(redcirclefile).convert_alpha()
screenrect = screen.get_rect()
self.screenRect = screenrect
#gets rectangle
self.rect = self.image.get_rect()
self.rect = self.rect.move(x,y)
self.speed = (20)
def update(self):
'''Code to move this segment'''
'''Set a result variable we can return at the end'''
result = True
'''If this segment is at any of the edges of the screen, set result to false'''
if self.rect.x >= (self.screenRect.width - self.rect.width) and self.direction == Direction.right:
result = False
elif self.rect.y >= (self.screenRect.height - self.rect.height) and self.direction == Direction.down:
result = False
elif self.rect.x <= 0 and self.direction == Direction.left:
result = False
elif self.rect.y <= 0 and self.direction == Direction.up:
result = False
'''If result = true then this segment isn't at the edge, so move according to
what our direction is'''
if result == True:
if self.direction == Direction.up:
self.rect = self.rect.move(0, -self.speed)
elif self.direction == Direction.down:
self.rect = self.rect.move(0, self.speed)
elif self.direction == Direction.right:
self.rect = self.rect.move(self.speed, 0)
elif self.direction == Direction.left:
self.rect = self.rect.move(-self.speed, 0)
'''Return our result'''
return result
def eventHandling():
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
'''If we do then check which one and set the snakes direction'''
elif event.key == K_RIGHT:
redcircle.direction = Direction.right
elif event.key == K_LEFT:
redcircle.direction = Direction.left
elif event.key == K_DOWN:
redcircle.direction = Direction.down
elif event.key == K_UP:
redcircle.direction = Direction.up
size =(640,512)
screen = pygame.display.set_mode((size))
framespersecond = 20
print ("Frames/Sec:", framespersecond)
clock = pygame.time.Clock()
words = hitClass()
background = backgroundClass()
blackcircle = blackcircleClass()
redcircle = redcircleClass(70,10)
allsprites = pygame.sprite.RenderPlain((blackcircle, redcircle))
initPygame()
while(True):
clock.tick(framespersecond)
eventHandling()
background.draw()
#checks if theres a collsion
words.collide()
#updates the text
words.update()
allsprites.update()
allsprites.draw(screen)
pygame.display.update()
|
|
|
|
|
|
|
Zren
|
Posted: Thu Sep 20, 2012 7:41 pm Post subject: RE:School Alpha Program *NEED HELP* |
|
|
Traceback
<module> C:\Users\Admin\Desktop\New folder (3)\module1.py 168
update C:\Users\Admin\Desktop\New folder (3)\module1.py 59
AttributeError: 'tuple' object has no attribute 'get_rect'
Error on this line: text = pygame.font.render = ("Collision:", self.collide)
There's also another error here.
Python: |
def __init__(self):
pygame.font.init()
return None
self.font = pygame.font.Font(None,36)
self.collide = False
|
There's also another error after that... Seriously... =.='
|
|
|
|
|
|
|
|