只有4个对象掉落pygame

时间:2017-12-15 15:02:15

标签: python arrays pygame

当我运行我的代码时,礼物会很好但一次只会掉落4块煤。我该怎么做才能让更多的煤炭掉下来?煤炭和礼物都以同样的方式设置,所以我很困惑为什么只有4块煤落下。煤炭和礼物都不应该以同样的方式下降吗?

设置

import pygame, sys, time, random
from pygame.locals import *

# Set up pygame.
pygame.init()
mainClock = pygame.time.Clock()

# Set up the window.
WINDOWWIDTH = 800
WINDOWHEIGHT = 700
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Present catch')
myfont = pygame.font.SysFont("monospace", 40)

# Set up the colors.
WHITE = (255, 255, 255)


# Set up the player.
player = pygame.Rect(400, 600, 100, 100)
playerImage = pygame.image.load('player.png')
playerStretchedImage = pygame.transform.scale(playerImage, (100, 100))

# Set up the present
present = pygame.Rect(300, -20, 40, 40)
presentImage = pygame.image.load('present.png')
presentStretchedImage = pygame.transform.scale(presentImage, (40, 40))

# Set up the coal
coal = pygame.Rect(300, -20, 40, 40)
coalImage = pygame.image.load('coal.png')
coalStrechedImage = pygame.transform.scale(coalImage, (40, 40))

maxPresent = 100
highScore =  maxPresent

礼物数组

presents = []
for i in range(maxPresent):
    x = random.randrange(0, 800)
    y = random.randrange(-700, 100)
    presents.append(pygame.Rect(x, y, 20, 20))

煤炭阵列

coals = []
for z in range(120):
    x = random.randrange(0, 800)
    y = random.randrange(-300, -40)
    coals.append(pygame.Rect(x, y, 20, 20))

游戏的其​​余部分

score = 0
finalScore = 0

# Set up keyboard variables.
moveLeft = False
moveRight = False
moveUp = False
moveDown = False

MOVESPEED = 6


# Set up the music.
pickUpSound = pygame.mixer.Sound('pickup.wav')
pickCoal = pygame.mixer.Sound('drop.wav')
pygame.mixer.music.load('music.mid')
pygame.mixer.music.set_volume(.3)
pygame.mixer.music.play(-1, 0.0)
musicPlaying = True

# Run the game loop.
while True:
    # Check for the QUIT event.
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            # Change the keyboard variables.
            if event.key == K_LEFT or event.key == K_a:
                moveRight = False
                moveLeft = True
            if event.key == K_RIGHT or event.key == K_d:
                moveLeft = False
                moveRight = True

        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == K_LEFT or event.key == K_a:
                moveLeft = False
            if event.key == K_RIGHT or event.key == K_d:
                moveRight = False

            if event.key == K_x:
                player.top = random.randint(0, WINDOWHEIGHT - player.height)
                player.left = random.randint(0, WINDOWWIDTH - player.width)
            if event.key == K_m:
                if musicPlaying:
                    pygame.mixer.music.stop()
                else:
                    pygame.mixer.music.play(-1, 0.0)
                musicPlaying = not musicPlaying



    # Draw the background onto the surface.
    background = pygame.image.load('background.jpg').convert()
    background = pygame.transform.scale(background, (1280, 720))
    rect = background.get_rect()
    windowSurface.blit(background, rect)

    # Move the player.
    if moveLeft and player.left > 0:
        player.left -= MOVESPEED
    if moveRight and player.right < WINDOWWIDTH:
        player.right += MOVESPEED


      # Process each present in the list
    for i in range(len(presents)):

        # Draw the present
        windowSurface.blit(presentStretchedImage, present)

        #  Make present fall.
        presents[i][1] += 2

        # If the present leaves screen
        if presents [i][1] > 700:
            # Reset it just above the top
            y = random.randrange(-500, -10)
            presents[i][1] = y
            # Give it a new x position
            x = random.randrange(0, 800)
            presents[i][0] = x

    # Process each coal in the list
    for z in range(len(coal)):

        # Draw the coal
        windowSurface.blit(coalStrechedImage, coal)

        # Make coal fall.
        coals[z][1] += 3

        # If the coal leaves screen
        if coals [z][1] > 700:
            # Reset it just above the top
            p = random.randrange(-500, -10)
            coals[z][1] = p
            # Give it a new x position
            t = random.randrange(0, 800)
            coals[z][0] = t



    # Check number of presnts on screen.

    # Draw the player onto the surface.
    windowSurface.blit(playerStretchedImage, player)

    # Check whether the player has intersected with any presents.
    for present in presents[:]:
        if player.colliderect(present):
            presents.remove(present)
            maxPresent -= 1
            score += 1
            finalScore = score
            if musicPlaying:
                pickUpSound.play()

    # Check whether the player has intersected with any coal.
    for coal in coals[:]:
        if player.colliderect(coal):
            coals.remove(coal)
            score -= 2
            if musicPlaying:
                pickCoal.play()                


    # Draw text.
    scoretext = myfont.render("Score {0}".format(score), 1, (255,0,0))
    windowSurface.blit(scoretext, (600, 0))

    # Game Over.
    if maxPresent == 0:
        gameover = myfont.render('GAME OVER YOUR SCORE WAS {0}'.format(finalScore), 1, (255,0,0))
        best = myfont.render('BEST POSSIBLE SCORE = {} '.format(highScore), 1, (255,0,0))
        windowSurface.blit(gameover, (100, 350))
        windowSurface.blit(best, (150, 400))
        pygame.display.update()


    # Draw the presents.
    for present in presents:
        windowSurface.blit(presentStretchedImage, present)

    # Draw the coal
    for coal in coals:
        windowSurface.blit(coalStrechedImage, coal)

    # Draw the window onto the screen.
    pygame.display.update()
    mainClock.tick(40 )

0 个答案:

没有答案