Why does my computer give an attribute error when I try to blit my sprite to the screen

24 views Asked by At

I have been trying to blit my ball sprite but it just keeps giving me an attribute or type error. I have tried the blit, render, and draw methods but none are working. Can somebody explain why it isn't working?

My current code is this:

import pygame
import sys
pygame.init()
pixel=4
screen= pygame.display.set_mode([1200,800])
background= pygame.image.load('background.jpg')
background= pygame.transform.scale(background,(1200,800))
bar_img= pygame.image.load('bar.png')
bar= pygame.transform.scale(bar_img,(140,30))
bar_x= 600
bar_y= 560
pos_x= 100
pos_y= 100
speed= [1,1]
vel= 0.8
self_x= 100
self_y= 100
class Ball(pygame.sprite.Sprite):
    def __init__(self, image='ball.png', speed=[1,1]):
        pygame.sprite.Sprite.__init__(self)
        ballimg= pygame.image.load(image)
        ball_= pygame.transform.scale(ballimg,(25,25))
        self.velocity= speed
        def update(self):
            self_x += self.velocity[1]
            self_y += self.velocity[1]
ball= Ball()

running= True
while running:
    for event in pygame.event.get():
        if event.type== pygame.QUIT:
            running= False
            sys.exit()
        if event.type== pygame.KEYUP:
            bar_x-=0
            bar_x+=0
    keys= pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and bar_x>0:
        bar_x-=vel
    if bar_x==0:
        bar_x-=0
    if keys[pygame.K_RIGHT] and bar_x<1064:
        bar_x+=vel
    if bar_x==1064:
        bar_x+=0
    Ball.update(Ball)
    if self_x<0 or self_x>1064:
        ball.velocity[1]= -ball.velocity[1]
    if self_y<0:
        ball.velocity[1]= -ball.velocity[1]
    if self_y>800:
        running= False  
    screen.blit(background, (0,0))
    screen.blit(bar, (bar_x,bar_y))
    ball.draw(screen)
    pygame.display.update()
    pygame.display.flip()
    
pygame.quit()

I expected the code to open a window showing a bouncing ball and a bar on the screen. The game would end when the ball left the bottom of the screen . However, it just gives me an error telling me the object Ball has no 'draw' attributes. I also tried blitting it like this:

screen.blit(ball, (self_x,self_y))

the command panel told me that the first argument has to be a pygame.surface.Surface.

0

There are 0 answers