简单pygame程序的修复

61 阅读3分钟

一位Python新手修改了dodger游戏(链接:inventwithpython.com/dodger.py),…

2. 解决方案

  1. 在 # Add new baddies_type_1 at the top of the screen, if needed. 代码后,添加 baddies_type_1.append(newbaddie_type_1) 将坏人添加到列表中。
  2. 在 # Add new goddies_type_1 at the top of the screen, if needed. 代码后,添加 goddies_type_1.append(newgoddie_type_1) 将奖励添加到列表中。
  3. 将代码中的 goodies 拼写统一改为 goddies。

以下是纠错后的代码:

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

#Message to make the user decide the hardness of the game
msg = 'Inserisci un numero da 1 a 20\n per la difficoltà: \n1 = Semplice\n 20 = Impossibile'
title = 'Difficoltà'

#Message to make the user decide the colour of the background of the game
Difficoltà = easygui.enterbox(msg,title)
msg   = "Quale colore preferisci fra questi come sfondo?"
choices = ["Nero","Blu","Verde"]
COLORESCELTODALLUTENTE = easygui.buttonbox(msg,choices=choices)

#Unused Values as it runs in fullscreen mode
WINDOWWIDTH = 800
WINDOWHEIGHT = 600

#The text is white
TEXTCOLOR = (255, 255, 255)

#Changes the colour of the background according to the choice of the user
if COLORESCELTODALLUTENTE == 'Nero':
    BACKGROUNDCOLOR = (0, 0, 0)
elif COLORESCELTODALLUTENTE == 'Blu':
    BACKGROUNDCOLOR = (36, 68, 212)
elif COLORESCELTODALLUTENTE == 'Verde':
    BACKGROUNDCOLOR = (36, 237, 52)

#Frames per second the game will run at
FPS = 40

#Description of the baddies
baddie_type_1MINSIZE = 20
baddie_type_1MAXSIZE = 40
baddie_type_1MINSPEED = 4
baddie_type_1MAXSPEED = 5
ADDNEWbaddie_type_1RATE = 21 - int(Difficoltà)

#Description of the goddies
goddie_type_1MINSIZE = 20
goddie_type_1MAXSIZE = 40
goddie_type_1MINSPEED = 4
goddie_type_1MAXSPEED = 5
ADDNEWgoddie_type_1RATE = 10

#How fast you move with the arrows
PLAYERMOVERATE = 5

def terminate():
    pygame.quit()
    sys.exit()

def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: # pressing escape quits
                    terminate()
                return

def playerHasHitbaddie_type_1(playerRect, baddies_type_1):
    for b in baddies_type_1:
        if playerRect.colliderect(b['rect_b']):
            return True
    return False

def playerHasHitgoddie_type_1(playerRect, goddies_type_1):
    for g in goddies_type_1:
        if playerRect.colliderect(g['rect_g']):
            return True
    return False

def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

# set up pygame, the window, and the mouse cursor
pygame.init()
mainClock = pygame.time.Clock()
#This down here is windowed mode
#windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
#This down here is fullscreen mode
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), pygame.FULLSCREEN)
pygame.display.set_caption('Dodger')
pygame.mouse.set_visible(False)

# set up fonts
font = pygame.font.SysFont(None, 48)

# set up sounds
gameOverSound = pygame.mixer.Sound('Gameover.wav')
pygame.mixer.music.load('Background.mp3')

# set up images
playerImage = pygame.image.load('Player.png')
playerRect = playerImage.get_rect()
baddie_type_1Image = pygame.image.load('Baddie_type_1.png')
goddie_type_1Image = pygame.image.load('Goddie_type_1.png')

# show the "Start" screen
drawText('Dodger', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
drawText('Press a key to start.', font, windowSurface, (WINDOWWIDTH / 3) - 30, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()


topScore = 0
while True:
    # set up the start of the game
    baddies_type_1 = []
    goddies_type_1 = []
    score = 0
    playerRect.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT - 50)
    moveLeft = moveRight = moveUp = moveDown = False
    reverseCheat = slowCheat = False
    baddie_type_1AddCounter = 0
    goddie_type_1AddCounter = 0
    pygame.mixer.music.play(-1, 0.0)

    while True: # the game loop runs while the game part is playing
        score += 1 # increase score

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

            if event.type == KEYDOWN:
                if event.key == ord('z'):
                    reverseCheat = True
                if event.key == ord('x'):
                    slowCheat = True
                if event.key == K_LEFT or event.key == ord('a'):
                    moveRight = False
                    moveLeft = True
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveLeft = False
                    moveRight = True
                if event.key == K_UP or event.key == ord('w'):
                    moveDown = False
                    moveUp = True
                if event.key == K_DOWN or event.key == ord('s'):
                    moveUp = False
                    moveDown = True

            if event.type == KEYUP:
                if event.key == ord('z'):
                    reverseCheat = False
                    score = 0
                if event.key == ord('x'):
                    slowCheat = False
                    score = 0
                if event.key == K_ESCAPE:
                        terminate()

                if event.key == K_LEFT or event.key == ord('a'):
                    moveLeft = False
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveRight = False
                if event.key == K_UP or event.key == ord('w'):
                    moveUp = False
                if event.key == K_DOWN or event.key == ord('s'):
                    moveDown = False

            if event.type == MOUSEMOTION:
                # If the mouse moves, move the player where the cursor is.
                playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery)

        # ize is for size
        # Add new baddies_type_1 at the top of the screen, if needed.
        if not reverseCheat and not slowCheat:
            baddie_type_1AddCounter += 1
        if baddie_type_1AddCounter == ADDNEWbaddie_type_1RATE:
            baddie_type_1AddCounter = 0
            baddies_type_1ize = random.randint(baddie_type_1MINSIZE, baddie_type_1MAXSIZE)
            newbaddie_