python - 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)
同样,任何帮助将不胜感激,我知道其他类似的问题,我只是这种语言的新手,似乎无法弄清楚这一点。谢谢。
解决方案
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_rect
Surface :blit
screen
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)
推荐阅读
- javascript - 对于 2 个不同的元素,我有相同的 ngStyle。如何覆盖它们以保持代码干燥?
- python - 删除重复的列
- google-cloud-platform - Google Cloud - IoT Core - 每 1 小时发送一次配置重启设备
- jwt - 如何获取当前 Liferay 会话的 JWT 令牌
- java - 属性重命名后,OWL-API 与 EntitySearcher 存在问题
- javascript - 如何从 Firebase 存储中读取文件并在 React 组件上显示内容?
- javascript - JVectorMap 错误:
属性变换:预期数字,“缩放(NaN)翻译(N ...” - r - 在 R 中,获取一个日期字符串并将其转换为日期时间格式(使用 lubridate)
- javascript - 我们可以在没有事件处理程序的情况下模拟按键吗?
- scala - 为什么 import cat.implicits._ 不再需要导入类型类实例?