使用箭头键移动pygame目标图像(范围)时出现问题

时间:2019-07-19 21:34:08

标签: python pygame arrow-keys

当我们运行pygame的代码时,目标作用域图像不会移动,但机器人会生成。我们正在尝试使用箭头键移动它们,并且我包含了所有代码。

根据我们最新的移动试用代码,我们尝试了另外两件事。

import pygame, sys  
from graphics import *  
import time  
import random  

pygame.init()  

level=1  
bg = pygame.image.load('bg.png')  
bg_width = 800  
pygame.display.set_caption('Robot Apocalypse')  
surfacew = 1054  
surfacel = 562  
surface = pygame.display.set_mode((surfacew,surfacel))  
black = (0, 0, 0)  
score = 0  
#player_health = 99  
alive=True  

targetImg = pygame.image.load('target.png')  
targetImg = pygame.transform.scale(targetImg, (40, 40))  
targetxy = targetImg.get_rect()  
targetx = targetxy[0]  
targety = targetxy[1]  

def move_target(targetImg):  
    pygame.event.clear()  
    while alive == True:  
        keys_pressed = pygame.key.get_pressed()  
        if keys_pressed[pygame.K_LEFT]:  
            targetx -= 5  
        if keys_pressed[pygame.K_RIGHT]:  
            targetx += 5  
        if keys_pressed[pygame.K_UP]:  
            targety -= 5  
        if keys_pressed[pygame.K_DOWN]:  
            targety += 5  
        pygame.display.update()  
    # pygame.event.clear()  
    # for event in pygame.event.get():  
    #     if event.type ==KEYDOWN:  
    #         if event.key == K_LEFT:  
    #             direction = MOVE_LEFT  
    #         elif event.key == K_RIGHT:  
    #             direction = MOVE_RIGHT  
    #     elif event.type == KEYUP:  
    #         if event.key == K_LEFT:  
    #             direction = 0  
    #         elif event.key == K_RIGHT:  
    #             direction = 0  
    # if(direction == MOVE_LEFT):  
    #     targetx-=10  
    # elif(direction == MOVE_RIGHT):  
    #     targetx+=10  

    # for event in pygame.event.get():  
    #     print(event)  
    #     if event.type==QUIT:  
    #          pygame.quit()  
    #          sys.exit()  
    #     if event.type == KEYDOWN:  
    #         if event.key == K_LEFT:  
    #             targetx-=5  
    #         elif event.key == K_RIGHT:  
    #             targetx+=5  
    #         elif event.key == K_UP:  
    #             targety-=5  
    #         elif event.key == K_DOWN:  
    #             targety+=5  
    #     pygame.display.update()  

def shoot():  
    #while True:  
    shot = False  

    pos = (targetx, targety)  
    t = screen.blit(robot, (64,64))  
    if t.collidepoint(pos):  
        shot = True  
    return shot  

def generate_robot(x,y):  
    #while displayrobot == True  
    robot=pygame.draw.rect(surface, (255,0,0), (x,y,64,64), 0)  
    for event in pygame.event.get():  
        if event.type == pygame.KEYDOWN:  
            if event.key == pygame.K_SPACE:  
                shoot()  
                if shot == True:  
                    displayrobot = False  
                    cover = surface.blit(bg, (x,y))  
                    pygame.display.update()  
    return x, y  

    #if shot == True:  

def die():  
    message("YOU DIED")  
    pygame.display.update()  

def win(level):  
    level+=1  

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

def message(text):  
    largeText = pygame.font.Font('freesansbold.ttf',60)  
    TextSurf, TextRect = text_objects(text, largeText)  
    TextRect.center = ((surfacew/6),(surfacel/2))  
    surface.blit(TextSurf, TextRect)  
    pygame.display.update()  
    time.sleep(2)  

def main(alive):  
    #displayrobot = True:  
    robot=0  
    score=0  
    surface.fill(black)  
    surface.blit(bg, (0,0))  
    message("Level "+ str(level))  
    mouse = pygame.mouse.get_pos()  
    target = surface.blit(targetImg, (mouse))  
    while alive==True:  
        # robot = enemy(64, 64)  
        x=random.randint(40,760)  
        y=random.randint(40,560)  
        generate_robot(x,y)  
        pygame.display.update()  
        robot+=1  
        time.sleep(8/level)  
    if robot>50 and robot>score:  
        alive=False  
    # if pygame.mouse.get_pressed()==True:  
    #     shoot() #maybe??  
        if shot==True:  
            score+=1  
            robot-=1  
        if robot==10/(level/2): #if 10- robots on screen then...  
            die()  
        if score>25*(level/2):  
            win()  
move_target(targetImg)
main(alive)  
pygame.quit()  
quit()  

。 没有错误消息,但不会移动。我们尝试了很多不同的东西(未包含在内),并查找了许多网站,请帮助我们。谢谢

2 个答案:

答案 0 :(得分:1)

看起来我原来的功能注释仍然“有效”:您的代码不会移动任何游戏对象。

targetxy = targetImg.get_rect()  
targetx = targetxy[0]
targety = targetxy[1]

在这一点上,targetxy是对游戏对象边界矩形的引用。 targetxtargetyrect位置值的副本。

def move_target(targetImg):  
    pygame.event.clear()  
    while alive == True:  
        keys_pressed = pygame.key.get_pressed()  
        if keys_pressed[pygame.K_LEFT]:  
            targetx -= 5
        ...

您已更改了x坐标的本地副本。这不会影响targetImg的位置。您需要更改对象的属性,例如

            targetxy.x -= 5

此后,您需要update游戏画面。

答案 1 :(得分:1)

要移动对象,您不仅需要更改x,y并更新屏幕(将缓冲区发送到将显示它的视频卡),而且还必须清理缓冲区,在缓冲区(blit()中的新位置绘制图像

此代码仅显示有效的move_target。我跳过了其余的代码。

我在target_rect的{​​{1}}中保持位置。您可以将其用于pygame.Rect,但以后也可以用于检查冲突blit(img,rect)

rect.colliderect(other_rect)