python - 在乒乓球比赛中得分。如何在乒乓球比赛中更新分数?
问题描述
好的,所以我正在创建游戏乒乓球并且我目前正在设置分数。有一个小错误我无法修复。我尝试的代码是R2Score = R2Score + 1
让它每次离开屏幕时添加一个点。它告诉我这段代码有错误,我正在研究如何修复它。此外,当它要离开屏幕时,球会减慢速度,我对为什么也会发生这种情况感到困惑。
错误:
Traceback (most recent call last): File "/Users/name/Documents/PongGame.py", line 130, in <module> R2Score = R2Score + 1 TypeError: unsupported operand type(s) for +: 'pygame.Surface' and 'int'
# import the necessary modules
import pygame
import sys
import time
#initialize pygame
pygame.init()
screenSize=(700,500)
screen=pygame.display.set_mode((screenSize), 0)
# set the caption for the screen
pygame.display.set_caption("Pong Game")
# define colours you will be using
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
YELLOW = (255,255,0)
# set the size for the surface (screen)
screen_h = screen.get_height()
screen_w = screen.get_width()
cx = int(screen_w/2)
cy = int(screen_h/2)
#initialize variables for player
#variables for first rectangle
R1x = 660
R1y = 300
R1w = 10
R1h = 132
R1dx = 0
R1dy = 0
R1_score = 0
#variables for second rectangle
R2x = 10
R2y = 2
R2w = 10
R2h = 132
R2dx = 0
R2dy = 0
R2_score = 0
#ball variables
bx = cx
by = cy
dby = 3
dbx = 3
br = 5
cy = screen.get_height()/2
cx = screen.get_width()/2
# variable for scores
R1_score = 0
R2_score = 0
playerRect = pygame.Rect(R1x, R1y, R1w, R1h)
playerRect2 = pygame.Rect(R2x, R2y, R2w, R2h)
ballRect = pygame.Rect (cx,cy,30,30)
#speed
speed = 3
fontsize = 50
fontScore = pygame.font.SysFont('arial', 50)
fontScore = pygame.font.SysFont('arial', 50)
R1Score = fontScore.render(str(R1_score), True, (WHITE))
R2Score = fontScore.render(str(R2_score), True, (WHITE))
# speed of object "clock"
clock = pygame.time.Clock()
# set main loop to True so it will run
main = True
# main loop
while main:
for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
if event.type ==pygame.QUIT: # check to see if it was "x" at top right of screen
main = False # set the "main" variable to False to exit while loop
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_UP:
R1dx = 0
R1dy = -speed
elif event.key == pygame.K_DOWN:
R1dx = 0
R1dy = speed
if event.key == pygame.K_w:
R2dx = 0
R2dy = -speed
elif event.key == pygame.K_s:
R2dx = 0
R2dy = speed
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
R1dx = 0
R1dy = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
R2dx = 0
R2dy = 0
playerRect.y = playerRect.y + R1dy
playerRect2.y = playerRect2.y + R2dy
ballRect.move_ip(dbx,dby)
R1y = R1dy + R1y
R2y = R2dy + R2y
bx = dbx + bx
by = dby + by
if R1y >= screen_h - 80 or R1y < 0:
R1dy = 0
if R2y >= screen_h - 80 or R2y < 0:
R2dy = 0
if by >= screen_h - br:
dby = -1
if by <= 15:
dby = 1
if bx >= R2x - br and by - R2y > 0 and by - R2y <100:
dbx = -1
if bx >= R1x - br and by - R1y > 0 and by - R1y <100:
dbx = 1
if bx == 1:
R2Score = R2Score + 1
pause = True
if bx == 699:
R2Score = R2Score + 1
pause = True
#collision of ball
if ballRect.top <= 0:
dby = -dby
if ballRect.bottom >= screen_h:
dby = -dby
if ballRect.colliderect(playerRect2):
dbx = -dbx
if ballRect.colliderect(playerRect):
dbx = -dbx
screen.fill(BLACK)
# draw the shapes
pygame.draw.rect(screen, WHITE,(playerRect),0)
pygame.draw.rect(screen, WHITE,(playerRect2),0)
pygame.draw.circle(screen, RED,ballRect.center,br,0)
screen.blit(R1Score, (280,10))
screen.blit(R2Score, (400,10))
# we are using .flip() here, it basically works the same as .update()
time.sleep(0.005)
pygame.display.flip()
# quit pygame and exit the program (i.e. close everything down)
pygame.quit()
sys.exit()
解决方案
R2Score
是一个pygame.Surface
对象,那么你期望R2Score = R2Score + 1
什么?
您不能将 1 添加到Surface
.
如果你想增加分数,那么你必须(重新)渲染Surface
. 例如:
if bx == 1:
R1_score += 1
R1Score = fontScore.render(str(R1_score), True, (WHITE))
推荐阅读
- javascript - 盖茨比在线支付网关
- sql - 计算行前出现的次数
- c++ - 模板参数的使用顺序是什么?
- django - 如何从 Python Django 中删除数字海洋空间图像
- sql - Bigquery standardSQL:当前日期减去前一个日期,结果为天数?
- r - 如何在 quantstrat 中使用 sigPeak() 函数
- alfresco - 根据 .bpmn2 文件中的变量/条件的值将任务分配给特定的 alfresco 用户
- javascript - 即使在版本控制之后也无法重新加载 javascript 缓存
- html - 如何处理 CSS 媒体查询否定,包括不支持它的浏览器?
- javascript - 标题无在抽屉导航器中不起作用反应本机