我试图(非常)在像Pong这样的pygame中编写一个非常简洁的程序,但我似乎被困在试图垂直移动我的桨。而且,虽然它们确实在移动......它们只是在不断移动。
#python code
import pygame, sys
#Screen Width/Height
screen = pygame.display.set_mode((800,600))
#Background Image
background_image = pygame.image.load("tennis.jpg").convert()
screen.blit(background_image, [0,0])
#Frame Rate
clock = pygame.time.Clock()
#Paddles
paddle_player1 = pygame.Rect(5,50,5,60)
paddle_player2 = pygame.Rect(790,50,5,60)
#Process Player Input
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
player1_up = pygame.key.get_pressed()[pygame.K_w]
player1_down = pygame.key.get_pressed()[pygame.K_s]
player2_up = pygame.key.get_pressed()[pygame.K_UP]
player2_down = pygame.key.get_pressed()[pygame.K_DOWN]
#Updating Game State Logic
if player1_up:
paddle_player1.y += -50
if player1_down:
paddle_player1.y += 50
if player2_up:
paddle_player2.y += -50
if player2_down:
paddle_player2.y += 50
if paddle_player1.y < 0:
paddle_player1.y = 0
if paddle_player2.y < 0:
paddle_player2.y = 0
if paddle_player1.y > screen.get_height() - paddle_player1.height:
paddle_player1.y = screen.get_height() - paddle_player1.height
if paddle_player2.y > screen.get_height() - paddle_player2.height:
paddle_player2.y = screen.get_height() - paddle_player2.height
#Rendering
pygame.draw.rect(screen,(0,0,0), paddle_player1)
pygame.draw.rect(screen,(0,0,0), paddle_player2)
clock.tick(50)
pygame.display.flip()
答案 0 :(得分:0)
你需要在你的游戏循环中叫你blit,就像这样:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
player1_up = pygame.key.get_pressed()[pygame.K_w]
player1_down = pygame.key.get_pressed()[pygame.K_s]
player2_up = pygame.key.get_pressed()[pygame.K_UP]
player2_down = pygame.key.get_pressed()[pygame.K_DOWN]
#Updating Game State Logic
if player1_up:
paddle_player1.y += -50
if player1_down:
paddle_player1.y += 50
if player2_up:
paddle_player2.y += -50
if player2_down:
paddle_player2.y += 50
if paddle_player1.y < 0:
paddle_player1.y = 0
if paddle_player2.y < 0:
paddle_player2.y = 0
if paddle_player1.y > screen.get_height() - paddle_player1.height:
paddle_player1.y = screen.get_height() - paddle_player1.height
if paddle_player2.y > screen.get_height() - paddle_player2.height:
paddle_player2.y = screen.get_height() - paddle_player2.height
screen.blit(background_image, [0,0]) #move your blit here
pygame.draw.rect(screen,(0,0,0), paddle_player1)
pygame.draw.rect(screen,(0,0,0), paddle_player2)