我已经为pong创建了一个Score类,当我添加变量count+=1时,它应该继续添加,但只停留在on

2024-05-02 12:35:49 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

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增量。你知道吗

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))

Tags: imageimportselfdefcountloadsurfacepygame
2条回答

你必须把count作为一个类变量。你知道吗

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,则每次调用该方法时,count都会再次设置为0。你知道吗

添加

self.count = 0

然后在积分函数中使用它。

def points(self, paddle):
    point = 0
    if self.ball.x < paddle.getX():
        self.count += 1

相关问题 更多 >