-1

I'm making a simple parody of the game Chippy. In general, the monster(alien) must shoot and we must dodge and shoot back. I made it so that the picture of the ball is added to the array with all the other balls and removed when leaving the screen, but for some reason only one ball is drawn, and the rest are added to the array but do not appear on the screen. I am using module tkinter to get screen dimensions:

P.S. the ball is the bullets that the monster(alien) shoots, it's just that the bullets are already taken

# === ball ===

import pygame
from tkinter import *

root = Tk()

class Ball():
    def __init__(self, screen, alien):
        self.screen = screen
        self.ball_image = pygame.image.load('images/pixil-frame-0 (1).png')
        self.rect = self.ball_image.get_rect()
        self.rect.centerx = alien.x
        self.rect.centery = alien.y

        self.all_ball = []
        self.test = True

        self.timer = 0

    def draw_ball(self, alien):
        self.screen.blit(self.ball_image, (self.rect.centerx, self.rect.centery))

    def move(self):
        self.rect.centery += 15

    def create_ball(self, screen, alien):
        for ball in self.all_ball.copy():
            if ball.rect.top <= 0 or ball.rect.left <= 0 or ball.rect.right >= root.winfo_screenwidth() or ball.rect.bottom >= root.winfo_screenheight():
                self.all_ball.remove(ball)

        if self.test == True:
            self.all_ball.append(Ball(screen, alien))

For now, I want to get the monster(alien) to shoot these balls in different directions.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Kirill
  • 1
  • 1
  • 1
    Take a look again at [ask]. There should be a clear and specific question, such as an explanation of what's gone wrong so far – camille Feb 03 '23 at 16:20
  • 1
    `all_ball` should not be an attribute of `Ball`, because `Ball` is a class for one single `Ball` object. `all_ball` should be a variable in global namespace. – Rabbid76 Feb 03 '23 at 19:15
  • Now all_ball are called balls and the ball is also taken out, but nothing has changed, only one is drawn, it seems that the rest of the balls are also there, only the main picture is drawn, but I don’t know how to draw each element of the array, maybe this is the reason? self.screen.blit(self.ball_image, (self.rect.centerx, self.rect.centery)) But I tried to draw a single element of the array through the for loop, but this only led to numerous errors – Kirill Feb 04 '23 at 06:22
  • Changed version def create_ball(self, screen, alien, balls): for ball in balls.copy(): if self.rect.centery >= root.winfo_screenheight(): balls.remove(ball) if self.test == True: balls.append(Ball(screen, alien)) – Kirill Feb 04 '23 at 06:23

1 Answers1

0

all_ball should not be an attribute of Ball, because Ball is a class for one single Ball object. all_ball should be a variable in global namespace.

balls = []
def update_balls(screen, alien, create_new):

    screen_rect = screen.get_rect()    
    for ball in balls[:]:
        if not screen_rect.colliderect(ball.rect):
            balls.remove(ball)

    if create_new:
        balls.append(Ball(screen, alien))

Draw the balls in a loop:

for ball in balls:
    ball.draw_ball(screen)

However the Pygame solution would be to use pygame.sprite.Sprite and pygame.sprite.Group. See How can I add objects to a "pygame.sprite.Group()"? and What does pygame.sprite.Group() do.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you so much for your help, it’s a pity that I didn’t understand much) With the help pygame.sprite.Sprite I made bullets for a flying ship, for which we control, but the alien wants to make beautiful bullets, and not just squares of the same color. For several weeks I have been thinking about how to do this, but for some reason all my ideas do not work correctly( What does the colon mean balls[:] ? – Kirill Feb 04 '23 at 08:25