gpt4 book ai didi

包含 3 个项目的 Python map

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:35 25 4
gpt4 key购买 nike

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

我的问题是我不知道如何让鱼消失。我想到使用 python map 。唯一的问题是, map 只能有 2 个相互对应的项目。我想跟踪三个项目:fish_count(已被 block 传输的鱼的数量)以及每个项目的 x 和 y 位置。我想稍后引用 map 看看什么时候让鱼消失,并在乌龟与鱼碰撞时删除它们。

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

我不是在要求代码转储!

这是我已经拥有的代码:

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)

我有所有名为位置的字典( map ),因此当我说位置时会感到困惑,因为它们全部都称为位置。再次感谢您对我的帮助!

最佳答案

虽然 map (或字典)每个可以有 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

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

关于包含 3 个项目的 Python map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30135149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com