Python坐标函数

标签 python function class pygame coordinates

我已将木乃伊编程到我的游戏中,我希望它们跟随我的玩家。我想创建一个函数,可以用来应用于我选择创建的所有敌人,但我拥有的函数不太有效。木乃伊会生成,但它们不会跟随玩家。如何更改该功能以使其正常工作?

import random
import pygame
import sys
import time
from pygame import mixer

screenx = 800
screeny = 600

clock = pygame.time.Clock()

#PICTURES
#name = pygame.display.set_caption('Brutal Survival')
#win = pygame.display.set_mode((screenx,screeny))
#player_right = pygame.image.load('maincharright.png')
#player_left = pygame.image.load('maincharleft.png')
#player_up = pygame.image.load('maincharup.png')
#player_down = pygame.image.load('mainchardown.png')
#background = pygame.image.load('background.jpg')
#mummychar = pygame.image.load('mummychar.png')

randomspawnabove = (random.randint(0, screenx), -100)
randomspawnbelow = (random.randint(0, screenx), (screeny + 100))
randomspawnleft = (-100, random.randint(0, screeny))
randomspawnright = ((screenx + 100), random.randint(0, screeny))

enemyalive = True
multiplier = 2
level = 1
nextlevel = True
spawn = True
up = True
down = False
left = False
right = False
run = True


class player():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.alive = True
        self.vel = 5

class enemy():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.alive = False
        self.vel = 2

def enemyfollow(self):
    if self.alive == True and self.x > mainChar.x:
        self.x = self.x - self.vel
    if self.alive == True and self.x < mainChar.x:
        self.x = self.x + self.vel
    if self.alive == True and self.y > mainChar.y:
        self.y = self.y - self.vel
    if self.alive == True and self.y < mainChar.y:
        self.y = self.y + self.vel


def redrawScreen():
    pygame.display.update()
    if run == True:
        win.blit(background, (0,0))
    if up == True and left == False and right == False and down == False:
        win.blit(player_up, (mainChar.x, mainChar.y))
    if up == False and left == False and right == False and down == True:
        win.blit(player_down, (mainChar.x, mainChar.y))
    if up == False and left == False and right == True and down == False:
        win.blit(player_right, (mainChar.x, mainChar.y))
    if up == False and left == True and right == False and down == False:
        win.blit(player_left, (mainChar.x, mainChar.y))
    if enemyalive == True:
        win.blit(mummychar, (mummy1.x, mummy1.y))
        enemyfollow(mummy1)


mainChar = player(screenx/2 - 30, screeny/2, 60, 60)
maxenemies = (level * multiplier)
mummy_Spawn = [randomspawnleft, randomspawnright, randomspawnabove, randomspawnbelow]
mummy1 = enemy(*random.choice(mummy_Spawn), 50, 50)

while run:
    clock.tick(60)

    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()

    for x in range(maxenemies):
        Enemies.append(enemy(*random.choice(mummy_Spawn), 50, 50))

    if nextlevel == True:
        if level >= 1:
            enemyalive = True

    if keys[pygame.K_ESCAPE]:
        run = False
        pygame.quit()
    if keys[pygame.K_UP]:
        mainChar.y = mainChar.y - mainChar.vel
        up = True
        down = False
        left = False
        right = False
    if keys[pygame.K_DOWN]:
        mainChar.y = mainChar.y + mainChar.vel
        down = True
        left = False
        up = False
        right = False
    if keys[pygame.K_RIGHT]:
        mainChar.x = mainChar.x + mainChar.vel
        right = True
        left = False
        up = False
        down = False
    if keys[pygame.K_LEFT]:
        mainChar.x = mainChar.x - mainChar.vel
        left = True
        right = False
        down = False
        up = False
    if (mainChar.y + mainChar.height) > screeny:
        mainChar.y = mainChar.y - 5
    if mainChar.y < 0:
        mainChar.y = mainChar.y + 5
    if mainChar.x < 0:
        mainChar.x = mainChar.x + 5
    if (mainChar.x + mainChar.width) > screenx:
        mainChar.x = mainChar.x - 5

    redrawScreen()

最佳答案

if 语句的一部分是 if self == True,但 self 为 False

关于Python坐标函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55112254/

相关文章:

python - 用 "#"替换字母并反转

windows - WinAPI Unicode 和 ANSI 函数

python - 如何使用函数本身返回的值重复函数?

html - 类和 ID 在样式标签中不起作用

python - 使用问题标记提取已知两个标记之间的子字符串

python - HTTP/1.1 中的 400 Bad Request 但 HTTP/1.0 中没有

python - 启用 Anaconda 中 IPython qtconsole 的 Gnome/GTK 集成

javascript 函数未定义错误。句法?

java - 如何与另一个类的 GUI 通信

ruby - 使用 initialize 定义带参数的类方法