Python 包含 3 个项目的地图

Python Maps with 3 items

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

我的问题是我不知道如何让鱼消失。我想到了使用 python 地图。唯一的问题是,地图只能有 2 个相互对应的项目。我想跟踪三个项目,fish_count(被 blit 的鱼的数量),以及每个项目的 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)

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

虽然地图(或词典)每个可以有 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

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

你可以使用 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