将精灵返回原位?

时间:2018-02-15 16:35:46

标签: python-3.x pygame sprite

所以我有一个非常基本的PyGame Python程序,这是一个' car'用户控制的是一个用户控制的一个人。一旦它离开屏幕,我怎样才能让NPC车回到原来的位置?我现在的代码如下所示,感谢您提供的任何帮助!

import pygame, random
from car import Car
from car import AutoCar

pygame.init()
play = True
clock = pygame.time.Clock()
screen = pygame.display.set_mode((700, 750))
pygame.display.set_caption("Car Game")

# Define a bunch of colours
GREEN = (20, 255, 100)
GREY = (210, 210, 210)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
BLUE = (0, 0, 255)

all_sprites = pygame.sprite.Group()  # Creates a list of all sprites in the game

playerCar = Car(RED, 20, 30)  # Defines the car, its colour, and its 
size
playerCar.rect.x = 200
playerCar.rect.y = 300

AutoCar1 = AutoCar(BLUE, 20, 30)
AutoCar1.rect.x = 200
AutoCar1.rect.y = 20

all_sprites.add(playerCar)  # Adds the playerCar to the list of sprites
all_sprites.add(AutoCar1)  # Adds an automated 'enemy' car which you must avoid



while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # If the detected event is clicking the exit button, stop the program.
        play = False
    elif event.type == pygame.KEYDOWN:  # If a key is pressed and the key is 'x', quit the game.
        if event.key == pygame.K_x:
            play = False

keys = pygame.key.get_pressed()  # Defines a variable keys that consists of whatever event is happening in the game
if keys[pygame.K_LEFT]:
    playerCar.move_left(5)
if keys[pygame.K_RIGHT]:
    playerCar.move_right(5)
if keys[pygame.K_DOWN]:
    playerCar.move_backward(5)
if keys[pygame.K_UP]:
    playerCar.move_forward(5)

all_sprites.update()  # Draws in all sprites
AutoCar1.auto_move_forward(3)  # Makes the 'enemy' move forward automatically.



# Create the background
screen.fill(GREEN)
# Create the road
pygame.draw.rect(screen, GREY, [100, 0, 500, 750])
# Draw the lines on the road
pygame.draw.line(screen, WHITE, [340, 0], [340, 750], 5)

# Draw in all the sprites
all_sprites.draw(screen)

pygame.display.flip()
clock.tick(60)  # Cap limit at 60FPS

pygame.quit()

我还有一个定义Car和Autocar类的文件:

import pygame
WHITE = (255, 255, 255)

class Car(pygame.sprite.Sprite):  # Creates a class representing a car, derived from the sprite class

    def __init__(self, color, width, height):
        # Call the parent class (i.e. Sprite) constructor
        super().__init__()
        self.image = pygame.Surface([width,height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)  # Sets white as transparent for this class

        # alternatively load a proper car picture:
        # self.image = pygame.image.load("car.png").convert_alpha()

        pygame.draw.rect(self.image, color, [0, 0, width, height])
        self.rect = self.image.get_rect()  # Fetches the rectangle with the dimensions of the image

    def move_right(self, pixels):  # Adds to the x value of the car the desired amount of pixels
        self.rect.x += pixels

    def move_left(self, pixels):  # Same as move_right()
        self.rect.x -= pixels

    def move_forward(self, pixels):
        self.rect.y -= pixels

    def move_backward(self, pixels):
        self.rect.y += pixels

class AutoCar(pygame.sprite.Sprite):

    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)

        pygame.draw.rect(self.image, color, [0, 0, width, height])
        self.rect = self.image.get_rect()

    def auto_move_forward(self, pixels):
        self.rect.y += pixels

    def return_to_top(self):

1 个答案:

答案 0 :(得分:1)

只需将起始位置存储为AutoCar的属性,如果精灵位于屏幕下方,请检查update方法,然后设置self.rect.topleft(或另一个rect属性)到self.start_position

class AutoCar(pygame.sprite.Sprite):

    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        pygame.draw.rect(self.image, color, [0, 0, width, height])
        self.rect = self.image.get_rect()
        self.start_position = (200, -50)
        self.rect.topleft = self.start_position

    def auto_move_forward(self, pixels):
        self.rect.y += pixels

    def return_to_top(self):
        self.rect.topleft = self.start_position

    def update(self):
        if self.rect.top > 750:  # Screen height.
            self.rect.topleft = self.start_position
相关问题