Pygame,python,全局变量,崩溃

时间:2018-04-28 22:19:51

标签: python pygame

pygame相对较新。我正在尝试按下指定键时发射子弹。但是,当按下“播放”时,pygame会崩溃。我认为这是由于子弹问题。任何意见都表示赞赏。

import pygame
import time
import random
import sys
import os
pygame.init()

display_width=800
display_height=600
white=(255,255,255)
red=(200,0,0)
green=(0,200,0)
black=(0,0,0)

blue=(0,0,200)
bright_green=(0,255,0)
bright_red=(255,0,0)
gameDisplay=pygame.display.set_mode((800,600))  

clock=pygame.time.Clock()
zheImg=pygame.image.load("xeon.png").convert_alpha()
bgImg=pygame.image.load("Spacebackground.jpg").convert()
xeon_width=300


def xeon(x,y): ##Character

    gameDisplay.blit(zheImg,(x,y))

def quitgame():
    pygame.quit()
    quit()


def bullets(x,y,bulletw,bulleth,color):
    fire=True
    while True:
        pygame.draw.rect(gameDisplay,color,[x,y,bulletw,bulleth])

#textstuff, no problems from here

def text_objects(text,font):
    textSurface=font.render(text,True,black)
    return textSurface,textSurface.get_rect()

def obstacles(obstaclex,obstacley,obstaclew,obstacleh,color):
    pygame.draw.rect(gameDisplay,color,[obstaclex,obstacley,obstaclew,obstacleh])

def obstacles2(obstacle2x,obstacle2y,obstacle2w,obstacle2h,color):
    pygame.draw.rect(gameDisplay,color,[obstacle2x,obstacle2y,obstacle2w,obstacle2h,])



def message_display(text):
    largeText=pygame.font.Font("freesansbold.ttf",115)
    TextSurf,TextRect=text_objects(text,largeText)
    TextRect.center=((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf,TextRect) 
    pygame.display.update()
    time.sleep(1)
    game_loop()

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()

    if x+w>mouse[0]>x and y+h>mouse[1]>y:
        pygame.draw.rect(gameDisplay,ac,(x,y,w,h))
        if click[0]==1 and action !=None:
            action()
            if action=="Play":
                game_loop()
            elif action=="Quit":
                pygame.quit()
                quit()

    else:
        pygame.draw.rect(gameDisplay,ic,(x,y,w,h))  
    smallText=pygame.font.Font("freesansbold.ttf",20)
    textSurf,textRect=text_objects(msg,smallText)
    textRect.center=(   (x+(w/2)),(y+(h/2)) )
    gameDisplay.blit(textSurf,textRect)
    largeText=pygame.font.Font("freesansbold.ttf",115)


def game_intro():
    intro=True

    while intro:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.fill(white)
        largeText=pygame.font.Font("freesansbold.ttf",115)
        TextSurf,TextRect=text_objects("Xeon",largeText)
        TextRect.center=((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf,TextRect)
        xeon(10,100)
        button("Play",150,450,100,50,green,bright_green,game_loop)
        button("Quit",550,450,100,50,red,bright_red,quitgame)

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

- 到这里

def game_loop():



    x=(display_width*.10)
    y=(display_height*0.52)
    x_change=0
    y_change=0
    x+=x_change
    y+=y_change


    obstacle_speed=7
    obstacle_height=100
    obstacle_width=50
    obstacle_startx=random.randrange(300,display_width)
    obstacle_starty=-100


    obstacle2_width=20
    obstacle2_height=20
    obstacle2_startx=800
    obstacle_starty=100



#GLOBAL BULLETS

    global bullets  
    fire=False  


    bullet_height=10
    bullet_width=20
    bullet_startx=xeon_width-115
    bullet_starty=xeon_width+120
    bullet_speed=7

#KEYS

    gameExit=False
    while not gameExit:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_RIGHT:
                    x_change=-5
                    print(y)

                if event.key==pygame.K_LEFT:
                    x_change=+5
                if event.key==pygame.K_UP:
                    y_change=-15
                    if (y<=-2):
                        y_change=+10                

                if event.key==pygame.K_DOWN:
                    fire=True
         ###DRAW BULLET IF KEYDOW




            if event.type==pygame.KEYUP:
                if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
                    x_change=0
        x+=x_change
        y+=y_change

        if (y<=-2):
            y_change=+10

        if (y>=312):
            y_change=0


        rel_x=x%bgImg.get_rect().width
        gameDisplay.blit(bgImg,(rel_x-bgImg.get_rect().width,0))

        gameDisplay.blit(bgImg,(rel_x,0))
        xeon(x_change,y)

CALL BULLETS

        bullets(bullet_startx,bullet_starty,bullet_width,bullet_height,red)
        bullet_startx+=bullet_speed 


#REST IS NON ESSENTIAL

obstacles2(obstacle2_startx,obstacle_starty,obstacle2_width,obstacle2_height,blue)


        obstacles(obstacle_startx,obstacle_starty,obstacle_width,obstacle_height,red)
        obstacle_starty+=obstacle_speed



        if obstacle_starty>display_height:
            obstacle_starty=0-obstacle_height
            obstacle_startx=random.randrange(300,display_width)

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





    game_intro()
    game_loop()
    pygame.quit()
    quit()

0 个答案:

没有答案