Python, AttributeError: 'PlayerPaddle' object has no attribute 'update' - python

import pygame
from pygame.locals import *
class PlayerPaddle(object):
def __init__(self, screensize):
self.screensize = screensize
self.position_x = int(screensize[0]*0.5)
self.position_y = int(screensize[1]*0.8)
self.width = 10
self.height = 4
self.rect = pygame.Rect(self.position_x - (self.width*0.5),
self.position_y - (self.height*0.5),
self.width, self.height)
self.color = (100, 200, 200)
self.speed = 5
self.direction = 0
def update(self):
self.position_x += self.direction * self.speed
def render(self, screen):
pygame.draw.rect(screen, self.color, self.rect, 0)
pygame.draw.rect(screen, (0,0,0), self.rect, 1)
def main():
pygame.init()
screensize = (600, 700)
screen = pygame.display.set_mode(screensize)
clock = pygame.time.Clock()
player_paddle = PlayerPaddle(screensize)
running = True
while running:
clock.tick(64)
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_paddle.direction = 1
elif event.key == pygame.K_RIGHT:
player_paddle.direction = -1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player_paddle.direction = 0
elif event.key == pygame.K_RIGHT:
player_paddle.direction = 0
player_paddle.update()
screen.fill((100, 100, 100))
player_paddle.render(screen)
pygame.display.flip()
pygame.quit()
main()
Sorry for long code.. But I'm getting so frustrated. Why am i getting "'PlayerPaddle' object has no attribute 'update'"
Error !?
For what i have been able to understand its my def update(self) function that is returning null or smth.. But how is that? Is it in my eventhandler that the error is? Is it updating the position wrong?

It looks like your indentation is off. Your methods update and render are indented inside the __init__ method. Move them out one indentation level.

Related

Second surface doesn't appear on screen until I freeze the game

I'm trying out Pygame and started with a classic, creating the Snake game, the issue is that I try to blit the 'apple' on the board but the apple doesn't appear up until I crash in the walls this is when I set fps = 0 and I freeze the game. I've noticed that when I increase the fps to something like 100 or more the apple starts appearing and disappearing.
import random
import pygame
class Game:
def __init__(self):
pygame.init()
self.board = pygame.display.set_mode((400, 400))
self.apple = Apple(self.board)
self.snake = Snake(self.board, self)
self.direction = 'right'
self.going = True
self.fps = 5
def play(self):
clock = pygame.time.Clock()
while self.going:
clock.tick(self.fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.going = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.direction = 'left'
if event.key == pygame.K_RIGHT:
self.direction = 'right'
if event.key == pygame.K_UP:
self.direction = 'up'
if event.key == pygame.K_DOWN:
self.direction = 'down'
self.apple.spawn()
self.snake.move(self.direction)
def game_over(self):
self.fps = 0
def get_fps(self):
return self.fps
class Snake:
def __init__(self, board, game):
self.board = board
self.image = pygame.Surface((10, 10))
self.game = game
self.length = 5
self.x = [10] * self.length
self.y = [10] * self.length
def move(self, direction):
for i in range(self.length-1, 0, -1):
self.x[i] = self.x[i-1]
self.y[i] = self.y[i-1]
if direction == 'right':
self.x[0] += 10
if direction == 'left':
self.x[0] -= 10
if direction == 'up':
self.y[0] -= 10
if direction == 'down':
self.y[0] += 10
if self.x[0] == self.board.get_width() or self.x[0] == -10 or\
self.y[0] == self.board.get_height() or self.y[0] == -10:
self.game.game_over()
if self.game.get_fps() != 0:
self.draw()
def draw(self):
self.board.fill((0, 255, 0))
for i in range(self.length):
self.board.blit(self.image, (self.x[i], self.y[i]))
pygame.display.flip()
class Apple:
def __init__(self, board):
self.board = board
self.image = pygame.Surface((10, 10))
self.image.fill(pygame.color.Color('red'))
self.x = random.randint(0, self.board.get_width())
self.y = random.randint(0, self.board.get_height())
def spawn(self):
self.board.blit(self.image, (self.x, self.y))
pygame.display.flip()
if __name__ == '__main__':
game = Game()
game.play()
Clear the display at the begin of the application loop and update the dispaly at the end of the application loop:
class Game:
# [...]
def play(self):
clock = pygame.time.Clock()
while self.going:
clock.tick(self.fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.going = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.direction = 'left'
if event.key == pygame.K_RIGHT:
self.direction = 'right'
if event.key == pygame.K_UP:
self.direction = 'up'
if event.key == pygame.K_DOWN:
self.direction = 'down'
self.board.fill((0, 255, 0)) # <---
self.apple.spawn()
self.snake.move(self.direction)
pygame.display.flip() # <---
class Snake:
# [...]
def draw(self):
# self.board.fill((0, 255, 0)) <-- DELETE
for i in range(self.length):
self.board.blit(self.image, (self.x[i], self.y[i]))
# pygame.display.flip() <-- DELETE
class Apple:
# [...]
def spawn(self):
self.board.blit(self.image, (self.x, self.y))
#pygame.display.flip() <-- DELETE
fill clears the entire Surface. Everything that was previously drawn is lost.
One update of the display at the end of the application loop is sufficient. Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering.

how to use collide direct for multiple sprites

#sprites
import random, sys, time, math, pygame
from pygame.locals import *
from pygame import mixer # Load the required library
FPS = 30 # frames per second to update the screen
fpsClock = pygame.time.Clock()
WINWIDTH = 1500 # width of the program's window, in pixels
WINHEIGHT = 1000 # height in pixels
HALF_WINWIDTH = int(WINWIDTH / 2)
HALF_WINHEIGHT = int(WINHEIGHT / 2)
moveX,moveY=0,0
move2X,move2Y=0,0
GRASSCOLOR = (24, 255, 0)
WHITE = (255, 255, 255)
PURPLE = (128, 0, 128)
RED = (255, 0, 0)
pygame.init()
global FPSCLOCK, DISPLAYSURF, self
DISPLAYSURF = pygame.display.set_mode((WINWIDTH, WINHEIGHT))
pygame.display.set_icon(pygame.image.load('god.png'))
pygame.display.set_caption('Smash Bros Melee')
BASICFONT = pygame.font.Font('freesansbold.ttf', 32)
class Sprite:
def __init__(self,x,y):
self.x=x
self.y=y
self.width=50
self.height=50
self.i0= pygame.image.load('ryanrenolds.jpeg')
self.timeTarget=10
self.timeNum=0
self.currentImage=0
def render(self):
DISPLAYSURF.blit(self.i0, (self.x, self.y))
player=Sprite(0,0)
class Sprite2:
def __init__(self,x,y):
self.x=x
self.y=y
self.width=50
self.height=50
self.i0= pygame.image.load('ArkhamKnight.png')
self.i1= pygame.image.load('Black-Panther.png')
self.timeTarget=10
self.timeNum=0
self.currentImage=0
def update(self):
self.timeNum+=1
if (self.timeNum==self.timeTarget):
if (self.currentImage==0):
self.currentImage+=1
else:
self.currentImage=0
self.timeNum=0
self.render()
def render(self):
if(self.currentImage==0):
DISPLAYSURF.blit(self.i0, (self.x, self.y))
else:
DISPLAYSURF.blit(self.i1, (self.x, self.y))
player2=Sprite2(200,300)
def checkCollision(Sprite, Sprite2):
col = Sprite.rect.colliderect(Sprite2.rect)
if col == True:
sys.exit()
while True:
for event in pygame.event.get():
if event.type == KEYUP:
if event.key == K_a:
moveX = moveX - 0
elif event.key == K_d:
moveX == False
elif event.key == K_w:
moveY == False
elif event.key == K_s:
moveY == False
if (event.type==pygame.KEYDOWN):
if (event.key==pygame.K_a):
moveX = moveX - 5
elif (event.key==pygame.K_d):
moveX = moveX + 5
elif (event.key==pygame.K_w):
moveY = moveY - 5
elif (event.key==pygame.K_s):
moveY = moveY + 5
elif (event.key==pygame.K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_LEFT:
move2X = move2X - 0
elif event.key == K_RIGHT:
move2X == False
elif event.key == K_UP:
move2Y == False
elif event.key == K_DOWN:
move2Y == False
if (event.type==pygame.KEYDOWN):
if (event.key==pygame.K_LEFT):
move2X = move2X - 5
elif (event.key==pygame.K_RIGHT):
move2X = move2X + 5
elif (event.key==pygame.K_UP):
move2Y = move2Y - 5
elif (event.key==pygame.K_DOWN):
move2Y = move2Y + 5
elif (event.key==pygame.K_o):
player=Sprite(0,0)
player2=Sprite2(200,300)
elif (event.key==pygame.K_ESCAPE):
pygame.quit()
sys.exit()
DISPLAYSURF.fill(PURPLE)
player.x+=moveX
player.y+=moveY
player2.x+=move2X
player2.y+=move2Y
player.render()
player2.update()
checkCollision(Sprite, Sprite2)
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()
sys.exit()
how do i get to wear my sprites will collide my collide direct code asks for three arguements when i used self in the parameters but when i took self out it says that class Sprite has no attribute 'rect'
This the error i keep getting how do i fix this to where my game works to where my sprites will exit the game if they collide with each other
Traceback (most recent call last):
File "C:\Python27\Sprites\sprites.py", line 159, in
checkCollision(Sprite, Sprite2)
File "C:\Python27\Sprites\sprites.py", line 88, in checkCollision
col = Sprite.rect.colliderect(Sprite2.rect)
AttributeError: class Sprite has no attribute 'rect'
You don't have the attribute rect in your Sprite class, which is causing this problem. You also need an attribute rect in your Sprite2 class. You can do this by using the get_rect() method.
You can do this in your Sprite class by entering this in the __init__(self)
self.rect = self.i0.get_rect()
And for the Sprite2 class you can do this (because displayed image can change, put this in the update().)
if self.currentImage == 0:
self.rect = self.i0.get_rect()
elif self.currentImage == 1:
self.rect = self.i1.get_rect()
I hope this helps.

Block cannot move

I am trying to move this rectangle to make Pong. I had it working before but I messed up the code.
Could anyone help me make it move and possibly make my code look cleaner?
Again, I made it move, but the problem seems to be in the Update method.
Possibly the ScreenSide parameter???...
import pygame, sys, random
from pygame.locals import *
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((800, 600))
rectImg1 = 'Rect.jpg'
rectImg2 = 'Rect2.jpg'
RIGHT = "RIGHT"
LEFT = "LEFT"
WHITE = (255,255,255)
FPS = 30
PADDLE_SPEED = 5
BALL_SPEED = 10
fpsClock = pygame.time.Clock()
xPos = 0
yPos = 0
leftY = 20
rightY = 20
class Paddle(pygame.sprite.Sprite):
def __init__(self, screenSide):
pygame.sprite.Sprite.__init__(self)
self.screenSide = screenSide
if self.screenSide == LEFT:
self.image = pygame.image.load(rectImg1).convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = 20
self.rect.y = 20
def update(self):
if self.screenSide == LEFT:
self.y = leftY
allSpritesGroup = pygame.sprite.Group()
paddle = Paddle(LEFT)
allSpritesGroup.add(paddle)
#code to make it move
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if paddle.screenSide == LEFT:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
paddle.y += PADDLE_SPEED
elif event.key == pygame.K_w:
paddle.y -= PADDLE_SPEED
elif event.type == pygame.KEYUP:
if event.key == pygame.K_s or event.key == pygame.K_w:
paddle.y == 0
screen.fill((255,255,255))
allSpritesGroup.draw(screen)
allSpritesGroup.update()
pygame.display.flip()
fpsClock.tick(FPS)
pygame.quit()
Just a guess but your problem might be in:
elif event.type == pygame.KEYUP:
if event.key == pygame.K_s or event.key == pygame.K_w:
paddle.y == 0
This looks more like a comparison and if not then you're setting the y to 0 whenever you let go of a key.
Also, You're right about the update function:
def update(self):
if self.screenSide == LEFT:
self.y = leftY
You're constantly setting the y to 20 so it won't move since every time it updates its moved to 20.
Your event handling is broken. The KEYDOWN and KEYUP events are outside of the event loop because of this line if paddle.screenSide == LEFT:. You also need to update paddle.rect.y not paddle.y and you should do that in the class not with global variables. I'd give the paddles a self.y_speed attribute which you set in the event loop and then use it to update the self.rect.y position each frame in the update method. And remove the screenSide checks and just pass the image and position to the sprites during the instantiation.
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()
rectImg1 = pygame.Surface((30, 50))
rectImg1.fill((20, 20, 120))
rectImg2 = pygame.Surface((30, 50))
rectImg2.fill((120, 10, 20))
WHITE = (255,255,255)
FPS = 30
PADDLE_SPEED = 5
fpsClock = pygame.time.Clock()
class Paddle(pygame.sprite.Sprite):
def __init__(self, image, pos):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect(topleft=pos)
self.y_speed = 0
def update(self):
self.rect.y += self.y_speed
allSpritesGroup = pygame.sprite.Group()
paddle = Paddle(rectImg1, (20, 20))
paddle2 = Paddle(rectImg2, (750, 20))
allSpritesGroup.add(paddle, paddle2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
paddle.y_speed = PADDLE_SPEED
elif event.key == pygame.K_w:
paddle.y_speed = -PADDLE_SPEED
elif event.type == pygame.KEYUP:
if event.key == pygame.K_s or event.key == pygame.K_w:
paddle.y_speed = 0
allSpritesGroup.update()
screen.fill(WHITE)
allSpritesGroup.draw(screen)
pygame.display.flip()
fpsClock.tick(FPS)

Pygame creating multiple objects instead of moving only one

I'm trying to move a single image around the screen in Pygame using methods controlled by the keyboard, but it appears to be creating duplicates in those positions instead of moving the original image. Why is this happening, given that the image is created before entering the main loop?
Here is my code:
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((600, 500), 0, 32)
pygame.display.set_caption('Animation')
posx = 10
posy = 10
class Hero():
def __init__(self):
self.x = posx
self.y = posy
def appearance(self):
return pygame.image.load('C:\\Users\\admin\\Desktop\\right.png')
def move_right(self):
self.x += 25
return self.x
def move_left(self):
self.x -= 25
return self.x
def move_up(self):
self.y -= 25
return self.y
def move_down(self):
self.y += 25
return self.y
new_hero = Hero() #create a Hero
item = new_hero.appearance() #create one reference
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
posx = new_hero.move_left()
elif event.key == pygame.K_RIGHT:
posx = new_hero.move_right()
elif event.key == pygame.K_UP:
posy = new_hero.move_up()
elif event.key == pygame.K_DOWN:
posy = new_hero.move_down()
DISPLAYSURF.blit(item, (posx, posy))
pygame.display.update()
fpsClock.tick(FPS)
You forgot to put in code to delete all the past images.
This is the basic layout of a pygame project. You should ALWAYS (if you do not have a background image) fill the screen with white after updating.
white = 255, 255, 255
while True:
DISPLAYSURF.fill(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()

Pygame animation using mutiple images overlapping/not working

import pygame
pygame.init()
window = pygame.display.set_mode((800,600))
pygame.display.set_caption("TEST2")
black=(0,0,0)
white=(255,255,255)
moveX,moveY=0,0
clock = pygame.time.Clock()
class Sprite:
def __init__(self,x,y):
self.x=x
self.y=y
self.width=100
self.height=110
self.i100 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite0.PNG")
self.i1 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite1.PNG")
self.i2 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite2.PNG")
self.i3 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite3.PNG")
self.i4 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite4.PNG")
self.i5 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite5.PNG")
self.i6 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite6.PNG")
self.i7 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite7.PNG")
self.i8 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite8.PNG")
self.i9 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite9.PNG")
self.i10 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite10.PNG")
self.i11 = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite11.PNG")
self.timeTarget=10
self.timeNum=0
self.currentImage=0
def update(self):
self.timeNum+=1
if(self.timeNum==self.timeTarget):
if (self.currentImage==0):
self.currentImage+=1
else:
self.currentImage=0
self.timeNum=0
self.render()
def render(self):
if (self.currentImage==0):
window.blit(self.i100, (self.x,self.y))
else:
window.blit(self.i1, (self.x,self.y))
window.blit(self.i2, (self.x,self.y))
window.blit(self.i3, (self.x,self.y))
player=Sprite(110,100)
gameLoop = True
while gameLoop:
for event in pygame.event.get():
if event.type==pygame.QUIT:
gameLoop = False
if (event.type==pygame.KEYDOWN):
if (event.key==pygame.K_LEFT):
moveX = -3
if (event.key==pygame.K_RIGHT):
moveX = 3
if (event.key==pygame.K_UP):
moveY = -3
if (event.key==pygame.K_DOWN):
moveY = 3
if (event.type==pygame.KEYUP):
if (event.key==pygame.K_LEFT):
moveX=0
if (event.key==pygame.K_RIGHT):
moveX=0
if (event.key==pygame.K_UP):
moveY=0
if (event.key==pygame.K_DOWN):
moveY=0
window.fill(black)
player.x+=moveX
player.x+=moveY
player.update()
clock.tick(50)
pygame.display.flip()
pygame.quit()
What im doing is trying to animate 11 photos into an animation with pygame. this code works but when I run it the pictures seem to almost overlap. I did window.blit for the first few images and put them under else? I feel like I rendered them wrong. also I must add im really bad at picking up what people are trying to say and best learn from examples. Thanks!
BTW: your code could look like this:
I use my images in example but there are still lines with your images.
I use timer to change images.
You can press space to pause and escape to exit.
etc.
import pygame
#----------------------------------------------------------------------
class Sprite:
def __init__(self, x, y, curren_time):
self.rect = pygame.Rect(x, y, 100, 110)
self.images = []
#for x in range(12):
for x in range(1,4):
img = pygame.image.load("ball" + str(x) +".png")
#img = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST2.0/Sprite" + str(x) +".PNG")
self.images.append( img )
self.current_image = 0
self.time_num = 100 # miliseconds
self.time_target = curren_time + self.time_num
def update(self, curren_time):
if curren_time >= self.time_target:
self.time_target = curren_time + self.time_num
self.current_image += 1
if self.current_image == len(self.images):
self.current_image = 0
def render(self, window):
window.blit(self.images[self.current_image], self.rect)
#----------------------------------------------------------------------
# CONSTANS - uppercase
BLACK = (0 ,0 ,0 )
WHITE = (255,255,255)
#----------------------------------------------------------------------
# MAIN
def main():
pygame.init()
window = pygame.display.set_mode((800,600))
pygame.display.set_caption("TEST2")
move_x, move_y = 0, 0
clock = pygame.time.Clock()
curren_time = pygame.time.get_ticks()
player = Sprite(110,100, curren_time)
font = pygame.font.SysFont(None, 150)
pause_text = font.render("PAUSE", 1, WHITE)
pause_rect = pause_text.get_rect( center = window.get_rect().center ) # center text on screen
# mainloop
state_game = True
state_pause = False
while state_game:
curren_time = pygame.time.get_ticks()
# events
for event in pygame.event.get():
if event.type == pygame.QUIT:
state_game = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
state_game = False
elif event.key == pygame.K_SPACE:
state_pause = not state_pause
if event.key == pygame.K_LEFT:
move_x = -3
elif event.key == pygame.K_RIGHT:
move_x = 3
elif event.key == pygame.K_UP:
move_y = -3
elif event.key == pygame.K_DOWN:
move_y = 3
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
move_x = 0
elif event.key in (pygame.K_UP, pygame.K_DOWN):
move_y = 0
# moves
if not state_pause:
player.rect.x += move_x
player.rect.y += move_y
player.update(curren_time)
# draws
window.fill(BLACK)
player.render(window)
if state_pause:
window.blit(pause_text, pause_rect)
pygame.display.flip()
# FPS
clock.tick(50)
# the end
pygame.quit()
#----------------------------------------------------------------------
if __name__ == '__main__':
main()
ball1.png
ball2.png
ball3.png
By putting all those window.blit(...) calls one after another, you are drawing those three frames on top of each other. Even if your computer lagged for a second between each call, you still wouldn't see them individually because they all can't appear until pygame.display.flip() is called.
You should store the images in a list, and keep a counter like currentFrame that loops from 0 to number_of_frames-1 (or len(frames)-1). Then each frame of the game you do something like this:
class Player:
...
def draw(window):
window.blit(self.frames[self.currentFrame])

Categories

Resources