我为乒乓球创建了一个 Score class,当我添加我的变量 count += 1 它应该继续添加但只停留在一个
I have created a Score class for pong, when I add my variable count += 1 it should continue to add but only stays at one
这是我的代码:
import pygame
import ball
import paddle
from pygame.locals import*
class Score:
def __init__(self, ball, paddle):
self.numbers = [pygame.image.load('digit_0.bmp'),
pygame.image.load('digit_1.bmp'),
pygame.image.load('digit_2.bmp'),
pygame.image.load('digit_3.bmp'),
pygame.image.load('digit_4.bmp'),
pygame.image.load('digit_5.bmp'),
pygame.image.load('digit_6.bmp'),
pygame.image.load('digit_7.bmp'),
pygame.image.load('digit_8.bmp'),
pygame.image.load('digit_9.bmp'),
]
self.player = 0
self.computer = 0
self.secdig = 0
self.ball = ball
self.paddle = paddle
无法弄清楚为什么当计数器应该继续增加 1 个增量时点(桨)只停留在 1。
def points(self, paddle):
count = 0
point = 0
if self.ball.x < paddle.getX():
count += 1
print(count)
def paint(self, surface):
surface.blit(self.numbers[self.computer], (200, 30))
surface.blit(self.numbers[self.secdig], (160, 30))
您必须将计数设为 class 变量。
def __init__(self, ball, paddle):
...
self.count = 0
def points(self, paddle):
point = 0
if self.ball.x < paddle.getX():
self.count += 1
print(count)
如果在 points()
方法中将 count
设置为 0,则每次调用该方法时,计数都会再次设置为 0。
添加
self.count = 0
添加到您的 class,然后在 points 函数中使用它。
def points(self, paddle):
point = 0
if self.ball.x < paddle.getX():
self.count += 1
这是我的代码:
import pygame
import ball
import paddle
from pygame.locals import*
class Score:
def __init__(self, ball, paddle):
self.numbers = [pygame.image.load('digit_0.bmp'),
pygame.image.load('digit_1.bmp'),
pygame.image.load('digit_2.bmp'),
pygame.image.load('digit_3.bmp'),
pygame.image.load('digit_4.bmp'),
pygame.image.load('digit_5.bmp'),
pygame.image.load('digit_6.bmp'),
pygame.image.load('digit_7.bmp'),
pygame.image.load('digit_8.bmp'),
pygame.image.load('digit_9.bmp'),
]
self.player = 0
self.computer = 0
self.secdig = 0
self.ball = ball
self.paddle = paddle
无法弄清楚为什么当计数器应该继续增加 1 个增量时点(桨)只停留在 1。
def points(self, paddle):
count = 0
point = 0
if self.ball.x < paddle.getX():
count += 1
print(count)
def paint(self, surface):
surface.blit(self.numbers[self.computer], (200, 30))
surface.blit(self.numbers[self.secdig], (160, 30))
您必须将计数设为 class 变量。
def __init__(self, ball, paddle):
...
self.count = 0
def points(self, paddle):
point = 0
if self.ball.x < paddle.getX():
self.count += 1
print(count)
如果在 points()
方法中将 count
设置为 0,则每次调用该方法时,计数都会再次设置为 0。
添加
self.count = 0
添加到您的 class,然后在 points 函数中使用它。
def points(self, paddle):
point = 0
if self.ball.x < paddle.getX():
self.count += 1