首页 > 解决方案 > Pygame 改变 blit 的移动速度

问题描述

好的,所以我尝试使用 pygame 模块在 python 中编写一个简单的乒乓球游戏,但我一直遇到属性错误。我知道以前有人问过类似的问题,但我无法从这些问题中找出答案。我将不胜感激任何帮助:)。

所以,我在代码中遇到的错误是

  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'

我的代码是


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)

同样,任何帮助将不胜感激,我知道其他类似的问题,我只是这种语言的新手,似乎无法弄清楚这一点。谢谢。

标签: pythonpygame

解决方案


Apygame.Surface没有位置。Surface 仅包含图像。

如果要ball在窗口中绘制曲面,则必须blit()ball某个位置。

您必须创建一个pygame.Surface和一个pygame.Rect对象:

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

然后你可以改变球的位置到ball_rectSurface :blitscreen

screen.blit(ball, ball_rect)

请参阅示例:

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)

推荐阅读