Nate.Olson
Nate.Olson

Reputation: 153

Python Maps with 3 items

I am trying to make a game where a turtle follows your mouse, and you have to collect a certain amount of fish before time runs out, but the fish disappear after a while.

My problem is that I can't figure out how to make the fish disappear. I thought of using a python map. The only problem is, maps can only have 2 items that correspond to each other. I want to keep track of three items, fish_count (the number of fish that have been blitted), and the x and y positions of each. I want to reference the map later to see when to make the fish disappear, and to delete them when the turtle collides with the fish.

My question is: Is there an easier way to store the information so that I can keep track of it easier and more efficiently

I AM NOT ASKING FOR A CODE DUMP!

Here is my code that I have already:

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)

EDIT: Full project so far:

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)

Thanks for helping me with this, hd1, but I cant say for postion in positions (I am calling positions "fish_pos_list") because i have this if statement:

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)

I have all of the dictionaries (maps) named positions, so it will be confused when i say positions, because they are all called positions. Again, thank you a TON for helping me!

Upvotes: 1

Views: 144

Answers (2)

hd1
hd1

Reputation: 34677

While maps (or dictionaries) can have 2 items apiece, there's no rule saying the items themselves have to be scalar. See the following example:

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

To address your comment:

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

Hope that helps. If you have further problems, feel free to post a comment.

Upvotes: 1

Christopher Pearson
Christopher Pearson

Reputation: 1193

can you use a named tuple?

from collections import namedtuple

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

then you can access by index: a_fish[0] or access by name: a_fish.fish_count

Upvotes: 0

Related Questions