我想做一个游戏,一只乌龟跟着你的老鼠,你必须在时间用完之前收集一定数量的鱼,但是鱼过了一会儿就消失了。你知道吗
我的问题是我不知道怎么让鱼消失。我想用python地图。唯一的问题是,地图只能有两个相互对应的项目。我想记录三个项目,鱼计数(已被闪电鱼的数量),以及每个项目的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,但我不能说我的职位(我称职位为“鱼位置列表”),因为我有这个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)
我把所有的字典(地图)都命名为positions,所以当我说positions时会感到困惑,因为它们都被称为positions。再次感谢你对我的帮助!你知道吗
虽然地图(或字典)每个可以有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
相关问题 更多 >
编程相关推荐