[英]How to calculate Score when ball hits the ball in pygame 'Pong'
最近,我編寫了一個pygame'Pong '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()
有很多問題。 首先,您永遠不會調用任何與樂譜有關的代碼。 我想您需要調用printscore
東西(在update
或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
函數只會返回0
或1
,因為每次調用該函數時都要重置Score
局部變量(請注意,擁有與該函數同名的局部變量也是一個很糟糕的主意在)。 您可能需要使用全局變量(和global
語句),或者您需要擺脫單獨的函數,並將分數跟蹤邏輯放入主循環中,在該循環中可以修改局部變量。 或者,您可以返回一個特定的值來指示應提高分數,而不是總分。 有很多方法可以做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.