如何使敌人在屏幕上移动

问题描述

import pygame
import sys
import os
import random

pygame.init()

path = os.getcwd()

bg = pygame.transform.scale2x(pygame.image.load(f'{path}/Images/index.jpg'))

char = [
    pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R1.png'),(14 / 18)),pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R2.png'),pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R3.png'),pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R4.png'),pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R5.png'),pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R6.png'),pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R7.png'),pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R8.png'),pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R9.png'),]

charFlipped = [
    pygame.transform.flip(char[0],False,True),pygame.transform.flip(char[1],pygame.transform.flip(char[2],pygame.transform.flip(char[3],pygame.transform.flip(char[4],pygame.transform.flip(char[5],pygame.transform.flip(char[6],pygame.transform.flip(char[7],pygame.transform.flip(char[8],]

fireball = [
    pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB001.png'),3),True,False),pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB002.png'),pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB003.png'),pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB004.png'),pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB005.png'),False)
]

winLength = 600
winWidth = 336

screen = pygame.display.set_mode((winLength,winWidth))
pygame.display.set_caption("Space Adventures")

clock = pygame.time.Clock()

running = True


class Background(object):
    def __init__(self,speed):
        self.bgx1 = 0
        self.bgx2 = self.bgx1 + winLength
        self.speed = speed

    def draw(self):
        if self.bgx1 < (0 - winLength):
            self.bgx1 = winLength
        if self.bgx2 < (0 - winLength):
            self.bgx2 = self.bgx1 + winLength

        screen.blit(bg,(self.bgx1,0))
        screen.blit(bg,(self.bgx2,0))

    def move(self):
        self.bgx1 -= self.speed
        self.bgx2 -= self.speed


class Player(object):
    def __init__(self,x,vel,buffer):
        self.x = x
        self.y = 128
        self.vel = vel
        self.buffer = buffer
        self.count = 0
        self.flipped = False
        self.width = 64 * (14 / 18)

    def draw(self):
        if self.count + 1 >= 63:
            self.count = 0

        self.count += 1

        if self.flipped is False:
            screen.blit(char[self.count // 7],(self.x,self.y))

        if self.flipped is True:
            screen.blit(charFlipped[self.count // 7],self.y))

    def move(self):
        if self.flipped is False and self.y < winWidth - self.buffer - self.width:
            self.y += self.vel
        if self.flipped is True and self.y > self.buffer:
            self.y -= self.vel


class Fireball(object):
    def __init__(self,x):
        self.x = x
        self.count = 0

        self.y = 100

        self.vel = 2
        self.passed = False

    def draw(self):
        if self.count + 1 >= 35:
            self.count = 0
        self.count += 1

        screen.blit(fireball[self.count // 7],self.y))

    def move(self):
        self.x -= self.vel


background = Background(0.5)
character = Player(128,2,32)
meteor = Fireball(200)


def gameWindow():
    background.draw()
    character.draw()
    meteor.draw()
    pygame.display.update()


while running:

    clock.tick(60)

    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == pygame.QUIT or keys[pygame.K_ESCAPE]:
            pygame.quit()
            sys.exit()

        elif event.type == pygame.MOUSEBUTTONUP:
            character.flipped = not character.flipped

    background.move()
    character.move()

    gameWindow()

我正在尝试制作一款与《飞鸟》非常相似的游戏,但是我无法使火球完全沿着屏幕移动,我什至没有想出一种可能会取得成功的方法,这就是我的想法在我上网搜索某人的代码之前,不得已。唯一的方法是传递一个x值列表,然后将其插入到类中,然后更改xs,然后在离开屏幕后将其删除,并传入新的fo xs集合。

解决方法

FireBall类已经具有一个move方法。您只需要使用它即可。

进行以下更改:

在FireBall移动方法中:

def move(self):
    self.x -= self.vel
    if self.x < -100:
       self.x = 700    # restart for right side

在主循环中:

background.move()
character.move()
meteor.move()   # add this line

gameWindow()