gpt4 book ai didi

python - 我无法在 pygame python 中传递名为 window 的窗口

转载 作者:行者123 更新时间:2023-12-05 01:50:24 25 4
gpt4 key购买 nike

为什么我每次尝试将 Win 传递到 WIN 时它都不接受它如果可以请帮助我:

敌人类(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 ) )

我正在主循环中输入:

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

错误是:

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

窗口声明为:

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

完整代码:

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)

最佳答案

了解ClassesInstance Objects 。您需要创建 Enemy 类的实例对象:

enemy1 = Enemy(0, 0)

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

enemy1.draw(Win)

# [...]

在上面的示例中,enemy1Enemy 类的实例对象。

要管理多个敌人,您需要将它们放入一个enmeies列表中:

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

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

for enemie in enemies:
enemie.draw(Win)

# [...]

关于python - 我无法在 pygame python 中传递名为 window 的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73141759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com