gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:43 24 4
gpt4 key购买 nike

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

但是,我在这里遇到了一个问题。因为我想要得到的分数等于球击中 window 边缘的次数。对于我的程序,当球撞到墙壁时,得分不会增加,球也会停止移动。我不知道原因,我想在这里找到答案。

非常感谢!

代码:

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(在 updatemain 中)。

下一个问题是您的 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 语句),或者需要删除单独的函数并将分数跟踪逻辑放入主循环中,以便它可以修改局部变量。或者您可以返回一个特定值,表明分数应该增加,而不是总分。有很多方法可以做到这一点。

关于python - 如何在pygame中计算球击中球时的得分 'Pong',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33292890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com