我正在做一个面向篮球的乒乓球比赛。我正在尝试将当前具有的矩形更改为篮球运动员精灵,以使其看起来更具吸引力。发生的情况是我的左侧球员完全没有出现,是不可见的,但仍然起着阻挡作用,将球击中。非常感谢任何帮助,谢谢!
import pygame
black = (0,0,0)
white = (255,255,255)
pygame.init()
size = 800,600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Basketball Shootout")
done = False
clock = pygame.time.Clock()
class Player1(pygame.sprite.Sprite):
def __init__(self, x, y):
"""Constructor function"""
# Call the parent's constructor
super().__init__()
# Set height, width
self.image = pygame.Surface([15, 15])
self.image = pygame.image.load("player1.png")
def player1(x1, y1, xsize, ysize):
Player1(screen, [x1, y1, xsize, ysize])
def player2(x2, y2, xsize, ysize):
pygame.draw.rect(screen, black, [x2,y2,xsize,ysize])
def ball(ballx, bally):
pygame.draw.circle(screen, black, [ballx,bally],20)
def Score1(score1):
font = pygame.font.Font("Minecraft.ttf" ,50)
text = font.render(str(score1), True, white)
screen.blit(text, [160, 550])
def Score2(score2):
font = pygame.font.Font("Minecraft.ttf" ,50)
text = font.render(str(score2), True, white)
screen.blit(text, [610, 550])
x1 = 20
y1 = 175
xsize = 35
ysize = 150
speed1 = 0
x2 = 740
y2 = 175
speed2 = 0
ballx = 550
bally = 250
speedx = 8
speedy = 5
score1 = 0
score2 = 0
bg = pygame.image.load("pongbg2.png")
rect1 = pygame.Rect(50,510,100,50)
def pausescreen():
import pausescreen
display_game = True
game_page = 1
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.draw.rect(screen, (255, 255, 255), rect1)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
speed1 = -15
if event.key == pygame.K_s:
speed1 = 15
if event.key == pygame.K_UP:
speed2 = -15
if event.key == pygame.K_DOWN:
speed2 = 15
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
speed1 = 0
if event.key == pygame.K_s:
speed1 = 0
if event.key == pygame.K_UP:
speed2 = 0
if event.key == pygame.K_DOWN:
speed2 = 0
if event.key == pygame.K_p:
pausescreen()
screen.blit(bg, (0, 0))
player1(x1, y1, xsize, ysize)
player2(x2, y2, xsize, ysize)
ball(ballx,bally)
Score1(score1)
Score2(score2)
y1 += speed1
y2 += speed2
ballx += speedx
bally += speedy
if y1 < 0:
y1 = 0
if y1 > 350:
y1 = 350
if y2 < 0:
y2 = 0
if y2 > 350:
y2 = 350
if ballx+20 > x2 and bally-20 > y2 and bally+20 < y2+ysize and ballx < x2+3:
speedx = -speedx
if ballx-20 < x1+35 and bally-20 > y1 and bally+20 < y1+ysize and ballx > x1+38:
speedx = -speedx
if bally > 477 or bally < 23:
speedy = -speedy
if ballx < 13:
score2 += 1
ballx = 350
bally = 250
if ballx > 750:
score1 += 1
ballx = 350
bally = 250
pygame.display.flip()
clock.tick(60)
pygame.quit()
答案 0 :(得分:1)
您没有在左侧看到播放器,因为您没有将其绘制到屏幕上。
在大多数情况下,如果您查看代码,这就是所有绘图代码:
screen.blit(bg, (0, 0))
player1(x1, y1, xsize, ysize)
player2(x2, y2, xsize, ysize)
ball(ballx,bally)
Score1(score1)
Score2(score2)
查看完每个功能后,单数为player1
,我怀疑是“左边的玩家”。该函数仅调用Player1
的构造函数:
def player1(x1, y1, xsize, ysize):
Player1(screen, [x1, y1, xsize, ysize])
其他人实际上是一些东西,例如player2
:
def player2(x2, y2, xsize, ysize):
pygame.draw.rect(screen, black, [x2,y2,xsize,ysize])
所以我的猜测是:将功能player1
更改为pygame.draw.rect(screen, x1, y1, xsize, ysize)
。
编辑:如果要绘制要在Player1
类中加载的图像,请尝试以下代码:
class Player1(pygame.sprite.Sprite):
def __init__(self, x, y):
"""Constructor function"""
# Call the parent's constructor
super().__init__()
# Set height, width
self.image = pygame.Surface([15, 15])
self.image = pygame.image.load("player1.png")
def draw(self, x1, y1):
screen.blit(self.image, [x1, y1])
然后,在while
循环之前,您可以像这样初始化player1
对象:player1 = Player1(x1, y1)
。
摆脱player1
函数,在您以前调用player1
的地方,调用方法 player1.draw(x1, y1)
。 / p>