简体   繁体   中英

How to make enemies move across the screen

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'), 0, (14 / 18)),
    pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R2.png'), 0, (14 / 18)),
    pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R3.png'), 0, (14 / 18)),
    pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R4.png'), 0, (14 / 18)),
    pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R5.png'), 0, (14 / 18)),
    pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R6.png'), 0, (14 / 18)),
    pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R7.png'), 0, (14 / 18)),
    pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R8.png'), 0, (14 / 18)),
    pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/R9.png'), 0, (14 / 18)),
]

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

fireball = [
    pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB001.png'), 0, 3), True, False),
    pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB002.png'), 0, 3), True, False),
    pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB003.png'), 0, 3), True, False),
    pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB004.png'), 0, 3), True, False),
    pygame.transform.flip(pygame.transform.rotozoom(pygame.image.load(f'{path}/Images/FB005.png'), 0, 3), True, 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.x, 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.x, 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()

I am trying to make a game really similar to Flappy Bird, but I am having trouble making the fireballs move along the screen entirely, I have not even come up with a way that might even be partway successful, and this is my last resort before I go digging on the internet for someone's code. The only way to do this is to pass a list of x values and then plug those into the class, and then change the xs, then after they go off of the screen, they are removed and a new set fo xs are passed in.

The FireBall class already has a move method. You just need to use it.

Make these changes:

In the FireBall move method:

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

In the main loop:

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

gameWindow()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM