如何使用Pygame在Python中左右移动机器人?

时间:2019-07-04 10:37:10

标签: python

我的代码中有一个僵尸,应该在屏幕上左右移动。我设法将僵尸向右移动,但无法设法将其向左移动。我如何让僵尸在完成向右移动后向左移动?

我尝试过让屏幕尺寸的宽度大于zx的值(zx代表僵尸的x坐标),但是一旦到达屏幕末端,它就会停止。

import pygame
import time
from pygame.locals import *
import random
pygame.init()

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 700
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT,))
pygame.display.set_caption("Zombie Hunters")

background = pygame.image.load("background.jpg").convert()
background = pygame.transform.scale(background, (SCREEN_WIDTH,SCREEN_HEIGHT))

player = pygame.image.load("character_right.png").convert_alpha()
player = pygame.transform.scale(player, (270, 270))

zombie = pygame.image.load("zombie_right.png").convert_alpha()
zombie = pygame.transform.scale(zombie, (115,140))

#Coordinates
x, y = 0, 0
MOVE_RIGHT = 1
MOVE_LEFT = 2
MOVE_UP = 3
MOVE_DOWN = 4
direction = 0
speed = 0.5

#Zombie coordinate

#zx stands for the zombie's x coordinate
zx = 0

#zy stands for the zombie's y coordinate
zy = 0

#zpeed stands for the zombies speed
zspeed = 0.12

#House rectangle
rectangle_xlocation = 345
rectangle_ylocation = 80
rectangle_width = 190
rectangle_height = 260

#House barrier
barrier_xlocation = 450
barrier_ylocation = 200
barrier_width = 0.1
barrier_height = 50

#time
start = time.time()
end = time.time()

#Text
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render('WAVE 1', True, (255,50,50))
textRect = text.get_rect()  
textRect.center = (400,400)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

        if event.type == KEYDOWN:
            if event.key == ord('q'):
                pygame.quit()
                exit()

            if event.key == K_LEFT:
                player = pygame.image.load("character_left.png").convert_alpha()
                player = pygame.transform.scale(player, (270, 270))
                direction = MOVE_LEFT

            if event.key == K_RIGHT:
                player = pygame.image.load("character_right.png").convert_alpha()
                player = pygame.transform.scale(player, (270, 270))
                direction = MOVE_RIGHT

            if event.key == K_UP:
                player = pygame.image.load("character_up.png").convert_alpha()
                player = pygame.transform.scale(player, (270, 270))
                direction = MOVE_UP

            if event.key == K_DOWN:
                player = pygame.image.load("character_down.png").convert_alpha()
                player = pygame.transform.scale(player, (270, 270))
                direction = MOVE_DOWN

        if event.type == KEYUP:

            if event.key == K_LEFT:
                direction = 0

            if event.key == K_RIGHT:
                direction = 0

            if event.key == K_UP:
                direction = 0

            if event.key == K_DOWN:
                direction = 0

    px,py = x,y

    if(direction == MOVE_LEFT):
        x-= speed

    if(direction == MOVE_RIGHT):
        x+= speed

    if(direction == MOVE_UP):
        y-= speed

    if(direction == MOVE_DOWN):
        y += speed

    #Player doesn't walk out of screen
    if x > 630 and (direction == MOVE_RIGHT):
        x -= speed

    if x < -100 and (direction == MOVE_LEFT):
        x += speed

    if y > 540 and (direction == MOVE_DOWN):
        y -= speed

    if y < -100 and (direction == MOVE_UP):
        y += speed

    #zx stands for the zombie's x coordinate
    #zy stands for the zombie's y coordinate
    if zx < 700: #zombie goes right
        zx += zspeed



    # set player and barrier rectangle
    playerRect = pygame.Rect(x, y, * player.get_size())
    barrierRect = pygame.Rect(barrier_xlocation, barrier_ylocation, barrier_width, barrier_height)
    pygame.draw.rect(screen, (255,0,0), barrierRect)
    # check for collision
    if playerRect.colliderect(barrierRect):
    # reset position
        x, y = px, py

    screen.blit(background, (0, 0))
    screen.blit(player, (x,y))
    screen.blit(zombie, (zx,zy + 400))
    pygame.display.update()

我希望僵尸到达屏幕末端后再向左走。 经验:我的僵尸走了,但是一旦达到700像素就停止了 我想要一些代码,以便我的僵尸向右移动后再向左移动。 记住:zx代表僵尸的x坐标,zx代表僵尸的y坐标。

0 个答案:

没有答案