Python 改变游戏规则的闪电移动速度
好的,我正在尝试使用pygame模块在python中编写一个简单的乒乓球游戏,我一直遇到一个属性错误。我知道以前也有人问过类似的问题,但我无法从这些问题中找出答案。我非常感谢任何帮助:) 因此,我在代码中遇到的错误是Python 改变游戏规则的闪电移动速度,python,pygame,Python,Pygame,好的,我正在尝试使用pygame模块在python中编写一个简单的乒乓球游戏,我一直遇到一个属性错误。我知道以前也有人问过类似的问题,但我无法从这些问题中找出答案。我非常感谢任何帮助:) 因此,我在代码中遇到的错误是 File "D:/Dev/Documents/Projects/Pong/pong.py", line 81, in <module> ball.x += ball_speed_x AttributeError: 'pygame.Surface' objec
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'
文件“D:/Dev/Documents/Projects/Pong/Pong.py”,第81行,在
球.x+=球的速度
AttributeError:“pygame.Surface”对象没有属性“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)
导入pygame,sys
pygame.init()
clock=pygame.time.clock()
屏幕宽度=1280
屏幕高度=960
screen=pygame.display.set_模式((屏幕宽度、屏幕高度))
pygame.display.set_标题('game')
background=pygame.image.load('bg.png')
白色=(255,255,255)
绿色=(51255,51)
ball=pygame.image.load('ball.png')
palle1=pygame.Rect(屏幕宽度-整数(20)、屏幕高度/整数(2)-整数(70)、整数(10)、整数(140))
桨2=pygame.Rect(整数(10),屏幕高度/整数(2)-整数(70),整数(10),整数(140))
basicfont=pygame.font.SysFont(无,48)
text=basicfont.render('Pong Game',True,绿色)
textrect=text.get_rect()
textrect.centerx=屏幕。get_rect().centerx
球速×7
球速y=7
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
屏幕填充(白色)
屏幕光点(背景,(0,0))
screen.blit(text,textrect)
pygame.draw.rect(屏幕、白色、挡板1)
pygame.draw.rect(屏幕,白色,挡板2)
pygame.draw.aaline(屏幕,绿色,(屏幕宽度/2,0),(屏幕宽度/2,屏幕高度))
pygame.display.flip()
球.x+=球的速度
ball.y+=ball\u speed\u y
如果ball.top=屏幕高度:
球速度y*=-1
如果ball.left=屏幕宽度:
球的速度x*=-1
时钟滴答(60)
再次重申,任何帮助都将不胜感激,我也知道其他类似的问题,我只是对这门语言不熟悉,似乎无法理解。谢谢。A没有职位。曲面仅包含图像
如果要在窗口中绘制球
曲面,则必须在特定位置绘制球
您必须创建一个pygame.Surface
和一个对象:
ball=pygame.image.load('ball.png')
ball\u rect=ball.get\u rect(中心=(屏幕宽度//2,屏幕高度//2))
然后,您可以将球的位置更改为ball\u rect
和blit
到屏幕
表面:
screen.blit(球,球)
请参见示例:
import pygame,sys
pygame.init()
clock=pygame.time.clock()
屏幕宽度=1280
屏幕高度=960
screen=pygame.display.set_模式((屏幕宽度、屏幕高度))
pygame.display.set_标题('game')
background=pygame.image.load('bg.png')
白色=(255,255,255)
绿色=(51255,51)
ball=pygame.image.load('ball.png')
ball\u rect=ball.get\u rect(中心=(屏幕宽度//2,屏幕高度//2))
palle1=pygame.Rect(屏幕宽度-整数(20)、屏幕高度/整数(2)-整数(70)、整数(10)、整数(140))
桨2=pygame.Rect(整数(10),屏幕高度/整数(2)-整数(70),整数(10),整数(140))
basicfont=pygame.font.SysFont(无,48)
text=basicfont.render('Pong Game',True,绿色)
textrect=text.get_rect()
textrect.centerx=屏幕。get_rect().centerx
球速×7
球速y=7
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
屏幕填充(白色)
屏幕光点(背景,(0,0))
screen.blit(text,textrect)
pygame.draw.rect(屏幕、白色、挡板1)
pygame.draw.rect(屏幕,白色,挡板2)
pygame.draw.aaline(屏幕,绿色,(屏幕宽度/2,0),(屏幕宽度/2,屏幕高度))
屏幕。blit(球,球)
pygame.display.flip()
球速x+=球速x
球速y+=球速y
如果ball_rect.top=屏幕高度:
球速度y*=-1
如果ball_rect.left=屏幕宽度:
球的速度x*=-1
时钟滴答(60)