如何计算球在pygame'Pong'中击球的得分

时间:2015-10-23 00:14:25

标签: python pygame

最近我编写了一个pygame 'Pong'代码,我还没有完成它,因为拨片还不能移动。

然而,我在这里遇到了一个问题。因为我希望得到的分数等于球击中窗口边缘的次数。对于我的节目,当球击中墙壁时,得分将不会增加并且球将停止移动。我不知道原因,我想在这里找到答案。

非常感谢!

代码:

import pygame, sys, time,math
from pygame.locals import *


# User-defined functions

def main():

   # Initialize pygame
   pygame.init()

   # Set window size and title, and frame delay
   surfaceSize = (500, 400) # window size
   windowTitle = 'Pong' #window title
   frameDelay = 0.005 # smaller is faster game

   # Create the window
   surface = pygame.display.set_mode(surfaceSize, 0, 0)
   pygame.display.set_caption(windowTitle)

   # create and initialize red dot and blue dot
   gameOver = False
   color1=pygame.Color('white')
   center1 = [250, 200]
   radius1=10
   speed1=[1,-1]
   location1=(50, 150)
   location2=(450, 150)
   size=(5, 100)
   rect1=pygame.Rect(location1,size)
   rect2=pygame.Rect(location2,size)
   r1=pygame.draw.rect(surface, color1, rect1)
   r2=pygame.draw.rect(surface, color1, rect2)

   # Draw objects
   pygame.draw.circle(surface, color1, center1, radius1, 0)

   # Refresh the display
   pygame.display.update()

   # Loop forever
   while True:
      # Handle events
      for event in pygame.event.get():
         if event.type == QUIT:
            pygame.quit()
            sys.exit()
         # Handle additional events

      # Update and draw objects for the next frame
     gameOver = update(surface,color1,center1,radius1,speed1,rect1,rect2)


      # Refresh the display
      pygame.display.update()

      # Set the frame speed by pausing between frames
      time.sleep(frameDelay)

def update(surface,color1,center1,radius1,speed1,rect1,rect2):
   # Check if the game is over. If so, end the game and
   # returnTrue. Otherwise, erase the window, move the dots and
   # draw the dots return False.
   # - surface is the pygame.Surface object for the window
   eraseColor=pygame.Color('Black')
   surface.fill(eraseColor)
   moveDot(surface,center1,radius1,speed1)
   pygame.draw.circle(surface,color1,center1,radius1,0)
   r1=pygame.draw.rect(surface, color1, rect1)
   r2=pygame.draw.rect(surface, color1, rect2)  
   if r1.collidepoint(center1) and speed1[0]<0:
      speed1[0]=-speed1[0]
   if r2.collidepoint(center1) and speed1[0]>0:
      speed1[0]=-speed1[0]


def moveDot(surface,center,radius,speed):
   #Moves the ball by changing the center of the ball by its speed
   #If dots hits left edge, top edge, right edge or bottom edge
   #of the window, the then the ball bounces
   size=surface.get_size()
   for coord in range(0,2):
      center[coord]=center[coord]+speed[coord]
      if center[coord]<radius:
         speed[coord]=-speed[coord]
      if center[coord]+radius>size[coord]: 
         speed[coord]=-speed[coord]

def Score(center1):
   # Score is how many times that ball hits the wall.
   Score=0
   while center1[0]==10:
      Score=Score+1
   return Score


def drawScore(center1,surface):
   FontSize=60
   FontColor=pygame.Color('White')
   score=str(Score(center1))
   String='Score : '
   font=pygame.font.SysFont(None, FontSize, True)
   surface1=font.render(String+score, True, FontColor,0)
   surface.blit(surface1,(0,0))



main()     

1 个答案:

答案 0 :(得分:1)

有很多问题。首先,你永远不会调用任何处理得分的代码。我想(printscoreupdate中)需要点击main的内容。

下一个问题是你的Score函数包含一个无限循环。您的while应该是if,因为您每帧只需要计算一个点:

def Score(center1):
   # Score is how many times that ball hits the wall.
   Score=0
   while center1[0]==10:
      Score=Score+1
   return Score

即使你解决了这个问题,它仍然无法正常工作。 Score函数只会返回01,因为每次调用函数时都会重置Score局部变量(注意,它也是一个非常糟糕的想法,让一个局部变量与它所在的函数同名。您可能需要使用全局变量(和global语句),或者您需要摆脱单独的函数并将得分跟踪逻辑放在主循环中,它可以修改局部变量。或者您可以返回一个特定值,表示应该增加分数,而不是总分。有很多方法可以做到。