我的pygame在开始,碰撞或者感觉像是

时间:2015-11-22 18:43:20

标签: python

我正在编写一个' catch'角色被一个敌人追逐的游戏,并且必须追逐另外两个角色才能获得积分。我的代码在大多数情况下工作正常,但在启动时会随机崩溃,当角色追逐与敌人或其他角色碰撞时,或者只是在游戏中。在过去的两周里,我和老师一直在讨论这个问题,但是我们都不知道出了什么问题。他告诉我按原样递交,但我确实想知道如何让它工作。如果有人对如何解决问题有任何见解,或者可能存在什么问题,那我就非常感激。

#Animation Programming Assignment: Cat and Mouse

import pygame, sys
from pygame.locals import *

import random

pygame.mixer.pre_init(frequency=22050, size=-16, channels=1, buffer=256)
pygame.init()

#Screen Junk
pygame.init()
width = 656
height = 498
screen = pygame.display.set_mode((width, height))
black = (0, 0, 0)


#All the text set up stuff
pygame.display.set_caption("Cat and Mouse")


#Title
titleObj = pygame.font.Font("freesansbold.ttf", 22)

titleTxt = "Cat and Mouse"


#Start
startObj = pygame.font.Font("freesansbold.ttf", 18)

startTxt = "Press 'P' to Start!"


#Score
scoreObj = pygame.font.Font( "freesansbold.ttf" , 22)

score = 0
scoreTxt = "Score: " + str(score)


#Lives
livesObj = pygame.font.Font( "freesansbold.ttf" , 22)

lives = 3
livesTxt = "Lives: " + str(lives)


#Music
audioObj = pygame.font.Font( "freesansbold.ttf" , 12)

audioTxt = "Music by Visager"


#End
endObj = pygame.font.Font( "freesansbold.ttf" , 22)

endTxt = "Press 'P' to play again!"


#Images
bakImg = pygame.image.load("bkgnd.png").convert()
catImg = pygame.image.load("catFace.png").convert_alpha()
dogImg = pygame.image.load("dogFace.png").convert_alpha()
mosImg = pygame.image.load("mouse.png").convert_alpha()
yrnImg = pygame.image.load("yarn.png").convert_alpha()
scrImg = scoreObj.render(scoreTxt, True, black)
livImg = livesObj.render(livesTxt, True, black)
audImg = audioObj.render(audioTxt, True, black)
ttlImg = titleObj.render(titleTxt, True, black)
staImg = startObj.render(startTxt, True, black)
endImg = endObj.render(endTxt, True, black)


#Image Widths/Heights
cWid = catImg.get_width()
cHgt = catImg.get_height()

dWid = dogImg.get_width()
dHgt = dogImg.get_height()

mWid = mosImg.get_width()
mHgt = mosImg.get_height()

yWid = yrnImg.get_width()
yHgt = yrnImg.get_height()

sWid = scrImg.get_width()
sHgt = scrImg.get_height()

lWid = livImg.get_width()
lHgt = livImg.get_height()

aWid = audImg.get_width()
aHgt = audImg.get_height()

tWid = ttlImg.get_width()
tHgt = ttlImg.get_height()

stWid = staImg.get_width()
stHgt = staImg.get_height()

eWid = endImg.get_width()
eHgt = endImg.get_height()


#Image X/Y
catX = (width//2) - (cWid/2)
catY = height//2 - cHgt//2

dogX = random.randint(1, width - dWid)
dogY = random.randint(1, height - dHgt)

while dogX < catX + cWid and dogX + dWid > catX and dogY < catY + cHgt and dogY + dHgt > catY:
    dogX = random.randint(1, width - dWid)
    dogY = random.randint(1, height - dHgt)

mosX = width//2
mosY = width//2

while mosX < catX + cWid and mosX + mWid > catX and mosY < catY + cHgt and mosY + mHgt > catY:
    mosX = random.randint(1, width - mWid)
    mosY = random.randint(1, height - mHgt)

yrnX = random.randint(1, width - yWid)
yrnY = random.randint(1, height - yHgt)

while yrnX < catX + cWid and yrnX + yWid > catX and yrnY < catY + cHgt and yrnY + yHgt > catY:
    yrnX = random.randint(1, width - (yWid-10))
    yrnY = random.randint(1, height - (yHgt-10))

scrX = width//2 - sWid//2
scrY = 20

livX = width//2 - lWid//2
livY = 40

audX = (width - aWid) - 10
audY = (height - aHgt) - 10

ttlX = (width//2) - (tWid//2)
ttlY = 60

staX = (width//2) - (stWid//2)
staY = 90


#Speeds
catXSpd = 0
catYSpd = 0

dogXSpd = random.randint(-2, 2)
dogYSpd = random.randint(-2, 2)

while dogXSpd == 0:
    dogXSpd = random.randint(-2, 2)

while dogYSpd == 0:
    dogXSpd = random.randint(-2, 2)

mosXSpd = random.randint(-2, 2)
mosYSpd = random.randint(-2, 2)

while mosXSpd == 0:
    mosXSpd = random.randint(-2, 2)

while mosYSpd == 0:
    mosXSpd = random.randint(-2, 2)

yrnXSpd = random.randint(-2, 2)
yrnYSpd = random.randint(-2, 2)

while yrnXSpd == 0:
    yrnXSpd = random.randint(-2, 2)

while yrnYSpd == 0:
    yrnXSpd = random.randint(-2, 2)


#Audio Things
pygame.mixer.music.load('windyBluffs.mp3')
pygame.mixer.music.play(-1, 0.0)

mosSnd = pygame.mixer.Sound('mosSound.ogg')
dogSnd = pygame.mixer.Sound('dogSound.ogg')
catSnd = pygame.mixer.Sound('catSound.ogg')

#Drawing permanent junk
screen.blit(bakImg, (0, 0))
screen.blit(audImg, (audX, audY))


#Start Screen
start = True
while start:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_p:
                start = False

        if event.type == KEYUP:
            if event.key == K_p:
                running = True

    screen.blit(bakImg, (0, 0))
    screen.blit(ttlImg, (ttlX, ttlY))
    screen.blit(staImg, (staX, staY))

    pygame.display.update()
    pygame.time.delay(5)

#Main Game
running = True
while running:
    playing = True
    while playing:
        #Exit
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            #Keyboard Junk
            if event.type == KEYDOWN:
                if event.key == K_RIGHT:
                    catXSpd = 3
                if event.key == K_LEFT:
                    catXSpd = -3
                if event.key == K_UP:
                    catYSpd = -3
                if event.key == K_DOWN:
                    catYSpd = 3

            if event.type == KEYUP:
                if event.key == K_RIGHT or event.key == K_LEFT:
                    catXSpd = 0
                if event.key == K_UP or event.key == K_DOWN:
                    catYSpd = 0

        screen.blit(scrImg, (scrX, scrY))
        screen.blit(livImg, (livX, livY))

        dogX += dogXSpd
        dogY += dogYSpd

        mosX += mosXSpd
        mosY += mosYSpd

        yrnX += yrnXSpd
        yrnY += yrnYSpd

        if dogX >= width - dWid or dogX <= 0:
            dogXSpd *= -1

        if dogY >= height - dHgt or dogY <= 0:
            dogYSpd *= -1


        if mosX >= width - cWid or mosX <= 0:
            mosXSpd *= -1

        if mosY >= height - mHgt or mosY <= 0:
            mosYSpd *= -1


        if yrnX >= width - cWid or yrnX <= 0:
            yrnXSpd *= -1

        if yrnY >= height - yHgt or yrnY <= 0:
            yrnYSpd *= -1

        catX += catXSpd
        catY += catYSpd

        if catX >=  width - cWid:
            cXSpd = 0
            catX = width - cWid
        elif catX <= 0:
            cXSpd = 0
            catX = 0

        if catY >=  height - cHgt:
            cYSpd = 0
            catY = height - cHgt
        elif catY <= 0:
            cYSpd = 0
            catY = 0


        #Score
        if mosX < catX + cWid and mosX + mWid > catX and mosY < catY + cHgt and mosY + mHgt > catY:
            pygame.init()
            mosSnd.play()
            score = score + 2
            scoreTxt = "Score: " + str(score)
            scrImg = scoreObj.render(scoreTxt, True, black)
            sWid = scrImg.get_width()
            sHgt = scrImg.get_height()
            scrX = width//2 - sWid//2
            scrY = 20

            catX = random.randint(1, (width-cWid))
            catY = random.randint(1, (height-cHgt))

            while mosX < catX + cWid and mosX + mWid > catX and mosY < catY + cHgt and mosY + mHgt > catY:
                mosX = random.randint(1, width - mWid)
                mosY = random.randint(1, height - mHgt)

            mosXSpd = random.randint(-2, 2)
            mosYSpd = random.randint(-2, 2)

            while mosXSpd == 0:
                mosXSpd = random.randint(-2, 2)

            while mosYSpd == 0:
                mosXSpd = random.randint(-2, 2)

        if yrnX < catX + cWid and yrnX + yWid > catX and yrnY < catY + cHgt and yrnY + yHgt > catY:
            catSnd.play()
            score = score + 1
            scoreTxt = "Score: " + str(score)
            scrImg = scoreObj.render(scoreTxt, True, black)
            sWid = scrImg.get_width()
            sHgt = scrImg.get_height()
            scrX = width//2 - sWid//2
            scrY = 20

            yrnX = random.randint(1, (width-mWid))
            yrnY = random.randint(1, (height-mHgt))

            while yrnX < catX + cWid and yrnX + yWid > catX and yrnY < catY + cHgt and yrnY + yHgt > catY:
                yrnX = random.randint(1, width - yWid)
                yrnY = random.randint(1, height - yHgt)

            yrnXSpd = random.randint(-2, 2)
            yrnYSpd = random.randint(-2, 2)

            while yrnXSpd == 0:
                yrnXSpd = random.randint(-2, 2)

            while yrnYSpd == 0:
                yrnXSpd = random.randint(-2, 2)

        #Lives
        if dogX < catX + cWid and dogX + dWid > catX and dogY < catY + cHgt and dogY + dHgt > catY:
            dogSnd.play()
            lives = lives - 1
            livesTxt = "Lives: " + str(lives)
            livImg = livesObj.render(livesTxt, True, black)
            lWid = livImg.get_width()
            lHgt = livImg.get_height()
            livX = width//2 - lWid//2
            livY = 40

            if lives == 0:
                playing = False


            dogX = random.randint(1, (width-dWid))
            dogY = random.randint(1, (height-dHgt))
            dogXSpd = random.randint(-2, 2)
            dogYSpd = random.randint(-2, 2)

            while dogX < catX + cWid and dogX + dWid > catX and dogY < catY + cHgt and dogY + dHgt > catY:
                dogX = random.randint(1, width - dWid)
                dogY = random.randint(1, height - dHgt)

            while dogXSpd == 0:
                dogXSpd = random.randint(-2, 2)

            while dogYSpd == 0:
                dogXSpd = random.randint(-2, 2)

        screen.blit(bakImg, (0, 0))
        screen.blit(mosImg, (mosX, mosY))
        screen.blit(yrnImg, (yrnX, yrnY))
        screen.blit(catImg, (catX, catY))
        screen.blit(dogImg, (dogX, dogY))
        screen.blit(scrImg, (scrX, scrY))
        screen.blit(livImg, (livX, livY))
        screen.blit(audImg, (audX, audY))

        pygame.display.update()
        pygame.time.delay(5)


    #End Screen
    end = True
    while end:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            if event.type == KEYDOWN:
                if event.key == K_p:
                    playing = True
                    start = False

                    lives = 3
                    livesTxt = "Lives: " + str(lives)
                    livImg = livesObj.render(livesTxt, True, black)
                    lWid = livImg.get_width()
                    lHgt = livImg.get_height()
                    livX = width//2 - lWid//2
                    livY = 40

                    score = 0
                    scoreTxt = "Score: " + str(score)
                    scrImg = scoreObj.render(scoreTxt, True, black)
                    sWid = scrImg.get_width()
                    sHgt = scrImg.get_height()
                    scrX = width//2 - sWid//2
                    scrY = 20


            if event.type == KEYUP:
                if event.key == K_p:
                    end = False
                    start = False

                    catX = (width//2) - (cWid/2)
                    catY = 520

                    catXSpd = 0
                    catYSpd = 0
                    screen.blit(catImg, (catX, catY))

        screen.blit(bakImg, (0, 0))
        screen.blit(endImg, ((width//2) - (eWid//2) , (height//2) - (eHgt//2)))

        pygame.display.update()
        pygame.time.delay(5)

0 个答案:

没有答案