我正在为我的大学班制作一个pong的修改版本,并且在传递一些方法时遇到了一些麻烦。我相信我已经设置了所需的方式来进行一些参数传递。代码运行正常,并在我的main函数中的while循环之后停止工作。任何帮助表示赞赏。
import pygame
SCR_WID, SCR_HEI = 640, 480
class Player():
def __init__(self):
self.x, self.y = 16, SCR_HEI/2
self.x1, self.y1 = SCR_WID-16, SCR_HEI/2
self.speed = 3
self.padWid, self.padHei = 8, 64
self.score = 0
self.scoreFont = pygame.font.Font("imagine_font.ttf", 64)
def scoring(self):
scoreBlit = self.scoreFont.render(str(self.score), 1, (0,0,0))
screen.blit(scoreBlit, (32, 16))
if self.score == 10:
print ("player 1 wins!")
exit()
def scoring1(self):
scoreBlit = self.scoreFont.render(str(self.score), 1, (0,0,0))
screen.blit(scoreBlit, (SCR_HEI+92, 16))
if self.score == 10:
print ("Player 2 wins!")
exit()
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.y -= self.speed
elif keys[pygame.K_s]:
self.y += self.speed
if self.y <= 0:
self.y = 0
elif self.y >= SCR_HEI-64:
self.y = SCR_HEI-64
keys1 = pygame.key.get_pressed()
if keys1[pygame.K_UP]:
self.y1 -= self.speed
elif keys1[pygame.K_DOWN]:
self.y1 += self.speed
if self.y1 <= 0:
self.y1 = 0
elif self.y >= SCR_HEI-64:
self.y1 = SCR_HEI-64
def draw(self):
pygame.draw.rect(screen, (0,0,0), (self.x, self.y, self.padWid, self.padHei))
pygame.draw.rect(screen, (0,0,0), (self.x1, self.y1, self.padWid, self.padHei))
class Ball():
def __init__(self):
self.x, self.y = SCR_WID/2, SCR_HEI/2
self.x1, self.y1 = SCR_WID/2, SCR_HEI/2
self.speed_x = -3
self.speed_y = 3
self.size = 8
def movement(self):
self.x += self.speed_x
self.y += self.speed_y
if self.y <= 0:
self.speed_y *= -1
elif self.y >= SCR_HEI-self.size:
self.speed_y *= -1
if self.x <= 0:
self.__init__()
enemy.score += 1
elif self.x >= SCR_WID-self.size:
self.__init__()
self.speed_x = 3
player.score += 1
for n in range(-self.size, player.padHei):
if self.y == player.y + n:
if self.x <= player.x + player.padWid:
self.speed_x *= -1
break
n += 1
self.x1 += self.speed_x
self.y1 += self.speed_y
if self.y1 <= 0:
self.speed_y *= -1
elif self.y1 >= SCR_HEI-self.size:
self.speed_y *= -1
if self.x1 <= 0:
self.__init__()
enemy.score += 1
elif self.x1 >= SCR_WID-self.size:
self.__init__()
self.speed_x = 3
player.score += 1
for n in range(-self.size, enemy.padHei):
if self.y1 == enemy.y1 + n:
if self.x1 >= enemy.x1 - enemy.padWid:
self.speed_x *= -1
break
n += 1
def draw(self):
pygame.draw.rect(screen, (112,138,144), (self.x, self.y, 8, 8))
SCR_WID, SCR_HEI = 640, 480
screen = pygame.display.set_mode((SCR_WID, SCR_HEI))
pygame.display.set_caption("Justin's pong")
pygame.font.init()
clock = pygame.time.Clock()
FPS = 60
player = Player()
ball = Ball()
enemy = Player()
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print ("Game exited by user")
exit()
backgroundimage = pygame.image.load("background.png")
ball.movement()
player.movement()
enemy.movement()
position = (0,0)
screen.fill((0, 0, 0))
screen.blit(backgroundimage, position)
ball.draw()
player.draw()
player.scoring()
enemy.draw()
enemy.scoring1()
pygame.display.flip()
clock.tick(FPS)
main()
答案 0 :(得分:3)
在循环完成之前,将执行超出while
循环的任何内容。
i = 0
while i < 5:
print(i)
i += 1
print('Finished!')
输出:
0
1
2
3
4
Finished!