Python Forum
New to Python, having some trouble
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to Python, having some trouble
#1
Hi Everyone,

I'm new to Python and I'm trying to write a small game using Pygame called Necromancers Vs Werewolves. Its not a game in the normal sense, I want to see if using the rules I laid down if it will demonstrate any unique patterns. Anyway, N represents a necromancer, and W a werewolf. My Ns are supposed to teleport across the screen but what it is doing is teleporting once or twice and then all the Ns vanish. Please look at my source code below and help me out. Wink

import pygame
import time
import random

pygame.init()
screen = pygame.display.set_mode((1024, 768))
done = False
is_blue = True
x = 30
y = 30
pause = True
random.seed()
clock = pygame.time.Clock()


#lists
necromancers = [0 for x in range(100)]
werewolves = [0 for x in range(100)]
corpses = [0 for x in range(100)]
fireballs = [0 for x in range(100)]
necromancers = [0 for x in range(100)]

w, h = 100, 100;
necro_pos = [[0 for x in range(w)] for y in range(h)]
werewolve_pos = [[0 for x in range(w)] for y in range(h)]
fireball_pos = [[0 for x in range(w)] for y in range(h)]
bones_pos = [[0 for x in range(w)] for y in range(h)]
stench_pos = [[0 for x in range(w)] for y in range(h)]


#initialize positions
#initalize necromancers
for index in range(10):
    #Winitialize 10 necromancers
    necromancers[index] = index
    x = random.randint(1,99)
    y = random.randint(1,99)
    necro_pos[x][y] = index
#initialize werewolves
for index2 in range(10):
    #Winitialize 10 werewolves
    werewolves[index2] = index2
    x = random.randint(1,99)
    y = random.randint(1,99)
    werewolve_pos[x][y] = index2







while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_q]:
            done = True




    time.sleep(1)

    screen.fill((0,0,0))

    myfont = pygame.font.Font(pygame.font.get_default_font(), 24)

    #draw werewolves

    for idx in range(99):
        for idy in range(99):
            if werewolve_pos[idx][idy] is not 0:
                surface = pygame.font.Font.render(myfont, "W", False, (255, 255, 0), (0, 0, 0))
                screen.blit(surface, (10*idx, 10*idy))
    #draw necromancers

    for idx in range(99):
        for idy in range(99):
            if necro_pos[idx][idy] is not 0:
                surface = pygame.font.Font.render(myfont, "N", False, (255, 0, 0), (0, 0, 0))
                screen.blit(surface, (10 * idx, 10 * idy))

    # randomly change the position of the necromancers

    for ind in range(10):

        for idx in range(99):
            for idy in range(99):
                if necro_pos[idx][idy] is not 0:
                    movement_random_x = random.randint(1, 99)
                    movement_random_y = random.randint(1, 99)
                    print("Movement Random X: " + str(movement_random_y) + " Movement Random Y:"  + str(movement_random_y))
                    print("IND is : " + str(ind))
                    necro_pos[idx][idy] = 0
                    necro_pos[movement_random_x][movement_random_y] = ind

    screen.blit(surface, (99,99))

    pygame.display.flip()
    clock.tick(60)
Reply
#2
(Apr-25-2018, 03:36 PM)ted_gress Wrote: if necro_pos[idx][idy] is not 0:
This might not be related to your error, but you probably shouldn't be using the is operator for that. It should only be used for None, or for custom classes sometimes. Instead, you should just check for equality, ie: if necro_pos[idx][idy] != 0:.

It might happen to work the way you expect, on some versions of python, on some architectures (I think cPython caches small ints), but that shouldn't be relied on.
Reply
#3
I changed it to !=0 and there is no change in behavior. The Ns for the necromancers still vanish.
Reply
#4
(Apr-25-2018, 03:36 PM)ted_gress Wrote:
    # randomly change the position of the necromancers
 
    for ind in range(10):
 
        for idx in range(99):
            for idy in range(99):
                if necro_pos[idx][idy] is not 0:
                    movement_random_x = random.randint(1, 99)
                    movement_random_y = random.randint(1, 99)
                    print("Movement Random X: " + str(movement_random_y) + " Movement Random Y:"  + str(movement_random_y))
                    print("IND is : " + str(ind))
                    necro_pos[idx][idy] = 0
                    necro_pos[movement_random_x][movement_random_y] = ind

I'll snip a few parts of that out...
    # randomly change the position of the necromancers 
    for ind in range(10):
        for idx in range(99):
            for idy in range(99):
                    necro_pos[idx][idy] = 0
                    necro_pos[movement_random_x][movement_random_y] = ind
for ind in range(10): is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. That means you're iterating through all the necromancer positions, and if there's a necromancer there, you pick a new position, and then set both the old position and the new position to 0. So they "disappear", because you set all of the necromancer indices to 0, and then ignore 0 everywhere.
Reply
#5
I don't understand. And I tried changing my code to yours and it just floods the screen with N's.
Reply
#6
Why are you using two different lists for necromancers? What is each list doing?

The necromancers are vanishing, because when you try to randomize their position, you set all of the id's to 0, which you then treat as a null-necromancer. There's many ways to fix it (the easiest is probably just doing for ind in range(1, 10):), but I have a feeling you'll have similar issues in the future if you continue to use these id things to keep track of your characters.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020