我需要手动检查一张图像是否撞到另一张图像。我似乎无法得到结果......拜托这让我发疯了!我已经尝试了2天,它不会工作。
我没有包含其他一些代码,例如 Menu().main(),但它运行良好。我不明白为什么当僵尸在玩家身上时它不会返回 true。
class Game():
def setup(self):
global tiles
for files in os.listdir(os.curdir):
if files.endswith(".png") and files != "Player.png" and files != "Zombie.png":
tilees.append(files)
numtil = tilees.count(files)
print("a " + str(numtil))
fli = pygame.image.load(files).convert_alpha()
i = 0
while i <= 1000:
tiles.append(fli)
i = i + 1
global TileLen
TileLen = tiles.count(fli)
Game().Game()
def Game(self):
global zomx, zomy, zombie, MMS, step, zombieRect
zombieRect = []
zomx = []
zomy = []
zombie = []
step = 0
HP = 15
Map = pygame.image.load("BG.png").convert()
Map = pygame.transform.scale(Map, (pygame.display.list_modes()[0]))
person = pygame.image.load("Player.png").convert()
person = pygame.transform.scale(person, (30, 30))
dagger = pygame.image.load("Dagger.png").convert_alpha()
dagger = pygame.transform.scale(dagger, (13, 13))
screen.blit(Map, (0, 0))
perx = 800
pery = 700
ai().zombieadd(MMS)
screen.blit(person, (perx, pery))
screen.blit(dagger, (perx+20, pery-5))
global thething
thething = 0
for crap in zombie:
screen.blit(zombie[thething], (zomx[thething], zomy[thething]))
print(thething)
thething = thething + 1
pygame.display.flip()
awesome = 1
fttt = 0
ff = 0
ofx = 0
ofy = 0
while 1:
print(perx, pery)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_w:
pery = pery - 5
if fttt != 1:
if awesome == 4:
dagger = pygame.transform.rotate(dagger, 270)
person = pygame.transform.rotate(person, 270)
elif awesome == 3:
dagger = pygame.transform.rotate(dagger, 90)
person = pygame.transform.rotate(person, 90)
elif awesome == 2:
dagger = pygame.transform.rotate(dagger, 180)
person = pygame.transform.rotate(person, 180)
person = pygame.transform.rotate(person, 0)
dagger = pygame.transform.rotate(dagger, 0)
awesome = 1
fttt = 1
elif event.key == K_s:
pery = pery + 5
if fttt != 2:
if awesome == 4:
dagger = pygame.transform.rotate(dagger, 270)
person = pygame.transform.rotate(person, 270)
elif awesome == 3:
dagger = pygame.transform.rotate(dagger, 90)
person = pygame.transform.rotate(person, 90)
person = pygame.transform.rotate(person, 180)
dagger = pygame.transform.rotate(dagger, 180)
awesome = 2
fttt = 2
elif event.key == K_a:
perx = perx - 5
if fttt != 3:
if awesome == 2:
dagger = pygame.transform.rotate(dagger, 180)
person = pygame.transform.rotate(person, 180)
elif awesome == 4:
dagger = pygame.transform.rotate(dagger, 270)
person = pygame.transform.rotate(person, 270)
person = pygame.transform.rotate(person, 270)
dagger = pygame.transform.rotate(dagger, 270)
awesome = 3
fttt = 3
elif event.key == K_d:
perx = perx + 5
if fttt != 4:
if awesome == 2:
dagger = pygame.transform.rotate(dagger, 180)
person = pygame.transform.rotate(person, 180)
elif awesome == 3:
dagger = pygame.transform.rotate(dagger, 90)
person = pygame.transform.rotate(person, 90)
person = pygame.transform.rotate(person, 90)
dagger = pygame.transform.rotate(dagger, 90)
awesome = 4
fttt = 4
elif event.key == K_e:
if awesome == 1:
while ofx >= -6:
ofx = ofx - 1
screen.blit(dagger, (perx+20, pery-5+ofx))
pygame.time.delay(1)
pygame.display.update()
if ofx <= -6:
ofx = 0
elif awesome == 2:
while ofx >= -6:
ofx = ofx - 1
screen.blit(dagger, (perx-5, pery+25-ofx))
pygame.time.delay(1)
pygame.display.update()
if ofx <= -6:
ofx = 0
if awesome == 3:
while ofx >= -6:
ofx = ofx - 1
screen.blit(dagger, (perx-5+ofx, pery-5))
pygame.time.delay(1)
pygame.display.update()
if ofx <= -6:
ofx = 0
if awesome == 4:
while ofx >= -6:
ofx = ofx - 1
screen.blit(dagger, (perx+20-ofx, pery+25))
pygame.time.delay(1)
pygame.display.update()
if ofx <= -6:
ofx = 0
screen.blit(Map, (0, 0))
screen.blit(person, (perx, pery))
if awesome == 1:
screen.blit(dagger, (perx+20, pery-5))
elif awesome == 2:
screen.blit(dagger, (perx-5, pery+25))
elif awesome == 3:
screen.blit(dagger, (perx-5, pery-5))
else:
screen.blit(dagger, (perx+20, pery+25))
text = "Health: "+str(HP)
font = pygame.font.Font(None, 32)
ttex = font.render(text, True, (255, 0, 0))
screen.blit(ttex, (30, 30))
thecrap = 0
while thecrap < thething:
print(thecrap)
screen.blit(zombie[thecrap], (zomx[thecrap], zomy[thecrap]))
if perx >= zomx[thecrap]:
loop = 0
while loop <= 5:
zomx[thecrap] = zomx[thecrap] + .09
loop = loop + 1
if pery >= zomy[thecrap]:
loop = 0
while loop <= 5:
zomy[thecrap] = zomy[thecrap] + .09
loop = loop + 1
if perx <= zomx[thecrap]:
loop = 0
while loop <= 5:
zomx[thecrap] = zomx[thecrap] - .09
loop = loop + 1
if pery <= zomy[thecrap]:
loop = 0
while loop <= 5:
zomy[thecrap] = zomy[thecrap] - .09
loop = loop + 1
if person.get_rect().contains(pygame.Rect(zomx[thecrap], zomy[thecrap], 30, 30)):
HP = HP - .5
thecrap = thecrap + 1
thecrap = 0
pygame.display.update()
if HP <= 0:
print("Game over! Sorry!")
while 1:
abss = font.render("Sorry, but you lost! Press any key to continue...", True, (255, 255, 255))
pygame.display.update()
screen.blit(abss, (90, 90))
for event in pygame.event.get():
Menu().main()
class ai():
def zombieadd(self, Num):
global zomx, zomy, zombie, zombieRect
zombieRect = []
zombie = []
global step
step = 0
print(Num)
while Num > step:
global zomx, zomy, zombie
print(step)
zomx.append(random.randint(0, 600))
zomy.append(random.randint(0, 600))
zombieI = pygame.image.load("Zombie.png").convert()
zombie.append(zombieI)
zombie[step] = pygame.transform.scale(zombie[step], (30, 30))
print(zomx[step], zomy[step])
zombieRect.append(zombie[step].get_rect())
step = step + 1
Num = 0
return 0
Menu().main()
class Game():
def setup(self):
global tiles
for files in os.listdir(os.curdir):
if files.endswith(".png") and files != "Player.png" and files != "Zombie.png":
tilees.append(files)
numtil = tilees.count(files)
print("a " + str(numtil))
fli = pygame.image.load(files).convert_alpha()
i = 0
while i <= 1000:
tiles.append(fli)
i = i + 1
global TileLen
TileLen = tiles.count(fli)
Game().Game()
def Game(self):
global zomx, zomy, zombie, MMS, step, zombieRect
zombieRect = []
zomx = []
zomy = []
zombie = []
step = 0
HP = 15
Map = pygame.image.load("BG.png").convert()
Map = pygame.transform.scale(Map, (pygame.display.list_modes()[0]))
person = pygame.image.load("Player.png").convert()
person = pygame.transform.scale(person, (30, 30))
dagger = pygame.image.load("Dagger.png").convert_alpha()
dagger = pygame.transform.scale(dagger, (13, 13))
screen.blit(Map, (0, 0))
perx = 800
pery = 700
ai().zombieadd(MMS)
screen.blit(person, (perx, pery))
screen.blit(dagger, (perx+20, pery-5))
global thething
thething = 0
for crap in zombie:
screen.blit(zombie[thething], (zomx[thething], zomy[thething]))
print(thething)
thething = thething + 1
pygame.display.flip()
awesome = 1
fttt = 0
ff = 0
ofx = 0
ofy = 0
while 1:
print(perx, pery)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_w:
pery = pery - 5
if fttt != 1:
if awesome == 4:
dagger = pygame.transform.rotate(dagger, 270)
person = pygame.transform.rotate(person, 270)
elif awesome == 3:
dagger = pygame.transform.rotate(dagger, 90)
person = pygame.transform.rotate(person, 90)
elif awesome == 2:
dagger = pygame.transform.rotate(dagger, 180)
person = pygame.transform.rotate(person, 180)
person = pygame.transform.rotate(person, 0)
dagger = pygame.transform.rotate(dagger, 0)
awesome = 1
fttt = 1
elif event.key == K_s:
pery = pery + 5
if fttt != 2:
if awesome == 4:
dagger = pygame.transform.rotate(dagger, 270)
person = pygame.transform.rotate(person, 270)
elif awesome == 3:
dagger = pygame.transform.rotate(dagger, 90)
person = pygame.transform.rotate(person, 90)
person = pygame.transform.rotate(person, 180)
dagger = pygame.transform.rotate(dagger, 180)
awesome = 2
fttt = 2
elif event.key == K_a:
perx = perx - 5
if fttt != 3:
if awesome == 2:
dagger = pygame.transform.rotate(dagger, 180)
person = pygame.transform.rotate(person, 180)
elif awesome == 4:
dagger = pygame.transform.rotate(dagger, 270)
person = pygame.transform.rotate(person, 270)
person = pygame.transform.rotate(person, 270)
dagger = pygame.transform.rotate(dagger, 270)
awesome = 3
fttt = 3
elif event.key == K_d:
perx = perx + 5
if fttt != 4:
if awesome == 2:
dagger = pygame.transform.rotate(dagger, 180)
person = pygame.transform.rotate(person, 180)
elif awesome == 3:
dagger = pygame.transform.rotate(dagger, 90)
person = pygame.transform.rotate(person, 90)
person = pygame.transform.rotate(person, 90)
dagger = pygame.transform.rotate(dagger, 90)
awesome = 4
fttt = 4
elif event.key == K_e:
if awesome == 1:
while ofx >= -6:
ofx = ofx - 1
screen.blit(dagger, (perx+20, pery-5+ofx))
pygame.time.delay(1)
pygame.display.update()
if ofx <= -6:
ofx = 0
elif awesome == 2:
while ofx >= -6:
ofx = ofx - 1
screen.blit(dagger, (perx-5, pery+25-ofx))
pygame.time.delay(1)
pygame.display.update()
if ofx <= -6:
ofx = 0
if awesome == 3:
while ofx >= -6:
ofx = ofx - 1
screen.blit(dagger, (perx-5+ofx, pery-5))
pygame.time.delay(1)
pygame.display.update()
if ofx <= -6:
ofx = 0
if awesome == 4:
while ofx >= -6:
ofx = ofx - 1
screen.blit(dagger, (perx+20-ofx, pery+25))
pygame.time.delay(1)
pygame.display.update()
if ofx <= -6:
ofx = 0
screen.blit(Map, (0, 0))
screen.blit(person, (perx, pery))
if awesome == 1:
screen.blit(dagger, (perx+20, pery-5))
elif awesome == 2:
screen.blit(dagger, (perx-5, pery+25))
elif awesome == 3:
screen.blit(dagger, (perx-5, pery-5))
else:
screen.blit(dagger, (perx+20, pery+25))
text = "Health: "+str(HP)
font = pygame.font.Font(None, 32)
ttex = font.render(text, True, (255, 0, 0))
screen.blit(ttex, (30, 30))
thecrap = 0
while thecrap < thething:
print(thecrap)
screen.blit(zombie[thecrap], (zomx[thecrap], zomy[thecrap]))
if perx >= zomx[thecrap]:
loop = 0
while loop <= 5:
zomx[thecrap] = zomx[thecrap] + .09
loop = loop + 1
if pery >= zomy[thecrap]:
loop = 0
while loop <= 5:
zomy[thecrap] = zomy[thecrap] + .09
loop = loop + 1
if perx <= zomx[thecrap]:
loop = 0
while loop <= 5:
zomx[thecrap] = zomx[thecrap] - .09
loop = loop + 1
if pery <= zomy[thecrap]:
loop = 0
while loop <= 5:
zomy[thecrap] = zomy[thecrap] - .09
loop = loop + 1
if person.get_rect().contains(zombieRect)):
HP = HP - .5
thecrap = thecrap + 1
thecrap = 0
pygame.display.update()
if HP <= 0:
print("Game over! Sorry!")
while 1:
abss = font.render("Sorry, but you lost! Press any key to continue...", True, (255, 255, 255))
pygame.display.update()
screen.blit(abss, (90, 90))
for event in pygame.event.get():
Menu().main()
class ai():
def zombieadd(self, Num):
global zomx, zomy, zombie, zombieRect
zombieRect = []
zombie = []
global step
step = 0
print(Num)
while Num > step:
global zomx, zomy, zombie
print(step)
zomx.append(random.randint(0, 600))
zomy.append(random.randint(0, 600))
zombieI = pygame.image.load("Zombie.png").convert()
zombie.append(zombieI)
zombie[step] = pygame.transform.scale(zombie[step], (30, 30))
print(zomx[step], zomy[step])
zombieRect.append(zombie[step].get_rect())
step = step + 1
Num = 0
return 0
Menu().main()