在没有用户输入的情况下,我无法使用python2.7和pygam使对象从A点移动到B点

2024-04-24 23:27:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个游戏,玩家(在月亮最右边产卵的时候)可以使用“WASD”键四处移动,敌人会从屏幕顶部掉下来,以便击中玩家并结束游戏。我目前正在为一个任务做这个,我已经设置了一个测试,看看我是否可以这样做,到目前为止,我已经能够创建一个“玩家”对象,它可以由“WASD”键控制,并制作一个需要玩家按“空格”键才能玩游戏的屏幕。我有困难,但能够让敌人落在'Y'轴顺利下降屏幕。只有当用户按下或释放一个键时,图片才会在屏幕上向下移动,这会产生锯齿状的移动,或者当用户将鼠标移到pygame屏幕上时,会产生非常平滑的移动。在

 #import necessary modules and pygame.
    import pygame, sys, random
    from pygame.locals import *
    #set global variables
    pygame.init()
    WINDOWWIDTH =  800
    WINDOWHEIGHT = 800
    BACKGROUNDCOLOUR = (255,255,255)
    TEXTCOLOUR = (0,0,0)
    FPS = 30
    ENEMYMINSIZE = 10
    BOMBSAWAY = -1
    ENEMYMAXSIZE = 40
    ENEMYMINSPEED = 1
    ENEMYMAXSPEED = 10
    ADDNEWENEMYRATE = 5
    PLAYERMOVERATE = 5
    FSIZE = 48
    BLUE = (0,0,255)
    global PLAY
    PLAY = False
    global fpsClock
    fpsClock = pygame.time.Clock()
    # set up pygame and GUI

    MainClock = pygame.time.Clock()
    windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    windowSurface.fill(BACKGROUNDCOLOUR)
    pygame.display.set_caption('Bubble Dash')


    enemy = pygame.image.load('player.jpg')
    char = pygame.image.load('player.jpg')

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

    # set up the text
    text = basicFont.render('Press any key to play!', True, (255,255,0))
    textRect = text.get_rect()
    textRect.centerx = windowSurface.get_rect().centerx
    textRect.centery = windowSurface.get_rect().centery
    # draw the text onto the surface


    # set up x and y coordinates

    # music 
    windowSurface.blit(text, textRect)


    def playgame(PLAY):
        x,y = 0,0
        movex,movey = 0,0
        charx=300
        chary=200
        direction = 'down'
        enemyx= 10
        enemyy=10
        while PLAY:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN: 
                    if event.key == ord('m'):
                        pygame.mixer.music.stop()
                    if event.key == ord('n'):
                        pygame.mixer.music.play()
                    if event.key == ord('a'):
                        movex = -0.5
                    if event.key == ord('d'):
                        movex = 0.5
                    if event.key == ord('w'):
                        movey = -0.5
                    if event.key == ord('s'):
                        movey = 0.5


                if event.type ==KEYUP:
                    if event.key == ord('a'):
                        movex = 0
                    if event.key == ord('d'):
                        movex = 0
                    if event.key == ord('w'):
                        movey = 0
                    if event.key == ord('s'):
                        movey = 0
                if direction == 'down':
                    enemyy += 7
            windowSurface.fill(BLUE)
            windowSurface.blit(char, (charx, chary))
            windowSurface.blit(enemy, (enemyx, enemyy))
            pygame.display.update()
            charx+=movex
            chary+=movey

    def playertopresskey(PLAY):

        while True: # main game loop

            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_SPACE:
                        PLAY = True
                if PLAY == True:
                    playgame(PLAY)
            pygame.display.update()


    playertopresskey(PLAY)

我希望“敌人”对象能够从顶部掉落,而用户无需按键或向上键或必须在屏幕上移动鼠标,而“敌人”只要调用子程序就会从顶部掉落。在

你可能需要调整一下,因为它是为我设置的,但我删除了一些东西,会给你带来麻烦。希望有人能帮我。谢谢。在

下面是一个类似于我的图片的链接,你可以下载并替换代码中的'char'和'defent'变量,以便自己查看,因为我目前无法访问课程。 http://www.roleplaygateway.com/roleplay/the-five-elements/characters/miss-joy/image


Tags: keytexteventplaygetif屏幕type
2条回答

我发现了你的错误,如果你把你的代码分成几个函数,你会自己发现的。问题是:

for event in pygame.event.get():
    if event.type == QUIT:
        ...
    if event.type == KEYDOWN:
        ...
    if event.type == KEYUP:
        ...
    if direction == 'down':
         enemyy += 7

只有在队列中等待事件时,才会调用移动敌人的代码。把它移出循环,你就可以走了。在

您必须更改缩进:

     if direction == 'down':
                enemyy += 7

现在它在for event in pygame.event.get():

相关问题 更多 >