Search code examples
pythonpygame

Pygame changing movement speed of a blit


Alright so I am trying to just code a simple pong game in python using pygame module and I keep running into an attribute error. I know similar questions have been asked before but I can't figure out the answer from those questions. I'd appreciate any help at all :).

so, the error i am getting in my code is

  File "D:/Dev/Documents/Projects/Pong/pong.py", line 81, in <module>
    ball.x += ball_speed_x
AttributeError: 'pygame.Surface' object has no attribute 'x'

my code is


import pygame, sys
pygame.init()

clock = pygame.time.Clock()

screen_width = 1280
screen_height = 960

screen = pygame.display.set_mode((screen_width,screen_height))

pygame.display.set_caption('game')

background = pygame.image.load('bg.png')

white = (255, 255, 255)
green = (51, 255, 51)

ball = pygame.image.load('ball.png')

paddle1 = pygame.Rect(screen_width - int(20), screen_height/int(2) - int(70), int(10), int(140))

paddle2 = pygame.Rect(int(10), screen_height/int(2) - int(70), int(10), int(140))

basicfont = pygame.font.SysFont(None, 48)
text = basicfont.render('Pong Game', True, green)
textrect = text.get_rect()
textrect.centerx = screen.get_rect().centerx

ball_speed_x = 7
ball_speed_y = 7

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill(white)

    screen.blit(background, (0, 0))

    screen.blit(text, textrect)

    pygame.draw.rect(screen, white, paddle1)
    pygame.draw.rect(screen, white, paddle2)

    pygame.draw.aaline(screen, green, (screen_width/2, 0), (screen_width/2, screen_height))

    pygame.display.flip()

    ball.x += ball_speed_x
    ball.y += ball_speed_y

    if ball.top <= 0 or ball.bottom >= screen_height:
        ball_speed_y *= -1
    if ball.left <= 0 or ball.right >= screen_width:
        ball_speed_x *= -1

    clock.tick(60)

Again, any help would be greatly appreciated and I am aware of other similar questions, I'm just new to this language and can't seem to figure this out. Thanks.


Solution

  • A pygame.Surface has no position. A Surface just contains the image.

    If you want to draw the ball surface in the window, then you have to blit() the ball at a certain position.

    You have to create a pygame.Surface and a pygame.Rect objecct:

    ball = pygame.image.load('ball.png')
    ball_rect = ball.get_rect(center = (screen_width // 2, screen_height // 2))
    

    Then you can change the location of ball_rect and blit the ball to the screen Surface:

    screen.blit(ball, ball_rect)
    

    See the example:

    import pygame, sys
    pygame.init()
    
    clock = pygame.time.Clock()
    
    screen_width = 1280
    screen_height = 960
    
    screen = pygame.display.set_mode((screen_width,screen_height))
    
    pygame.display.set_caption('game')
    
    background = pygame.image.load('bg.png')
    
    white = (255, 255, 255)
    green = (51, 255, 51)
    
    ball = pygame.image.load('ball.png')
    ball_rect = ball.get_rect(center = (screen_width // 2, screen_height // 2))
    
    paddle1 = pygame.Rect(screen_width - int(20), screen_height/int(2) - int(70), int(10), int(140))
    
    paddle2 = pygame.Rect(int(10), screen_height/int(2) - int(70), int(10), int(140))
    
    basicfont = pygame.font.SysFont(None, 48)
    text = basicfont.render('Pong Game', True, green)
    textrect = text.get_rect()
    textrect.centerx = screen.get_rect().centerx
    
    ball_speed_x = 7
    ball_speed_y = 7
    
    while True:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
    
        screen.fill(white)
        screen.blit(background, (0, 0))
    
        screen.blit(text, textrect)
        pygame.draw.rect(screen, white, paddle1)
        pygame.draw.rect(screen, white, paddle2)
        pygame.draw.aaline(screen, green, (screen_width/2, 0), (screen_width/2, screen_height))
    
        screen.blit(ball, ball_rect)
    
        pygame.display.flip()
    
        ball_rect.x += ball_speed_x
        ball_rect.y += ball_speed_y
    
        if ball_rect.top <= 0 or ball_rect.bottom >= screen_height:
            ball_speed_y *= -1
        if ball_rect.left <= 0 or ball_rect.right >= screen_width:
            ball_speed_x *= -1
    
        clock.tick(60)