Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Cell in snake game grid always going to the top of the screen

$
0
0

In my computer science class, I am assigned to code a mod for a basic snake game program. I decided to add a feature where when the user presses the SPACE bar, it will shoot out a portal depending on what mode the user is in (similar to the game Portal). To start with the basics, I decided to start with the shoot method, and wanted it to be animated.

I managed to get the blue/enter portal working, but when I tried to implement the same methods to the orange/exit portal, it just automatically goes to the top of the screen.

I have tried to change the variables from local to global, using extra functions instead of just doing it in one, banging my head against the wall over and over again but nothing is working. What am I doing wrong?

Code

Dictionary for portals

ENTERP = 0EXITP = 1portalPos = [  # The first dict is the enter portal, and the second dict is the exit portal        {"x": random.randint(0, 31), "y": random.randint(0, 23), "dir": ""},        {"x": random.randint(0, 31), "y": random.randint(0, 23), "dir": ""}]

Key Checks

for event in pygame.event.get():  #            if event.type == QUIT:                terminate()            elif event.type == KEYDOWN:                if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:                    direction = LEFT                elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:                    direction = RIGHT                elif (event.key == K_UP or event.key == K_w) and direction != DOWN:                    direction = UP                elif (event.key == K_DOWN or event.key == K_s) and direction != UP:                    direction = DOWN                elif event.key == K_p:                    print(exitPortAniCount)                elif event.key == K_SPACE:                    if portalGunMode == "ent":                        portalPos[ENTERP]["dir"] = direction                        portalPos[ENTERP]['x'] = wormCoords[HEAD]['x']                        portalPos[ENTERP]['y'] = wormCoords[HEAD]['y']                        enterPortAniCount = PRANGE                    if portalGunMode == "ext":                        portalPos[EXITP]["dir"] = direction                        portalPos[EXITP]['x'] = wormCoords[HEAD]['x']                        portalPos[EXITP]['y'] = wormCoords[HEAD]['y']                        exitPortAniCount = PRANGE                elif event.key == K_ESCAPE:                    terminate()

Portal bound checks

def checkPortalBounds():    if portalPos[ENTERP]['x'] >= 31:        portalPos[ENTERP]['x'] = 31    elif portalPos[ENTERP]['x'] <= 0:        portalPos[ENTERP]['x'] = 0    elif portalPos[ENTERP]['y'] >= 23:        portalPos[ENTERP]['y'] = 23    elif portalPos[ENTERP]['y'] <= 0:        portalPos[ENTERP]['y'] = 0    if portalPos[EXITP]['x'] >= 31:        portalPos[EXITP]['x'] = 31    elif portalPos[EXITP]['x'] <= 0:        portalPos[EXITP]['x'] = 0    elif portalPos[EXITP]['y'] >= 23:        portalPos[EXITP]['y'] = 23    elif portalPos[EXITP]['y'] >= 0:        portalPos[EXITP]['y'] = 0

Update portal animations

def updatePortalAnims():    global enterPortAniCount, exitPortAniCount    for i in range(PRANGE):        if not enterPortAniCount <= 0 and not portalPos[ENTERP]['x'] == 31 and not \                portalPos[ENTERP]['x'] == 0 and not portalPos[ENTERP]['y'] == 23 and not \                portalPos[ENTERP]['y'] == 0:            if portalPos[ENTERP]['dir'] == RIGHT:                portalPos[ENTERP]['x'] += SHOOTSPEED                enterPortAniCount -= 1                checkPortalBounds()            elif portalPos[ENTERP]['dir'] == LEFT:                portalPos[ENTERP]['x'] -= SHOOTSPEED                enterPortAniCount -= 1                checkPortalBounds()            elif portalPos[ENTERP]['dir'] == UP:                portalPos[ENTERP]['y'] -= SHOOTSPEED                enterPortAniCount -= 1                checkPortalBounds()            elif portalPos[ENTERP]['dir'] == DOWN:                portalPos[ENTERP]['y'] += SHOOTSPEED                enterPortAniCount -= 1                checkPortalBounds()        if not exitPortAniCount <= 0 and not portalPos[EXITP]['x'] == 31 and not \                portalPos[EXITP]['x'] == 0 and not portalPos[EXITP]['y'] == 23 and not \                portalPos[EXITP]['y'] == 0:            if portalPos[EXITP]['dir'] == RIGHT:                portalPos[EXITP]['x'] += 1                exitPortAniCount -= 1                checkPortalBounds()            elif portalPos[EXITP]['dir'] == LEFT:                portalPos[EXITP]['x'] -= 1                exitPortAniCount -= 1                checkPortalBounds()            elif portalPos[EXITP]['dir'] == UP:                portalPos[EXITP]['y'] -= 1                exitPortAniCount -= 1                checkPortalBounds()            elif portalPos[EXITP]['dir'] == DOWN:                portalPos[EXITP]['y'] += 1                exitPortAniCount -= 1                checkPortalBounds()        else:            break

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>