【问题标题】:Python Maps with 3 items包含 3 个项目的 Python 地图
【发布时间】:2025-12-13 22:10:01
【问题描述】:

我正在尝试制作一个游戏,其中一只乌龟跟随你的鼠标,你必须在时间用完之前收集一定数量的鱼,但鱼会在一段时间后消失。

我的问题是我不知道如何让鱼消失。我想到了使用python地图。唯一的问题是,地图只能有 2 个相互对应的项目。我想跟踪三个项目,fish_count(已被 blitted 的鱼的数量)以及每个项目的 x 和 y 位置。以后想参考一下地图,看看什么时候让鱼消失,在乌龟撞到鱼的时候删除。

我的问题是:有没有更简单的方法来存储信息,以便我可以更轻松、更有效地跟踪它

我不是要代码转储!

这是我已经拥有的代码:

import math
import random
import sys
import time
from pygame import *
import pygame
pygame.init()

 #------Easy-Change Variables
mouse_visibility = False
turtlespeed = 4

 #------Fonts
font = pygame.font.SysFont("C:/Python34/TurtleFont.ttf", 48)

 #------Timer Setup
start_time = time.time()
elapsed_time = time.time() - start_time

 #------Theme Music
pygame.mixer.music.load('C:/Python34/Jumpshot.mp3')
pygame.mixer.music.play(1, 0.0)

 #------BG, Screen & Caption
pygame.display.set_caption('Fishy the Turtle')
screen = pygame.display.set_mode((1024, 616))
bg_img = pygame.image.load("C:/Python34/icebackground.png")
screen.blit(bg_img,(0,0))
pygame.display.flip()

 #------Image Preparation
turtle = pygame.image.load("C:/Python34/turtle.png")
fish = pygame.image.load("C:/Python34/fish.png")

 #------Movement, Coords, Collisions & Fish
turtlepos = pygame.mouse.get_pos()
pygame.mouse.set_visible(mouse_visibility)

fish_count = 0

if pygame.mouse.get_pressed()[1] == True:
    while 1 == 1:
        mousepos = pygame.mouse.get_pos()

        text = font.render("Points: %s" % points, True, (0, 128, 0))
        screen.blit(text, (0, 0))

        new_fish = random.randint(0, 6)
        fish_x = random.randint(0, 943)
        fish_y = random.randint(0, 552)

        if mousepos[1] - turtlepos[1] < 0:  
            turtlepos[1] -= turtlespeed
        else:
            turtlepos[1] += turtlespeed

        if mousepos[2] - turtlepos[2] < 0:
            turtlepos[2] -= turtlespeed
        else:
            turtlepos[2] += turtlespeed

        if elapsed_time == 45:
            sys.exit()
            print("Sorry! Game Over!")

        screen.blit(turtle,(turtlepos[1],turtlepos[2]))
        pygame.display.flip()

        if new_fish == 0:
            screen.blit(fish,(fish_x, fish_y)
            pygame.display.flip()
            fish_count += 1


        pygame.display.update()
        mainClock.tick(40)

编辑:到目前为止的完整项目:

import math
import random
import sys
import time
from pygame import *
import pygame
pygame.init()

 #------Easy-Change Variables
mouse_visibility = False
turtlespeed = 4

 #------Font
font = pygame.font.SysFont("C:/Python34/TurtleFont.ttf", 48)

 #------Theme Music & Sound Effects
pygame.mixer.music.load('C:/Python34/Jumpshot.mp3')
pygame.mixer.music.play(1, 0.0)

chomp = pygame.mixer.Sound("C:/Python34/chomp.wav")
 #------BG, Screen & Caption
pygame.display.set_caption('Fishy the Turtle')
screen = pygame.display.set_mode((1024, 616))
bg_img = pygame.image.load("C:/Python34/icebackground.png")
screen.blit(bg_img,(0,0))
pygame.display.flip()

 #------Image Preparation
turtle = pygame.image.load("C:/Python34/turtle.png")
fish = pygame.image.load("C:/Python34/fish.png")

 #------Movement, Coords, Collisions & Fish
turtlepos = pygame.mouse.get_pos()
pygame.mouse.set_visible(mouse_visibility)

turtle_rect = turtle.get_rect()
fish_rect = fish.get_rect()

points = 0
fish_pos_list = []

if pygame.mouse.get_pressed()[1] == True:
    while 1 == 1:
        mousepos = pygame.mouse.get_pos()

        text = font.render("Points: %s" % points, True, (0, 128, 0))
        screen.blit(text, (0, 0))

        new_fish = random.randint(0, 6)
        fish_x = random.randint(0, 943)
        fish_y = random.randint(0, 552)

        if mousepos[1] - turtlepos[1] < 0:  
            turtlepos[1] -= turtlespeed
        else:
            turtlepos[1] += turtlespeed

        if mousepos[2] - turtlepos[2] < 0:
            turtlepos[2] -= turtlespeed
        else:
            turtlepos[2] += turtlespeed

        screen.blit(turtle_rect,(turtlepos[1],turtlepos[2]))
        pygame.display.flip()

        if new_fish == 0:
            screen.blit(fish_rect,(fish_x, fish_y)
            pygame.display.flip()
            fish_count += 1
            start_time = time.time()
            positions = {'x':fish_x, 'y':fish_y, 'fishtimer':start_time}
            fish_pos_list.append(positions)

        if time.time() - fish_pos_list[FISHTIMER OF FIRST ITEM IN LIST] >= 10:
            screen.blit(bg_img, (X OF FIRST ITEM, Y OF FIRST ITEM), pygame.Rect(X OF FIRST ITEM, Y OF FIRST ITEM, 81, 62))
            del fish_pos_list[0]

        if turtle_rect.colliderect(fish_rect):
            screen.blit(bg_img, (X OF FIRST ITEM, Y OF FIRST ITEM), pygame.Rect(X OF FIRST ITEM, Y OF FIRST ITEM, 81, 62))
            chomp.play()
            DELETE FISH FROM LIST
            points += 1

        pygame.display.update()
        mainClock.tick(40)

感谢你帮我解决这个问题,hd1,但我不能说职位(我称职位“fish_pos_list”),因为我有这个 if 语句:

if new_fish == 0:
            screen.blit(fish_rect,(fish_x, fish_y)
            pygame.display.flip()
            fish_count += 1
            start_time = time.time()
            positions = {'x':fish_x, 'y':fish_y, 'fishtimer':start_time}
            fish_pos_list.append(positions)

我有所有的字典(地图)命名的位置,所以当我说位置时会混淆,因为它们被称为位置。再次感谢 TON 对我的帮助!

【问题讨论】:

    标签: python python-3.x python-2.7 pygame


    【解决方案1】:

    虽然每个地图(或字典)可以有 2 个项目,但没有规定项目本身必须是标量。请参阅以下示例:

    position = {'x':1, 'y':3, 'fishcount':5}
    positions = []
    positions.append(position)
    

    解决您的评论:

    for position in positions:
        if position['fishcount'] > 9:
            # do stuff
        else:
            # do other stuff
    

    希望对您有所帮助。如果您还有其他问题,请随时发表评论。

    【讨论】:

    • 如果我在一个列表中有一堆字典,我想查看第一个字典的fishcount,我该怎么写?虽然只是列表的第一部字典,因为我想要一个 if 语句说“如果第一部字典的时间(fishcount)大于或等于十秒,则摆脱鱼并将其从列表中删除”我只是不知道怎么说第一部分。
    • 请看我的编辑——另外,你应该投票赞成接受的答案
    • 我没有足够的代表。我会真的喜欢你的帖子,我之前尝试过,但我需要 15 个代表。
    • 您可能希望看到原始帖子的底部。谢谢!
    • 糟糕,忘记标记你了。 @hd1
    【解决方案2】:

    你能用named tuple吗?

    from collections import namedtuple

    Fish = namedtuple('Fish', 'count, x, y')
    a_fish = Fish(fish_count, fish_x, fish_y)

    然后您可以通过索引访问:a_fish[0] 或按名称访问:a_fish.fish_count

    【讨论】: