Pygame指数超出范围

时间:2015-09-10 02:21:32

标签: python pygame

我正在使用python构建类似小行星的游戏,但是我遇到了问题,因为我保持在possibleTurrets数组的范围内。我正在做的就是遍历数组,当数组外的边缘情况出现时,我通过以下方式处理它们:

if currentTurPos > 8:
    currentTurPos = 8
elif currentTurPos < 0:
    currenTurPos = 0

这是对这个数组的引用:

possibleTurrets = [ (x-27,y-2),
                    (x-26,y-5),
                    (x-25,y-8),
                    (x-23,y-12),
                    (x-20,y-14),
                    (x-18,y-15),
                    (x-15,y-17),
                    (x-13,y-19),
                    (x-11,y-21)
    ]

出于某种原因,此代码在pygame.K_UP上正常工作,但在pygame.K_DOWN上却无效,我不明白为什么。

这似乎应该是非常明显的,但我花了一个小时试图修复它,并且画了一个空白。问题在于我的ufo函数和currentTurPos变量。

# Libraries
import pygame
import time
import random
import os

# Initialize Pygame
pygame.init()

# Colors
white = (255,255,255)
black = (0,0,0)
red = (200,0,0)
blue = (0,0,255)
green = (34,177,76)
light_green = (0,255,0)
yellow = (200,200,0)
light_yellow = (255,255,0)
light_red = (255,0,0)

# Display
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
#pygame.display.set_caption('Slither')
#icon = pygame.image.load('apple.bmp')
#pygame.display.set_icon(icon)
clock = pygame.time.Clock()

# Sprites
ufoWidth = 40
ufoHeight = 20
turretWidth = 5
turretHeight = 10

fps = 15 # Speed

# Fonts
smallFont = pygame.font.SysFont("comicsansms", 25)
medFont = pygame.font.SysFont("comicsansms", 50)
largeFont = pygame.font.SysFont("comicsansms", 80)

def ufo(x,y,turPos):
    x = int(x)
    y = int(y)

    possibleTurrets = [ (x-27,y-2),
                        (x-26,y-5),
                        (x-25,y-8),
                        (x-23,y-12),
                        (x-20,y-14),
                        (x-18,y-15),
                        (x-15,y-17),
                        (x-13,y-19),
                        (x-11,y-21)
        ]

    pygame.draw.circle(gameDisplay, blue, (x,y),int(ufoHeight/2))
    pygame.draw.ellipse(gameDisplay, black, (x-ufoHeight, y, ufoWidth, ufoHeight))
    pygame.draw.line(gameDisplay,blue,(x,y),possibleTurrets[turPos],turretWidth)    

# Font sizes        
def text_objects(text, color, size):
    if size == "small":
        textSurface = smallFont.render(text, True, color)
    elif size == "medium":
        textSurface = medFont.render(text, True, color)
    elif size == "large":
        textSurface = largeFont.render(text, True, color)
    return textSurface, textSurface.get_rect()

# Displays a message: Requires message, color, y-displacement, and size arguments
def message_to_screen(msg, color, y_displace=0, size="small"):
    textSurf, textRect = text_objects(msg, color, size)
    textRect.center = (int(display_width/2), (int(display_height/2) + y_displace))
    gameDisplay.blit(textSurf, textRect)

# Show button text
def text_to_button(msg,color, buttonx,buttony,buttonwidth,buttonheight,size="small"):
    textSurf, textRect = text_objects(msg,color,size)
    textRect.center = ((buttonx+(buttonwidth/2)), buttony+(buttonheight/2))
    gameDisplay.blit(textSurf, textRect) 

# On button hover
def button(text,x,y,width,height,inactive_color,active_color,action):
    cursor = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + width > cursor[0] > x and y + height > cursor[1] > y:
        pygame.draw.rect(gameDisplay,active_color,(x,y,width,height))
        if click[0] == 1 and action != None:
            if action == "play":
                gameLoop()
            if action == "controls":
                game_controls()
            if action == "quit":
                pygame.quit()
                quit()
    else:
        pygame.draw.rect(gameDisplay,inactive_color,(x,y,width,height))

    text_to_button(text,black,x,y,width,height)

def game_controls():
    gcont = True
    while gcont:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_c:
                    intro = False
                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()
        gameDisplay.fill(white)
        message_to_screen("Controls", green, -100, size="large")
        message_to_screen("Fire: Spacebar", black, -30, size="small")
        message_to_screen("Move turret: Up and Down arrows", black, 10, size="small")
        message_to_screen("Move UFO: Left and Right arrows", black, 50, size="small")
        message_to_screen("Pause: P", black, 90, size="small")

        button("Play",150,450,100,50, green, light_green, action="play")
        button("Quit",550,450,100,50, red, light_red, action="quit")

        pygame.display.update()
        clock.tick(fps)  

# Pauses game and gives continue and quit options
def pause():
    paused = True
    message_to_screen("Paused", black, -100, size="large")
    message_to_screen("Press P to play, or Q to quit.", black, 25, size="small")
    while paused:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    paused = False
                elif event.key == pygame.K_q:
                    pygame.quit()
                    quit()
        pygame.display.update()
        clock.tick(fps)

# Keeps track of, and displays the score
def score(score):
    text = smallFont.render(" Score: " + str(score), True, black)
    gameDisplay.blit(text, [0,0])

# Barrier
def barrier():
    xlocation = (display_width/2) + random.randint(-0.2*displayWidth,0.2*displayWidth)


# Intro screen - Title and directions
def game_intro():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_c:
                    intro = False
                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()
        gameDisplay.fill(white)
        message_to_screen("Welcome to UFO!", green, -100, size="large")
        message_to_screen("The objective of the game is to search and destroy.", black, -30, size="small")
        message_to_screen("Destroy your opponent before they destroy you.", black, 10, size="small")
        message_to_screen("The more enemies you destroy, the harder they get.", black, 50, size="small")

        button("Play",150,450,100,50, green, light_green, action="play")
        button("Controls",350,450,100,50, yellow, light_yellow, action="controls")
        button("Quit",550,450,100,50, red, light_red, action="quit")

        pygame.display.update()
        clock.tick(fps)   

# Main game loop
def gameLoop():
    gameExit = False
    gameOver = False

    mainUfoX = display_width * 0.9
    mainUfoY = display_height * 0.9
    ufoMove = 0
    currentTurPos = 0
    changeTur = 0

    # Game over screen and changes for key inputs
    while not gameExit:
        if gameOver == True:
            message_to_screen("Game over!", red, -50, size="large")
            message_to_screen("Press C to play again, Q to quit.", black, 50, size="medium")
            pygame.display.update()
        while gameOver == True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameExit = True
                    gameOver = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameLoop()

        # Make changes for specific key inputs
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    ufoMove = -5
                elif event.key == pygame.K_RIGHT:
                    ufoMove = 5
                elif event.key == pygame.K_UP:
                    changeTur = 1
                elif event.key == pygame.K_DOWN:
                    changeTur = -1
                elif event.key == pygame.K_p:
                    pause()
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    ufoMove = 0
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    changeTur = 0

        gameDisplay.fill(white)
        mainUfoX += ufoMove
        currentTurPos += changeTur

        if currentTurPos > 8:
            currentTurPos = 8
        elif currentTurPos < 0:
            currenTurPos = 0

        ufo(mainUfoX,mainUfoY,currentTurPos)
        pygame.display.update()
        clock.tick(fps)

    # Quits
    pygame.quit()
    quit()

# Loads game intro screen
game_intro()

# gameLoop calls itself to begin game
gameLoop()

1 个答案:

答案 0 :(得分:0)

这是一个错字(我是个白痴)。

currentTurPos是currenTurPos。

获得的经验教训:带有重复字母的复杂变量名称很危险。