我需要有关如何让我的塔防游戏有多个实例的帮助。我希望新实例在最后一个实例后大约一秒钟出现,但已经卡住了几个小时。我的代码如下。
import pygame
import os
import time
img_path = os.path.join('test.jpg')
class redEnemy(object):
def __init__(self):
self.image = pygame.image.load(img_path)
self.x = 0
self.y = 208
def moveEnemy(self):
dist = 3
if True:
self.x += dist
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
lifes(self)
def lifeText(life):
font = pygame.font.SysFont("monospace", 20)
text = font.render("Lifes %s" % life, True, black)
textpos = text.get_rect()
textpos.center = (50,20)
screen.blit(text, textpos)
def lifes(self):
global life
life = 50
if self.x > 950:
life = life - 1
enemies.append(redEnemy())
lifeText(life)
pygame.init()
screen = pygame.display.set_mode((960, 720))
black = (0,0,0)
enemies = []
redenemy = redEnemy()
clock = pygame.time.Clock()
mapImg = pygame.image.load("mapimage.jpg")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
for enemy in enemies:
enemy.update()
for enemy in enemies:
enemy.draw(screen)
redenemy.moveEnemy()
screen.blit(mapImg, (0,0))
redenemy.draw(screen)
pygame.display.update()
clock.tick(60)
答案 0 :(得分:0)
我不知道你在lifes()
尝试做什么,但其余代码可能如下所示:
import pygame
import random
# --- constants --- (UPPER_CASE names)
WIDTH = 960
HEIGHT = 720
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
FPS = 60
# --- classes --- (CamelCase names)
class RedEnemy(object):
# one image for all instances
image = pygame.image.load('test.jpg')
def __init__(self, x, y, screen):
self.screen = screen
self.screen_rect = screen.get_rect()
# every instance can have own image
#self.image = pygame.image.load('test.jpg')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.dist_x = 3
self.dist_y = 0
def update(self):
# move to right side and start at left side
if self.rect.right < self.screen_rect.right:
self.rect.x += self.dist_x
self.rect.y += self.dist_y
else:
self.rect.left = 0
def draw(self, screen):
screen.blit(self.image, self.rect)
# --- functions --- (lower_case names)
def life_text(screen, life, font):
text = font.render("Lifes %s" % life, True, BLACK)
text_rect = text.get_rect()
text_rect.center = (50,20)
screen.blit(text, text_rect)
# --- main --- (lower_case names)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
screen_rect = screen.get_rect()
# - objects -
# background
map_img = pygame.image.load("mapimage.jpg")
# enemies - 10 enemies in random places
enemies = []
for i in range(10):
x = random.randint(0, WIDTH)
y = random.randint(0, HEIGHT)
enemies.append(RedEnemy(x, y, screen))
# player
lifes = 50
# player = Player() # you need class Player()
# - other -
font = pygame.font.SysFont("monospace", 20)
clock = pygame.time.Clock()
# - mainloop -
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# - updates -
for enemy in enemies:
enemy.update()
if enemy.rect.right >= screen_rect.right:
lifes -= 1
# - draws -
# background
screen.blit(map_img, (0,0))
# enemies
for enemy in enemies:
enemy.draw(screen)
# other
life_text(screen, lifes, font)
# send buffer to monitor
pygame.display.update()
# - FPS -
clock.tick(FPS)
# - end -
pygame.quit()
答案 1 :(得分:0)
我必须说,在改进此代码方面肯定有工作要做,但我们当然必须从某个地方开始。 (我当然是一个留在学习过程中的人)。
幸运的是,通过一些随机图像,我能够运行您的代码,我相信我理解您的目标。首先,我认为需要解决的是你应该添加
found pattern in OurLatitude-eliemxgbqrgibwbimtxzdyhuagll/Build/Intermediates/OurLatitude.build/Debug-iphonesimulator/OurLatitude.build/dgph
line: 68 -framework DateTools
在你的循环开始。
但至于你想要的问题,我相信你只是忘了在你的敌人名单中放置“redEnemy”。通过删除事件处理器循环中的奇数函数,而不是在循环的敌方迭代器上应用“redEnemy”的方法,您可以获得所需的结果。此外,在你的生活功能中,我看到它适合移除前一艘船,因为有一连串的敌人只是从屏幕的一侧倾泻而出。同样,这里(希望)是你想要的代码:
screen.fill((0,0,0))
简单地运行它,并希望解决您的预期问题。