gpt4 book ai didi

Python坐标函数

转载 作者:太空宇宙 更新时间:2023-11-03 21:10:18 26 4
gpt4 key购买 nike

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

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/

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