0

I am trying to build a simple "drop the ball" minigame using pygame - first time using this module after trying & adding a platform sprite for the game I tried to run the code and I get a ton of errors with imports inside the pygame module, any suggestions?

If the errors are cleared I should get a screen if the platform sprite on it

code: main()

# the main page of the application - contains the main function
import ballGame

# object of the game
game = ballGame.BallGame()


def main():
    game.game_run()


if __name__ == '__main__':
    main()

ballGame()

import sys
import pygame
import service
import platform

# global variables
global screen, platform_group


# a class the holds the whole game
def game_run():
    # a variable to keep our game loop running
    running = True
    # game loop
    while running:
        # for loop through the event queue
        for event in pygame.event.get():
            # Check for a QUIT event if true, exit
            if event.type == pygame.QUIT:
                running = False
                sys.exit()
        # updating the screen
        pygame.display.flip()
        platform_group.draw(screen)


class BallGame(service.Service):
    # an empty constructor method
    def __init__(self):
        # inheriting all the values of the service class
        super().__init__(self, 500, 500)
        # getting the values for the screen size
        global screen
        screen = pygame.display.set_mode((self.get_w(), self.get_h()))
        # setting up a caption on the top of screen
        pygame.display.set_caption('ball game')
        # getting the screen back color while updating the screen
        screen.fill(self.get_color_light_blue())
        # creating an object for our ball platform
        platform1 = platform_.Platform(50, 50, self.get_platform_start_loc()[0], self.get_platform_start_loc()[1],
                                       self.get_color_crimson_red())

        # grouping the objects to be able to show them
        global platform_group
        platform_group = pygame.sprite.Group()
        platform_group.add(platform1)
        # updating the screen
        pygame.display.flip()

platform()

# a simple platform code for the ball to use
import pygame
import service


# a sprite class that creates a platform
class Platform_(pygame.sprite.Sprite, service.Service):
    # a simple constructor
    def __init__(self, width, height, pos_x, pos_y, color):
        # inheriting all the values of the super classes
        super().__init__()
        # getting the size and color of the object
        self.image = pygame.Surface([width, height])
        self.image.fill(self.get_color_crimson_red())
        # getting the general hitbox of the object //uses the width & height of the image surface method
        self.rect = self.image.get_rect()

service()

# a service class used to create all the 'helping' items for the game

class Service:
    # empty constructor function
    def __init__(self, screen_w, screen_h, color_yellow=(255, 255, 0), color_light_blue=(135, 206, 235),
                 color_crimson_red=(220, 20, 60), platform_started_loc=(0, 250)):
        # class default variables
        self.__color_yellow = color_yellow
        self.__color_light_blue = color_light_blue
        self.__color_crimson_red = color_crimson_red
        self.__platform_started_loc = platform_started_loc
        # class changed variables
        self.__screen_w = screen_w
        self.__screen_h = screen_h

    # get methods that returns the values of the
    def get_color_yellow(self):
        return self.__color_yellow

    def get_color_light_blue(self):
        return self.__color_light_blue

    def get_color_crimson_red(self):
        return self.__color_crimson_red

    def get_w(self):
        return self.__screen_w

    def get_h(self):
        return self.__screen_h

    def get_platform_start_loc(self):
        return self.__platform_started_loc

errors:

Traceback (most recent call last):
  File "C:\Users\bratt\Desktop\liran\py\HomeWork\Catch The Ball\main.py", line 2, in <module>
    import ballGame
  File "C:\Users\bratt\Desktop\liran\py\HomeWork\Catch The Ball\ballGame.py", line 3, in <module>
    import pygame
  File "C:\Users\bratt\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pygame\__init__.py", line 298, in <module>
    import pygame.surfarray
  File "C:\Users\bratt\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pygame\surfarray.py", line 47, in <module>
    import numpy
  File "C:\Users\bratt\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\numpy\__init__.py", line 140, in <module>
    from . import core
  File "C:\Users\bratt\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\numpy\core\__init__.py", line 100, in <module>
    from . import _add_newdocs_scalars
  File "C:\Users\bratt\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\numpy\core\_add_newdocs_scalars.py", line 87, in <module>
    add_newdoc_for_scalar_type('byte', [],
  File "C:\Users\bratt\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\numpy\core\_add_newdocs_scalars.py", line 62, in add_newdoc_for_scalar_type
    alias_doc += ''.join(":Alias on this platform ({} {}): `numpy.{}`: {}.\n    ".format(platform.system(), platform.machine(), alias, doc)
  File "C:\Users\bratt\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\numpy\core\_add_newdocs_scalars.py", line 62, in <genexpr>
    alias_doc += ''.join(":Alias on this platform ({} {}): `numpy.{}`: {}.\n    ".format(platform.system(), platform.machine(), alias, doc)
AttributeError: module 'platform' has no attribute 'system'
  • 1
    The problem is that `platform` is a standard library module that Pygame wants to use, but it will find your own `platform.py` instead. This is solved by using a different name. Please see the linked duplicate for details. – Karl Knechtel Dec 02 '22 at 16:32

0 Answers0