我正在制作一个类似于简化Civ 5的游戏,它使用for range函数在每个图块上创建一个雾,因此所有雾图块都具有相同的名称。因此,当我测试某个装置是否看到雾并因此消除雾时,它仅使用最后创建的雾砖,因为它们的名称相同。那么我如何同时影响所有名为“ Cloud”的精灵?
我试图检查Cloud Sprite的x和y是否在单位的视线范围内。
这是整个代码,"checkforfog"
函数将检查我刚才解释的内容
import pygame
from threading import Thread
import random
from random import randint
from pygame.locals import *
import time
import os
import sys
pygame.font.init()
clock = pygame.time.Clock()
black = (0, 0, 0)
red = (255, 0, 0)
white = (255, 255, 255)
green = (0, 150, 0)
blue = (30, 144, 255)
light_blue = (40, 191, 255)
pygame.init()
pygame.mixer.init()
walk_sound = pygame.mixer.Sound("walk.wav")
pygame.mixer.music.load("music.wav")
walk_sound.set_volume(10)
screen = pygame.display.set_mode((1920, 1080),pygame.FULLSCREEN)
numlist = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
font1 = pygame.font.Font('celtic.ttf', 25)
font2 = pygame.font.Font('celtic.ttf', 45)
turnnum = 0
starter = 0
mx,my = pygame.mouse.get_pos()
buildtabnum = 0
bvar = 1
gold = 100
food = 10
iron = 0
#------------------------------------------------------------------
def distance(x1, y1, x2, y2):
dist = ((x1-x2) ** 2 + (y1-y2) ** 2) ** 0.5
return (dist)
def myround(x, base = 64):
return base * round(x/base)
def randomlandtile():
if random.choice(numlist) > 3:
return random.choice(landtilelist)
else:
return oceantile
def randomoceantile():
if random.choice(numlist) > 1:
return oceantile
else:
return grasstile
def text(font, msg, color, x, y):
screen_text = (font).render(msg, True, color)
screen.blit(screen_text, [x, y])
def start(w, h, x, y, msgx, msgy):
img = pygame.image.load("StartScreen.png")
dark = pygame.image.load("DarkBuildTab.png")
bright = pygame.image.load("BrightBuildTab.png")
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
screen.blit(img, (0, 0))
global starter
if mouse[0] < (x + w) and mouse[0] > x and mouse[1] < (y + h) and mouse[1] > y:
screen.blit(bright, (x, y))
if click[0] == 1:
starter += 1
else:
screen.blit(dark, (x, y))
text(font2, "Play", black, msgx, msgy)
def resource(msg, icon, x, y, msgx, msgy):
img = pygame.image.load(icon)
screen.blit(img, (x, y))
text(font1, msg, white, msgx, msgy)
def resourcetab():
top = pygame.image.load("TopBar.png")
screen.blit(top, (0, 0))
resource(str(food), "FoodIcon.png", 150, 0, 185, 0)
resource(str(gold), "GoldIcon.png", 0, 0, 35, 0)
resource(str(iron), "IronIcon.png", 300, 0, 335, 0)
def build(msg, w, h, x, y, bimg):
global bvar
dark = pygame.image.load("BuildTab1.png")
bright = pygame.image.load("BuildTab2.png")
buildimg = pygame.image.load(bimg)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if mouse[0] < (x + w) and mouse[0] > x and mouse[1] < (y + h) and mouse[1] > y:
screen.blit(bright, (x, y))
if click[0] == 1 and bvar == 1:
town1 = town(green, 64, 64)
town1.rect.x = testunit.rect.x
town1.rect.y = testunit.rect.y
town_list.add(town1)
bvar = 0
print (town_list)
else:
screen.blit(dark, (x, y))
bvar = 1
screen.blit(buildimg, ((x + 200, y)))
text(font2, msg, black, (x + 35), (y + 18))
def buildtab(w, h, x, y, msgx, msgy):
global buildtabnum
dark = pygame.image.load("DarkBuildTab.png")
bright = pygame.image.load("BrightBuildTab.png")
img = pygame.image.load("BuildTab.png")
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if buildtabnum == 1:
screen.blit(img, (x, (y + h)))
if mouse[0] < (x + w) and mouse[0] > x and mouse[1] < (y + h) and mouse[1] > y:
screen.blit(bright, (x, y))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and buildtabnum == 0:
print ("click")
buildtabnum = 1
elif event.type == pygame.MOUSEBUTTONDOWN:
print ("click")
buildtabnum = 0
else:
screen.blit(dark, (x, y))
text(font2, "Build Menu", black, msgx, msgy)
def turnbutton(w, h, x, y, msgx, msgy):
dark = pygame.image.load("DarkTurnButton.png")
bright = pygame.image.load("BrightTurnButton.png")
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if mouse[0] < (x + w) and mouse[0] > x and mouse[1] < (y + h) and mouse[1] > y:
screen.blit(bright, (x, y))
if click[0] == 1:
if testunit.movement <= 0:
global turnnum
turnnum += 1
testunit.movement += 1
testbarb.movement += 1
testbbarb.movement += 1
else:
screen.blit(dark, (x, y))
text(font2, "Next Turn", black, msgx, msgy)
def testformove(self):
pos = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if self.rect.collidepoint(pos) and click[0] == 1:
self.select = 1
#self.imgchange()
def checkforfog(self):
x = self.rect.x
y = self.rect.y
print (distance(Cloud.rect.x, Cloud.rect.y, x, y))
if distance(Cloud.rect.x, Cloud.rect.y, x, y) >= (self.sight * 64):
for Cloud in cloud_list():
cloud_list.remove(Cloud)
if not distance(Cloud.rect.x, Cloud.rect.y, x, y) >= (self.sight * 64):
cloud_list.draw(screen)
def move(self):
mouse = pygame.mouse.get_pos()
global mx
global my
global lol
self.rect.x = myround(mx-32)
self.rect.y = myround(my-32)
dx,dy = pygame.mouse.get_pos()
cx,cy = self.rect.center
click = pygame.mouse.get_pressed()
if mouse[0] < (860 + 230) and mouse[0] > 860 and mouse[1] < (1000 + 60) and mouse[1] > 1000:
pass
elif click[0] == 1 and self.movement >= 1 and self.select == 1:
if distance(cx, cy, dx, dy) <= (self.moverange * 64 + 32) and distance(cx, cy, dx, dy) >= 64:
if Tile.rect.collidepoint(mouse):
print (type(Tile))
mx,my = pygame.mouse.get_pos()
self.movement -= 1
pygame.mixer.Sound.play(walk_sound)
def airandom(self):
num = random.randrange(64, ((self.moverange + 1) * 64), 64)
movelist = [(num) * -1, (num)]
return (random.choice(movelist))
def aimove(self):
if testunit.movement == 0 and self.movement >= 1:
self.rect.y += airandom(self)
self.rect.x += airandom(self)
self.movement -= 1
def main():
pygame.mixer.music.play(-1)
while starter == 0:
print
event = pygame.event.poll()
#print (event)
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(30)
start(300, 80, 800, 550, 850, 568)
pygame.display.flip()
while starter == 1:
all_sprites_list.draw(screen)
town_list.draw(screen)
event = pygame.event.poll()
#print (event)
checkforfog(testunit)
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
turnbutton(230, 60, 860, 1000, 870, 1008)
buildtab(300, 80, 100, 100, 130, 118)
if buildtabnum == 1:
build("Town", 200, 80, 100, 180, "Town1.png")
resourcetab()
pygame.display.flip()
allies = ally_list.sprites()
for pygame.sprite in allies:
testformove(pygame.sprite)
for pygame.sprite in allies:
move(pygame.sprite)
pygame.sprite.update
aimove(testbarb)
aimove(testbbarb)
clock.tick(30)
#---------------------------------------------------------------------
class spearman(pygame.sprite.Sprite):
def __init__(self, color, width, height, m, r):
super().__init__()
self.image = pygame.image.load("Spearman.png").convert_alpha()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
self.movement = m
self.moverange = r
self.select = 0
self.sight = 2
def imgchange(self):
self.image = pygame.image.load("SpearmanGlow").convert_alpha()
class barbarian(pygame.sprite.Sprite):
def __init__(self, color, width, height, m, r):
super().__init__()
self.image = pygame.image.load("Barbarian.png").convert_alpha()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
self.movement = m
self.moverange = r
class bossbarbarian(pygame.sprite.Sprite):
def __init__(self, color, width, height, m, r):
super().__init__()
self.image = pygame.image.load("BossBarbarian.png").convert_alpha()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
self.movement = m
self.moverange = r
class grasstile(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.image.load("Grass.png").convert()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
self.fog = 1
class oceantile(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.image.load("Ocean.png").convert()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
self.fog = 1
class mountaintile(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.image.load("Mountain.png").convert_alpha()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
self.fog = 1
class cloud(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.image.load("Fog.png").convert()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
class town(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.image.load("Town1.png").convert_alpha()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
class mine(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.image.load("Mine.png").convert_alpha()
self.image.set_colorkey(white)
self.rect = self.image.get_rect()
landtilelist = [grasstile, grasstile, grasstile, grasstile, grasstile, grasstile, grasstile, mountaintile]
all_sprites_list = pygame.sprite.Group()
ally_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
town_list = pygame.sprite.Group()
cloud_list = pygame.sprite.Group()
for row in range(0, 17):
for col in range(0, 30):
if row > 4 and row < 12 and col > 10 and col < 20:
Tile = randomlandtile()(red, 64, 64)
else:
Tile = randomoceantile()(red, 64, 64)
Tile.rect.x = col * 64
Tile.rect.y = row * 64
all_sprites_list.add(Tile)
for row in range(0, 17):
for col in range(0, 30):
if row > 4 and row < 12 and col > 10 and col < 20:
Cloud = cloud(red, 64, 64)
else:
Cloud = cloud(red, 64, 64)
Cloud.rect.x = col * 64
Cloud.rect.y = row * 64
cloud_list.add(Cloud)
testunit = spearman(red, 64, 64, 1, 2)
testunit.rect.x = 640
testunit.rect.y = 640
testbarb = barbarian(red, 64, 64, 1, 1)
testbarb.rect.x = 960
testbarb.rect.y = 704
testbbarb = bossbarbarian(red, 64, 64, 1, 2)
testbbarb.rect.x = 960
testbbarb.rect.y = 640
all_sprites_list.add(testunit)
all_sprites_list.add(testbarb)
all_sprites_list.add(testbbarb)
enemy_list.add(testbarb)
enemy_list.add(testbbarb)
ally_list.add(testunit)
all_sprites_list.draw(screen)
event = pygame.event.poll()
print (event)
main()
我希望"checkforfog"
函数能够检查所有Cloud Sprite,但它只会检查最后创建的Cloud Sprite。
答案 0 :(得分:0)
类似这样的东西:
def checkforfog():
for cloud in cloud_list:
print (distance(cloud.rect.x, cloud.rect.y, other_cloud.x, other_cloud.y))
if distance(cloud.rect.x, cloud.rect.y, other_cloud.x, other_cloud.y) >= (sight * 64):
cloud_list.remove(cloud)
elif not distance(cloud.rect.x, cloud.rect.y, other_cloud.x, other_cloud.y) >= (sight * 64):
cloud_list.draw(screen)
请注意,我发布的这段代码只是一个笼统的想法,因为我不相信您发布了所有内容,并且其中一些尚不清楚,因此请以它为指导。但是正如您所看到的,我遍历cloud_list来分别访问每个云。