以下是我的程序中控制得分的代码段。问题在于,每次触及右侧墙壁和左侧平底板时,它会在分数上加1,并且每当触及左侧墙壁时,它也会减去一个点。当它应该做的只是每次触及右墙时添加一个。
FRAMECLOCK = pygame.time.Clock() #set frame rate
SURFACEDISPLAY = pygame.display.set_mode((WIDTH,HEIGHT)) #Clear the surface on refresh
pygame.display.set_caption ('Pong') #title of window
ballX = WIDTH/2 - PLACEMENTMARKER/2 #ball position on X axis at the start
ballY = HEIGHT/2 - PLACEMENTMARKER/2 #ball position on Y axis at the start
playerOnePosition = (HEIGHT - PADDLESIZE) /2 #paddle one position at the start
playerTwoPosition = (HEIGHT - PADDLESIZE) /2 #paddle two position at the start
score = 0
#Sets starting position movement
ballDirX = -1 #-1 = left 1 = right
ballDirY = -1 # -1 = up 1 = down
paddle1 = pygame.Rect(PADDLEDISTANCE,playerOnePosition, PLACEMENTMARKER,PADDLESIZE) #paddle one drawing
paddle2 = pygame.Rect(WIDTH - PADDLEDISTANCE - PLACEMENTMARKER, playerTwoPosition, PLACEMENTMARKER,PADDLESIZE) #paddle two drawing
ball = pygame.Rect(ballX, ballY, PLACEMENTMARKER, PLACEMENTMARKER)#ball drawing
Pong() #calling the game surface in main function
paddles(paddle1) #calling paddle 1 main function
paddles(paddle2) #calling paddle 2 in main function
pongball(ball) #calling ball in main function
while True: #game Loop
for event in pygame.event.get(): #Checks to see if program is quit
if event.type == QUIT:
pygame.quit()
sys.exit() #system quit
Pong() #Otherwise it performs these functions
paddles(paddle1)
paddles(paddle2)
pongball(ball)
displayScore(str(score))
答案 0 :(得分:1)
checkscore功能正在重置分数,而不是减去分数。当你用桨击它时,它也明确地加了一个。
我已将此功能修改为仅在击中右墙时添加,而不是在击中左侧时减去:
let IP = "192.168.2.255"
let PORT:UInt16 = 6454
var socket:GCDAsyncUdpSocket!
func sendPacket(){
socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
let message = "test"
let data = message.dataUsingEncoding(NSUTF8StringEncoding)
socket.sendData(data, toHost: IP, port: PORT, withTimeout: 2, tag: 0)
}
只需用当前def checkscore (paddle1, ball, score, ballDirX):
#this is where the program resets after a point is scored
if ball.right == WIDTH - PLACEMENTMARKER:
score += 1
return score
#no points scored, return score unchanged
else: return score
替换此函数,一切都可以正常工作
我假设你至少复制了大部分内容,请确保彻底阅读所有内容并尝试理解每一点。