Har*_*nan 2 python pygame class sprite python-3.x
为什么我每次尝试将 Win 传递到 WIN 时它都不接受它,如果你能帮助我的话:
敌人等级:
# 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 ) )
Run Code Online (Sandbox Code Playgroud)
我在主循环中输入:
Enemy( 0 , 0 )
Enemy.draw( Win )
Run Code Online (Sandbox Code Playgroud)
错误是:
File "FILE PATH", line 157, in <module>
Enemy.draw( Win )
TypeError: Enemy.draw() missing 1 required positional argument: 'Win'
Run Code Online (Sandbox Code Playgroud)
窗口声明为:
DisplayInfo = pygame.display.Info()
SCREEN_WIDTH = DisplayInfo.current_w
SCREEN_HEIGHT = DisplayInfo.current_h
Win = pygame.display.set_mode(( SCREEN_WIDTH , SCREEN_HEIGHT ) , FULLSCREEN)
Run Code Online (Sandbox Code Playgroud)
完整代码:
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)
Run Code Online (Sandbox Code Playgroud)
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)
# [...]
Run Code Online (Sandbox Code Playgroud)
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)
# [...]
Run Code Online (Sandbox Code Playgroud)