Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame按键应该只调用一次函数_Python_Function_Pygame_Key Events - Fatal编程技术网

Python Pygame按键应该只调用一次函数

Python Pygame按键应该只调用一次函数,python,function,pygame,key-events,Python,Function,Pygame,Key Events,嗨,书名上写的基本上都是。。。 更详细地描述一下: 这是一个二维随机波浪图,你可以移动一个玩家,他可以开采煤炭。 问题是,如果他站在煤块上,一次又一次地压缩空间,那么煤块就会一次又一次地添加到他的库存中,这就是问题所在。即使没有更多的煤块,他也可以开采更多的煤块。 我搜索过谷歌,但我发现的唯一一件事是,我必须使用标志,一些布尔值…但如何 我知道代码写得很脏,难看等…可以优化 import random import pygame import Images_raw from colors imp

嗨,书名上写的基本上都是。。。 更详细地描述一下: 这是一个二维随机波浪图,你可以移动一个玩家,他可以开采煤炭。 问题是,如果他站在煤块上,一次又一次地压缩空间,那么煤块就会一次又一次地添加到他的库存中,这就是问题所在。即使没有更多的煤块,他也可以开采更多的煤块。 我搜索过谷歌,但我发现的唯一一件事是,我必须使用标志,一些布尔值…但如何

我知道代码写得很脏,难看等…可以优化

import random
import pygame
import Images_raw
from colors import *
from timeit import default_timer as timer


pygame.init()
#----------------------------------------------------------------------------------------------------------------------
#pygame Display settings
display_width=704;  display_height=512
gameDisplay=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('2D Coal miner')

# Misc
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 25)
mouseX=int;    mouseY=int

class Map:
    def __init__(self):
        global mapp,mapSize,mappSurfaces,QD_array,inventory,ammount,minedCoal
        mapSize=100
        ammount=0
        mapp=[[random.randint(0,1) for i in range(mapSize)] for i in range(mapSize)]
        mappSurfaces=mapp
        QD_array=[[random.randint(0,1) for i in range(mapSize)] for i in range(mapSize)]
        inventory=[[{'item':ammount} for i in range(5)] for i in range(5)]
        minedCoal=[]

    def createMap(self):
        global tileImage,recti,coalRect_list,chacheList
        tileImage=pygame.Surface
        coalRect=pygame.Surface
        coalRect_list=[]
        chacheList=[]

        for i in range(mapSize):
            for j in range(mapSize):
                randNum=random.randint(0,11)
                randNum2=random.randint(0,1)

                if randNum == 0 and randNum2==1:
                    tileImage= Images_raw.coal_img
                    QD_array[i][j]=0

                elif randNum== 2 and randNum2<= 9:
                    tileImage= Images_raw.water_img
                    QD_array[i][j]=1

                elif randNum>= 3 or randNum2>= 12:
                    tileImage= Images_raw.grass_img
                    QD_array[i][j]=2

                elif randNum== 5 or randNum2== 5:
                    tileImage= Images_raw.stone_img
                    QD_array[i][j]=3
                else:
                    tileImage= Images_raw.dirt_img
                    QD_array[i][j]=4

                mappSurfaces[i][j]=tileImage

        for k in range(mapSize):
            for l in range(mapSize):
                if QD_array[k][l]==0:
                    coalRect=pygame.Rect(k*32,l*32,32,32)
                    coalRect_list.append(coalRect)
                else:
                    pass

    def mineCoalAt(self,noCoal):

        for i in range(mapSize):
            for j in range(mapSize):
                if QD_array[i][j]==0:
                    chacheList.append((i,j))

        x=chacheList[noCoal][0]
        y=chacheList[noCoal][1]

        QD_array[x][y]=10

        inventory[0][0]['item']+=1

    def update(self,x_dif,y_dif):
        for i in range(mapSize):
            for j in range(mapSize):
                if QD_array[i][j] == 0:
                    tileImage= Images_raw.coal_img

                elif QD_array[i][j]== 1:
                    tileImage= Images_raw.water_img

                elif QD_array[i][j]== 2:
                    tileImage= Images_raw.grass_img

                elif QD_array[i][j]== 3:
                    tileImage= Images_raw.stone_img

                elif QD_array[i][j]== 10:
                    tileImage= Images_raw.coalUsed_img
                else:
                    tileImage= Images_raw.dirt_img

                mappSurfaces[i][j]=tileImage
                gameDisplay.blit(mappSurfaces[i][j],(x_dif+(i*32),y_dif+(j*32)),None,0)


#----------------------------------------------------------------------------------------------------------------------

# Main game function
#----------------------------------------------------------------------------------------------------------------------
def gameLoop():
    gameExit=False; outRight=False
    player_X=11;   player_Y=8
    allowMovement=False
    moveRight,moveLeft,moveUp,moveDown=False,False,False,False
    Space,sSpace=False,False
    x_difference,y_difference=0,0
    mapO=Map()
    mapO.createMap()
    counter=0
    allowMovement=True


    while not gameExit:
#-----------event loop---------------------------------------------------------
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                gameExit = True

            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_DOWN):
                    moveDown=True
                elif(event.key == pygame.K_UP):
                    moveUp=True
                elif(event.key == pygame.K_LEFT):
                    moveLeft=True
                elif(event.key == pygame.K_RIGHT):
                    moveRight=True
                elif(event.key == pygame.K_p):
                    print(player_X*32,player_Y*32)
                elif(event.key == pygame.K_SPACE):
                    Space=True
                elif(event.key == pygame.K_i):
                    print(inventory)

            elif event.type == pygame.KEYUP:
                if (event.key == pygame.K_DOWN):
                    moveDown=False
                elif(event.key == pygame.K_UP):
                    moveUp=False
                elif(event.key == pygame.K_LEFT):
                    moveLeft=False
                elif(event.key == pygame.K_RIGHT):
                    moveRight=False
                elif(event.key == pygame.K_SPACE):
                    Space=False


#-----------event loop END-----------------------------------------------------
        a=len(coalRect_list)

        #Logical/Main part

        if allowMovement==True:
            if moveRight==True:
                player_X+=1
                x_difference-=32
                for i in range(a):
                    coalRect_list[i].x-=32

            elif moveLeft==True:
                player_X-=1
                x_difference+=32
                for i in range(a):
                    coalRect_list[i].x+=32

            elif moveDown==True:
                 player_Y+=1
                 y_difference-=32
                 for i in range(a):
                    coalRect_list[i].y-=32

            elif moveUp==True:
                player_Y-=1
                y_difference+=32
                for i in range(a):
                    coalRect_list[i].y+=32

            else:
                player_X+=0;player_Y+=0
                y_difference+=0;x_difference+=0

            if Space==True:
                sSpace=True
            else:
                sSpace=False
        else:
            pass

        #Drawing objects to screen part
        gameDisplay.fill(white)  #Backgroundcolor

        mapO.update(x_difference,y_difference)
        playerRect=gameDisplay.blit(Images_raw.man_img,(player_X*0+352,player_Y*0+256),None,0)

        aba=playerRect.collidelist(coalRect_list)



        '''
        if aba!=-1 and sSpace==True:
            counter+=1


            if counter==1:
                minedCoal.append(aba)
                mapO.mineCoalAt(aba)
                inventory[0][0]['item']+=1
            elif counter==2:
                counter=0

            else:
                pass
        '''

        if aba!=1:
            if sSpace==True:
                minedCoal.append(aba)
                mapO.mineCoalAt(aba)

            else:
                pass

        #Update everything and set FPS-timer
        pygame.display.update()
        clock.tick(20)

    #Needed so that the program gets closed properly
    pygame.quit()
    quit()
#----------------------------------------------------------------------------------------------------------------------
#End of the Main game function
#---Normal coding goes on

#call the gameLoop
gameLoop()
随机导入
导入pygame
导入原始图像
从颜色导入*
从timeit导入默认\u计时器作为计时器
pygame.init()
#----------------------------------------------------------------------------------------------------------------------
#pygame显示设置
显示宽度=704;显示高度=512
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题('2D煤矿工人')
#杂项
clock=pygame.time.clock()
font=pygame.font.SysFont(无,25)
mouseX=int;mouseY=int
类图:
定义初始化(自):
全局mapp、mapSize、mappSurfaces、QD_阵列、库存、弹药量、minedCoal
mapSize=100
amount=0
mapp=[[random.randint(0,1)表示范围内的i(mapSize)]表示范围内的i(mapSize)]
mappSurfaces=mapp
QD_数组=[[random.randint(0,1)表示范围内的i(mapSize)]表示范围内的i(mapSize)]
库存=[[{'item':amount}针对范围(5)内的i]针对范围(5)内的i]
minedCoal=[]
def createMap(自):
全局tileImage、recti、coalRect_列表、chacheList
tileImage=pygame.Surface
coalRect=pygame.Surface
coalRect_列表=[]
chacheList=[]
对于范围内的i(贴图大小):
对于范围内的j(贴图大小):
randNum=random.randint(0,11)
randNum2=random.randint(0,1)
如果randNum==0且randNum2==1:
tileImage=图像\原煤\图像
QD_阵列[i][j]=0
elif randNum==2,randNum2=3或randNum2>=12:
tileImage=图像\u原始草\u图像
量子点阵列[i][j]=2
elif randNum==5或randNum2==5:
tileImage=图像\u原始石头\u图像
量子点阵列[i][j]=3
其他:
tileImage=图像\u原始。污垢\u图像
量子点阵列[i][j]=4
mappSurfaces[i][j]=tileImage
对于范围内的k(贴图大小):
对于范围内的l(贴图大小):
如果QD_数组[k][l]==0:
coalRect=pygame.Rect(k*32,l*32,32,32)
coalRect_list.append(coalRect)
其他:
通过
def mineCoalAt(自身,无煤):
对于范围内的i(贴图大小):
对于范围内的j(贴图大小):
如果QD_数组[i][j]==0:
chacheList.append((i,j))
x=查切利斯特[noCoal][0]
y=chacheList[noCoal][1]
量子点阵列[x][y]=10
存货[0][0]['item']+=1
def更新(自我、x_dif、y_dif):
对于范围内的i(贴图大小):
对于范围内的j(贴图大小):
如果QD_数组[i][j]==0:
tileImage=图像\原煤\图像
elif QD_数组[i][j]==1:
tileImage=图像\原水\图像
elif QD_数组[i][j]==2:
tileImage=图像\u原始草\u图像
elif QD_数组[i][j]==3:
tileImage=图像\u原始石头\u图像
elif QD_阵列[i][j]==10:
tileImage=图像\u原始使用的煤炭\u图像
其他:
tileImage=图像\u原始。污垢\u图像
mappSurfaces[i][j]=tileImage
blit(mappSurfaces[i][j],(x_dif+(i*32),y_dif+(j*32)),无,0)
#----------------------------------------------------------------------------------------------------------------------
#主要游戏功能
#----------------------------------------------------------------------------------------------------------------------
def gameLoop():
gameExit=False;直接=错误
玩家_X=11;玩家Y=8
allowMovement=False
moveRight,moveLeft,moveUp,moveDown=假,假,假,假
空格,sSpace=False,False
x_差,y_差=0,0
mapO=Map()
createMap()
计数器=0
allowMovement=True
不退出游戏时:
#-----------事件循环---------------------------------------------------------
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
gameExit=True
elif event.type==pygame.KEYDOWN:
如果(event.key==pygame.K_DOWN):
向下移动=真
elif(event.key==pygame.K_UP):
向上移动=真
elif(event.key==pygame.K_左):
moveLeft=True
elif(event.key==pygame.K_RIGHT):
moveRight=True
elif(event.key==pygame.K\p):
打印(播放器X*32,播放器Y*32)
elif(event.key==pygame.K_空间):
空格=真
elif(event.key==pygame.K_i):
打印(库存)
elif event.type==pygame.KEYUP:
如果(event.key==pygame.K_DOWN):
向下移动=错误
elif(event.key==pygame.K_UP):
moveUp=False
elif(event.key==pygame.K_左):
moveLeft=False
elif(event.key==pygame.K_RIGHT):
moveRight=False
elif(event.key==pygame.K_空间):
空格=假
#-----------事件循环结束-----------------------------------------------------
a=len(coalRect_列表)
#逻辑/主要部分
如果allowMovement==真:
如果moveRight==True:
玩家_X+=1
aba=playerRect.collidelist(coalRect_list)
if aba!=1:
    if sSpace==True:
         minedCoal.append(aba)
         mapO.mineCoalAt(aba)
     else:
         pass
if aba != -1 and sSpace:
    ...
if aba != -1 and sSpace:
    minedCoal.append(aba)
    mapO.mineCoalAt(aba)
    del coalRect_list[aba]