在Pygame中随机产生多个敌人

时间:2018-03-22 12:14:24

标签: python class pygame

基本上我正在为我的学校编写一个编程项目,我最初使用的是模块化编码形式,但很明显OOP是一种更有效的编码方式。我一直在关注一些youtube教程,因为我对OOP没有那么自信,似乎已经碰到了一堵砖墙。我目前正在努力从我的敌人类产生多个敌人,也在屏幕上的随机位置产生。我想也许不得不创建一个列表来附加敌人并从那里去,但我过去几天尝试的所有东西都没有用! 在此先感谢帮助我解决此问题,我将粘贴下面的代码。

    import tkinter as tk
    import pygame
    import time
    import random


    def shutdown():
      root.destroy()
    #Beginning of the game code for mainmenu link    
    def Begin():

pygame.init()
#display size
display_width = 640
display_height = 750

gameDisplay = pygame.display.set_mode((display_width,display_height))

pygame.display.set_caption ("Spellmaster")
#Images for wizard sprite
wizard_right = [pygame.image.load('right_1.png'), pygame.image.load('right_3.png'), pygame.image.load('right_2.png'), pygame.image.load('right_4.png'), pygame.image.load('right_5.png')]
wizard_left = [pygame.image.load('left_1.png'), pygame.image.load('left_3.png'), pygame.image.load('left_2.png'), pygame.image.load('left_4.png'), pygame.image.load('left_5.png')]
still = pygame.image.load("wizardsprite.png")
background = pygame.image.load("background.png")
clock = pygame.time.Clock()

score = 0

class player(object):
    def __init__ (self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 4
        self.steps= 0
        self.left = False
        self.right = False
        self.hitbox = (self.x + 20, self.y, 50, 60)

    #blit the character on screen
    def draw(self,gameDisplay):
        if self.steps + 1 >=27:
            self.steps = 0

        if self.left:
            gameDisplay.blit(wizard_left [self.steps//20], (self.x,self.y))
            self.steps +=1

        elif self.right:
            gameDisplay.blit(wizard_right [self.steps//20], (self.x,self.y))
            self.steps +=1

        else:
            gameDisplay.blit(still, (self.x,self.y))
            self.steps=0
        self.hitbox = (self.x + 20, self.y, 50, 60)
        #pygame.draw.rect(gameDisplay, (255,0,0),self.hitbox,2)

#FIRESPELL

class projectile(object):
    def __init__(self,x,y,radius,color,facing):
        self.x = x
        self.y = 662
        self.radius = radius
        self.color = color

        self.facing = 0
        self.vel = 10


    def draw(self,gameDisplay):
        pygame.draw.circle(gameDisplay, self.color, (self.x,self.y), self.radius)

#ENEMY CLASS
class enemy(object):
        redspider= [pygame.image.load('redspider_right.png'), pygame.image.load('redspider_left.png'), pygame.image.load('redspider_right.png'), pygame.image.load('redspider_left.png')]

        def __init__(self,x,y,width,height,end):
                self.x=x
                self.y=y
                self.width = width
                self.height = height
                self.end= end
                self.vel = 1.4
                self.walkCount = 0
                self.path = [self.y, self.end]
                self.hitbox = (self.x + 20, self.y, 70, 60)
                self.health = 0.2
                self.visible = True


        def draw(self,gameDisplay):
            self.move()
            if self.visible:
                if self.walkCount + 1 >= 27:
                    self.walkCount = 0
                else:
                    gameDisplay.blit(self.redspider[self.walkCount //2], (self.x, self.y))
                    self.walkCount += 0
                self.hitbox = (self.x + 2, self.y, 70, 60)
                #pygame.draw.rect(gameDisplay, (255,0,0), self.hitbox,2)


        def move(self):
            if self.vel > 0:
                if self.y + self.vel < self.path[1]:
                    self.y += self.vel


        def collision(self):
            if self.health > 0:
                self.health-= 1
            else:
                self.visible= False
            print("HIT")





def redraw():
    gameDisplay.blit(background, (0,0))
    text = font.render('SCORE: ' + str(score), 1,(255,0,0))
    gameDisplay.blit(text, (500,13))
    wizard.draw(gameDisplay)
    redspider.draw(gameDisplay)
    for bullet in bullets:
        bullet.draw(gameDisplay)


    pygame.display.update()


#MAIN LOOP

pygame.font.init()
font = pygame.font.SysFont('Engravers MT', 30)
wizard=player(300, 662, 80, 84)
redspider=enemy(360,80,80,84,650)
spides = []
shootControl = 0
bullets =[]
crashed = False
while not crashed:
    clock.tick(120)


    if shootControl >0:
        shootControl+=1
    if shootControl > 3:
        shootControl=0

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


    for bullet in bullets:
        if bullet.y - bullet.radius < redspider.hitbox[1] + redspider.hitbox[3] and bullet.y + bullet.radius > redspider.hitbox[1]:
            if bullet.x + bullet.radius > redspider.hitbox[0] and bullet.x - bullet.radius < redspider.hitbox[0] + redspider.hitbox[2]:
                redspider.collision()
                score += 1
                bullets.pop(bullets.index(bullet))

        if bullet.x < 500 and bullet.x > 0:
            bullet.y -= bullet.vel
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a] and wizard.x > wizard.vel:
            #if event.key == pygame.K_a and wizard.x > wizard.vel:
                wizard.x -= wizard.vel
                wizard.left= True
                wizard.right= False

    elif keys [pygame.K_d] and wizard.x < 560:
                wizard.x += wizard.vel
                wizard.right= True
                wizard.left=False

    if keys [pygame.K_SPACE] and shootControl ==0:
            if wizard.left:
                facing = 0
            else:
                facing = 0

            if len(bullets) < 500:
               bullets.append(projectile(round(wizard.x + wizard.width //2), round(wizard.y + wizard.height//2), 5, (255,0,0), facing))

            shootControl=1



    if event.type == pygame.KEYUP:
            if event.key == pygame.K_a or event.key == pygame.K_d:
                wizard.left= False
                wizard.right=False




    redraw()

pygame.quit()
shutdown

1 个答案:

答案 0 :(得分:1)

要在屏幕周围的随机位置产生敌人,这非常简单。有两种可能的方式:

首先,您可以修改敌人 init ()以使self.x和self.y成为randint,如下所示。

self.x = random.randint(0, display_width)
self.y = random.randint(0, display_height)

此方法有效,但限制了您将敌人放置在特定位置的能力,并且可以放置在任何地方,并且可以将敌人放置在您不想要的地方。 第二种方式基本上是第一种方式,但它是在定义新敌人时编写的

redspider = enemy(random.randint(0, screen_width), random.randint(0, screen_height), 80, 84, 650)

这也有效,但在手动定义每个新敌人时需要更多时间。但是,这允许您在以后使用时仍将敌人放置在某些位置。这两个都有缺陷,因为它们是随机放置的,但你可以创建函数来限制放置它的位置。

至于你在列表中附加敌人,你可以这样做:

enemies = []
maxenemies = 10
for i in range(maxenemies):
    enemies.append(enemy(random.randint(0, screen_width), random.randint(0, screen_height), 80, 84, 650))

这会将 maxenemies 数量的敌人推入敌人列表中,可以在以后绘制和使用。 maxenemies 只是可选的,但我会建议为可读性而做。 告诉我这些建议是否适合你。