在过去的几天里,我一直在研究乒乓球的这个代码,而且我几乎让它运行起来了。一切都取决于物体的运动。当pygame窗口打开时,屏幕会冻结。我的main()似乎有些不对劲,但我仍然是python / pygame的新手,并且错了。
import pygame, sys
pygame.init()
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))
player = Player()
ball = Ball()
enemy = Player()
def main():
running = True
while running == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print ("Game exited by user")
exit()
else:
running = False
pygame.font.init()
clock = pygame.time.Clock()
FPS = 60
backgroundimage = pygame.image.load("background.png")
pygame.display.set_caption("Justin's pong")
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 :(得分:0)
问题是这段代码:
running = True
while running == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print ("Game exited by user")
exit()
else:
running = False
您正确地获取了这些事件,但如果没有任何事件(因为开头没有),您将永远运行,检查事件。
我假设您希望其余代码也在while循环中运行,因此您应该缩进它。