Обьясните как здесь работает метод ball.score
import pygame
import sys
import random
from pygame_Ball import Ball
pygame.init()
sc = pygame.display.set_mode((600,400))
pygame.display.set_caption("Game")
icon = pygame.image.load("Gun/icon2.png")
pygame.display.set_icon(icon)
Clock = pygame.time.Clock()
pygame.time.set_timer(pygame.USEREVENT , 1000)
BLACK = (0,0,0)
Text = pygame.font.Font(r"C:\Users\77761\Desktop\Sborki/Text.otf",25)
Text_render = Text.render("SCORE : " ,1 , BLACK)
Text_pos = Text_render.get_rect(center = (75,30))
Running = True
WIDTH = 600
HEIGHT = 400
WHITE = (255,255,255)
RED = (255,0,0)
BLUE = (0,0,255)
GREEN = (0.255,0)
FPS = 60
balls_images = ({"path" : "Bomba.png" , "score" : 100},
{"path" : "Bomba2.png" , "score" : 150},
{"path" : "Bomba3.png" , "score" : 200})
balls_surf = [pygame.image.load(r"C:\Users\77761\Desktop\Sborki/" + score["path"]).convert_alpha() for score in balls_images]
town = pygame.image.load(r"C:\Users\77761\Desktop\Sborki/town.jpg").convert()
town1 = pygame.transform.scale(town , (600,428))
Lovitel = pygame.image.load(r"C:\Users\77761\Desktop\Sborki/Lovitel.png").convert_alpha()
Lovitel_rect = Lovitel.get_rect(centerx = WIDTH // 2 , bottom = HEIGHT - 5) # bottom - если что нижние координаты прямоугольника
speed = 10
def create_ball(group):
indx = random.randint(0 , len(balls_images) - 1)
x = random.randint(20 , WIDTH - 20)
speed = random.randint(1,5)
return Ball(x , speed , balls_surf[indx] ,balls_images[indx]["score"], group)
game_score = 0
def collidepoint():
global game_score
for ball in balls:
if Lovitel_rect.collidepoint(ball.rect.center):
game_score += ball.score # Как работает ball.score и почему он написан именно так
print(game_score)
ball.kill()
balls = pygame.sprite.Group()
while Running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.USEREVENT:
create_ball(balls)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
Lovitel_rect.x -= speed
if Lovitel_rect.x < 0:
Lovitel_rect.x = 0
elif keys[pygame.K_RIGHT]:
Lovitel_rect.x += speed
if Lovitel_rect.x > WIDTH - Lovitel_rect.width:
Lovitel_rect.x = WIDTH - Lovitel_rect.width
collidepoint()
sc.blit(town1 , (0,0))
sc.blit(Text_render , Text_pos)
TEXT = Text.render(str(game_score) ,1 , BLACK)
sc.blit(TEXT , (135,15))
balls.draw(sc)
sc.blit(Lovitel , Lovitel_rect)
pygame.display.update()
balls.update(HEIGHT)
Clock.tick(FPS)
# Работа самого обьекта
import pygame
class Ball(pygame.sprite.Sprite):
def __init__(self , x ,speed, surf , score, group):
pygame.sprite.Sprite.__init__(self)
self.score = score
self.image = surf
self.rect = self.image.get_rect(center = (x,0))
self.speed = speed
self.add(group)
def update(self,*args):
if self.rect.y < args[0] - 20:
self.rect.y += self.speed
else:
self.kill()
Ответы (1 шт):
Автор решения: CrazyElf
→ Ссылка
for ball in balls:
if Lovitel_rect.collidepoint(ball.rect.center):
game_score += ball.score
...
class Ball(pygame.sprite.Sprite):
def __init__(self , x ,speed, surf , score, group):
pygame.sprite.Sprite.__init__(self)
self.score = score
score - это поле класса Ball, тут просто перебираются объекты класса Ball и суммируется поле score всех этих объектов.
Поле - это переменная внутри класса, а метод - это функция внутри класса.
score - это переменная, а не функция.