如何解决从左到右产卵
鱼应该从左到右产卵。 而且产卵似乎有些奇怪。 即使我设置了最小和最大产卵时间,一些鱼也会在 20 秒后出现。 另外,我对 add_fish() 函数有疑问,我不确定这是生成鱼的正确方法:6 种鱼,所有鱼都有相同的生成机会。 我做错了什么?
class SpawnTime():
def __init__(self,previous,time):
self.time_previous = previous
self.time = time
class Fish():
count = 0
def __init__(self,txr,value,speed):
self.txr = txr
self.value = value
self.speed = speed
self.x = SCREEN_WIDTH
self.y = random.randint(0,SCREEN_HEIGHT - self.txr.get_height())
self.rect = pygame.Rect(
self.x,self.y,self.txr.get_width(),self.txr.get_height())
self.is_active = True
Fish.count += 1
def draw(self):
SCREEN.blit(self.txr,self.rect)
def update(self):
self.rect.x += self.speed
if self.rect.right <= 0:
self.is_active = False
class Player():
def __init__(self):
self.txr = PLAYER_TXR
self.x = 0
self.y = 0
self.width = self.txr.get_width()
self.height = self.txr.get_height()
self.rect = pygame.Rect(self.x,self.width,self.height)
self.score = 0
self.reset()
def draw(self):
SCREEN.blit(self.txr,self.rect)
def update(self):
self.x,self.y = pygame.mouse.get_pos()
self.rect.x = self.x
self.rect.y = self.y
def reset(self):
self.x = self.width
self.y = SCREEN_HEIGHT//2
self.rect = pygame.Rect(self.x,self.height)
self.score = 0
def clicked(self):
for f in fish_list:
if f.rect.collidepoint(self.x,self.y):
self.score += f.value
f.is_active = False
SOUND_SLIP.play()
break # exit the for loop
def update():
if playing:
clock.tick(FPS)
if playing and not game_over:
game_time = pygame.time.get_ticks()
if Fish.count < MAX_FISHES:
if game_time - fish_spawn.time_previous >= fish_spawn.time:
fish_spawn.time_previous = game_time
fish_spawn.time = random.randint(
MIN_FISH_SPAWN_TIME,MAX_FISH_SPAWN_TIME)
add_fish()
player.update()
for fish in fish_list:
fish.update()
for fish in fish_list:
if not fish.is_active:
fish_list.remove(fish)
def draw():
SCREEN.blit(TITLE_SCREEN,(0,0))
if not playing and game_over:
if new_high_score:
draw_text_centred_x(
f'New High Score: {high_score}',FONT_20,250,TEAL)
else:
draw_text_centred_x(
f'High Score: {high_score}',50,TEAL)
draw_text_centred_x(
f'SCORE: {player.score}',FONT_30,170,TEAL)
draw_text_centred_x('Press SPACE to Play',350,DARK_GREEN)
if not playing and not game_over:
draw_text_centred_x('Press SPACE to Play',FONT_40,DARK_GREEN)
if playing and not game_over:
draw_text(f'Score: {player.score} High Score: {high_score}',5,DARK_GREEN)
for fish in fish_list:
fish.draw()
player.draw()
def draw_text_centred_x(text,font,y,colour):
img = font.render(text,True,colour)
x = SCREEN_WIDTH//2 - img.get_width()//2
SCREEN.blit(img,(x,y))
def draw_text(text,x,colour)
SCREEN.blit(img,y))
def reset_game():
fish_list.clear()
player.reset()
def add_fish():
if random.randint(1,6) == 1:
rand = random.randint(0,len(FISHES_TXR)-1)
txr = FISHES_TXR[rand]
value = rand+1
speed = rand*5
if txr == FISHES_TXR[5]:
value = -3
f = Fish(txr,speed)
fish_list.append(f)
if __name__ == "__main__":
playing = False
game_over = False
high_score = 0
new_high_score = False
player = Player()
fish = Fish
fish_list = []
fish_spawn = SpawnTime(0,MAX_FISH_SPAWN_TIME)
# main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit()
if event.key == pygame.K_SPACE and not game_over:
fish_spawn.time = MAX_FISH_SPAWN_TIME
playing = True
if event.key == pygame.K_SPACE and not playing:
# new game
playing = True
game_over = False
new_high_score = False
player.score = 0
fish_spawn.time = 700
Fish.count = 0
pygame.time.delay(500) # pause for 500ms = 0.5s
elif event.type == pygame.MOUSEBUTTONDOWN:
player.clicked()
if playing:
update()
if Fish.count >= MAX_FISHES and len(fish_list) == 0:
game_over = True
playing = False
if player.score > high_score:
new_high_score = True
high_score = player.score
draw()
pygame.time.delay(500) # pause for 500ms = 0.5s
draw()
pygame.display.update()
clock.tick(FPS)
pygame.quit()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。