Harsshavardhan Kannan
Harsshavardhan Kannan

Reputation: 45

I could not pass in the window called window in pygame python

why I whenever try to pass in Win into WIN it does not accept it if you could please help me :

The ENEMY Class:

# Enemy Class
class Enemy:
    def __init__( self , x , y ): 
        self.x = x
        self.y = y

    def move( self ):
        fpx = self.x - PlayerPos[0]
        fpy = self.y - PlayerPos[1]
        fpxx = fpx * (-1)
        fpyy = fpy * (-1)
        fp = [ fpx , fpy ]

        if fp[0] > PlayerPos[0] and fpxx < 0:
            fp[0] = fp[0] * (-1)

        if fp[0] > 10 or fp[0] < -10 :
            self.x = self.x + ( fp[0] ) ^0 * enemy_speed

        if fp[1] > PlayerPos[1] and fpyy < 0:
            fp[1] = fp[1] * (-1)

        if fp[1] > 10 or fp[1] < -10 :
            self.y = self.y + ( fp[1] ) ^0 * enemy_speed

    def draw( self , Win ):
        Win.blit( enemy_img ( self.x , self.y ) )

I am Inputting in the main loop :

    Enemy( 0 , 0 )
    Enemy.draw( Win )

The error is :

  File "FILE PATH", line 157, in <module>
    Enemy.draw( Win )
TypeError: Enemy.draw() missing 1 required positional argument: 'Win'

The Window is declared as :

DisplayInfo = pygame.display.Info()
SCREEN_WIDTH = DisplayInfo.current_w
SCREEN_HEIGHT = DisplayInfo.current_h
Win = pygame.display.set_mode(( SCREEN_WIDTH , SCREEN_HEIGHT ) , FULLSCREEN)

The FULL CODE:

from pygame import display
from pygame.locals import *

# Initiate Pygame
pygame.init()

# Visual Window
DisplayInfo = pygame.display.Info()
SCREEN_WIDTH = DisplayInfo.current_w
SCREEN_HEIGHT = DisplayInfo.current_h
Win = pygame.display.set_mode(( SCREEN_WIDTH , SCREEN_HEIGHT ) , FULLSCREEN)
display.set_caption("SMASH!")
Icon = pygame.image.load('Icon.png')
pygame.display.set_icon(Icon)

# imageLoader
player_base = pygame.image.load('images/Player/player_base.png')
player_base = pygame.transform.scale( player_base , ( 64 , 64 ))
player_default_eyes = pygame.image.load('images/Player/eyes/default_eyes.png')
player_default_eyes = pygame.transform.scale( player_default_eyes , ( 64 , 64 ))
enemy_img = pygame.image.load('images/enemy.png')
enemy_img = pygame.transform.scale( enemy_img , ( 64 , 64 ))



# Clock
clock = pygame.time.Clock()

# Variables
Red = ( 255 , 0 , 0 )
Green = ( 0 , 255 , 0 )
Blue = ( 0 , 0 , 255 )
Black = ( 0 , 0 , 0 )
White = ( 255 , 255 , 255 )
Grey = (200, 200, 200)

PlayerPos = [ 500 , 500 ]
player_right = False
player_left = False
player_up = False
player_down = False
player_speed = 7
player_alive = True

enemy_cap = 20
enemy_speed = 5

# Cosmetics
player_eyes = player_default_eyes

# Player Function
def player():
    if player_alive:
        Win.blit( player_base , (PlayerPos[0] , PlayerPos[1]))  
        try:
            Win.blit( player_eyes , (PlayerPos[0] , PlayerPos[1])) 
        except:
            pass


# Enemy Class
class Enemy:
    def __init__( self , x , y ): 
        self.x = x
        self.y = y

    def move( self ):
        fpx = self.x - PlayerPos[0]
        fpy = self.y - PlayerPos[1]
        fpxx = fpx * (-1)
        fpyy = fpy * (-1)
        fp = [ fpx , fpy ]

        if fp[0] > PlayerPos[0] and fpxx < 0:
            fp[0] = fp[0] * (-1)

        if fp[0] > 10 or fp[0] < -10 :
            self.x = self.x + ( fp[0] ) ^0 * enemy_speed

        if fp[1] > PlayerPos[1] and fpyy < 0:
            fp[1] = fp[1] * (-1)

        if fp[1] > 10 or fp[1] < -10 :
            self.y = self.y + ( fp[1] ) ^0 * enemy_speed

    def draw( self , Win ):
        Win.blit( enemy_img ( self.x , self.y ) )


##### main loop #####

Running = True
while Running:

    Win.fill(( 39 , 188 , 39 ))


    for event in pygame.event.get():
        if event.type == QUIT:
            Running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                player_up = True

            if event.key == pygame.K_s:
                player_down = True

            if event.key == pygame.K_a:
                player_left = True

            if event.key == pygame.K_d:
                player_right = True      

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                player_up = False

            if event.key == pygame.K_s:
                player_down = False

            if event.key == pygame.K_a:
                player_left = False

            if event.key == pygame.K_d:
                player_right = False                              
                

    if player_up:
        PlayerPos[1] -= player_speed

    if player_down:
        PlayerPos[1] += player_speed

    if player_right:
        PlayerPos[0] += player_speed

    if player_left:
        PlayerPos[0] -= player_speed

    if PlayerPos[0] <= 0 :
        PlayerPos[0] = 0

    if PlayerPos[0] >= SCREEN_WIDTH - 64 :
        PlayerPos[0] = SCREEN_WIDTH - 64

    if PlayerPos[1] <= 0 :
        PlayerPos[1] = 0

    if PlayerPos[1] >= SCREEN_HEIGHT - 64 :
        PlayerPos[1] = SCREEN_HEIGHT - 64

    player()

    Enemy( 0 , 0 )
    Enemy.draw( Win )

    pygame.display.update()
    clock.tick(60)

Upvotes: 2

Views: 46

Answers (1)

Rabbid76
Rabbid76

Reputation: 210978

Read about Classes and Instance Objects. You need to create an instance object of the Enemy class:

enemy1 = Enemy(0, 0)

Running = True
while Running:
    # [...]

    enemy1.draw(Win)

    # [...]

In the above example, enemy1 is an instance object of the Enemy class.

To manage multiple enemies, you need to put them into one list of enmeies:

enemies = []
enemies.append(Enemy(100, 50))
enemies.append(Enemy(200, 50))

Running = True
while Running:
    # [...]

    for enemie in enemies:
        enemie.draw(Win)

    # [...]

Upvotes: 5

Related Questions