简体   繁体   English

如何使Sprite出现在窗口中?

[英]How do I make my Sprite appear in my window?

I have made an enemy sprite within my game, however it is not displayed on the screen. 我在游戏中制作了一个敌人精灵,但是它没有显示在屏幕上。 I am wondering how I would be able to make it appear? 我想知道如何显示它?

Tried updating the pygame display in the class but it still won't show the image. 尝试更新该类中的pygame显示,但仍不会显示图像。 I plan on making the sprite appear in order to allow it to move to the end of the grid line (horizontally), then shift down vertically when it reaches the end of a horizontal point. 我计划使精灵显示,以使其(水平)移动到网格线的末端,然后在到达水平点的末端时垂直向下移动。

import pygame
pygame.init()

window = pygame.display.set_mode((650, 630))

pygame.display.set_caption("PeaShooters")

avatar = pygame.image.load('Sprite 1 Red.png')
background = pygame.image.load('Bg.jpg')
white = (255, 255, 255)

class player(object):
    def __init__(self, x, y, width, height, shots):
        self.x = 300
        self.y = 500
        self.width = 40
        self.height = 60
        self.vel = shots



def drawGrid():
    window.blit(background, (0,0))
    window.blit(avatar, (av.x, av.y))
    pygame.draw.line(window, white, [50,50], [50, 600], 5)
    pygame.draw.line(window, white, [50,50], [600, 50], 5)
    pygame.draw.line(window, white, [600,600], [600, 50], 5)
    pygame.draw.line(window, white, [50,600], [600, 600], 5)
    pygame.draw.line(window, white, [50,450], [600, 450], 5)
    for bullet in bullets:
        bullet.draw(window)
    pygame.display.update()


class shots(object):
    def __init__(self, x, y, radius, colour):
        self.x = x
        self.y = y
        self.radius = radius
        self.colour = colour
        self.vel = shots

    def draw(self, window):
        pygame.draw.circle(window, self.colour, (self.x,self.y), self.radius)

class enemy(object):
    enemy = pygame.image.load('Sprite 3 Red.png')
    pygame.display.update()

    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self. x, self.end]
        self.vel = 4

    def draw(self,window):
        self.move()
        pass

    def move(self):
        pass

av = player(300, 500, 40, 60, 9)
bullets = []
running = True
while running:
    pygame.time.delay(100) 

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    for bullet in bullets:
        if bullet.y < 600 and bullet.y > 70:
            bullet.y -= 8
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_w] and av.y > 440:
        av.y -= av.vel

    if keys[pygame.K_a] and av.x > 65:
        av.x -= av.vel

    if keys[pygame.K_s] and av.y < 535:
        av.y += av.vel

    if keys[pygame.K_d] and av.x < 530:
        av.x += av.vel

    if keys[pygame.K_SPACE]:
        if len(bullets) < 5:
            bullets.append(shots(round(av.x + av.width//2), round(av.y + av.height//2), 6, (0,0,0)))


    drawGrid()

window.blit(avatar, (x,y))

pygame.quit()

I expect the avatar to appear on the screen so I can allow it to move. 我希望该头像会出现在屏幕上,以便我可以移动它。

All drawing should be done in the while loop. 所有绘制都应在while循环中完成。 Or you call directly the window.blit function on the various sprites, or you wrap it in a sprite method which is called during the loop (I see you have a draw() method but right now it does nothing). 或者直接在各种Sprite上调用window.blit函数,或者将其包装在Sprite方法中,该方法在循环期间会被调用(我看到您有一个draw()方法,但现在它什么都不做)。 Choose one way and follow that. 选择一种方法并遵循。 I personally prefer the second one, it's more object oriented, but it is my opinion. 我个人更喜欢第二个,它更面向对象,但这是我的观点。
And remember to clear the previous position if you want to give a sense of motion. 如果要给人一种运动感,请记住要清除先前的位置。
display.update() should be called once per iteration, usually at the end, after all the sprites have been blit. display.update()应该在每次迭代之后一次调用一次,通常在最后一次,所有sprites都被blit之后。

Your enemy does not appear because you never blit it's image. 您的敌人没有出现,因为您从未消灭过它的形象。 Actually, you never create an enemy, you have defined the class only. 实际上,您永远不会创建敌人,只定义了类。 So first create an enemy adding a line such that en = enemy(ex, ey, w, h, end) . 因此,首先创建一个敌人并添加一条线,使en = enemy(ex, ey, w, h, end) ex and ey here should be the position of the enemy on the screen, choose what you prefer. exey这里应该是敌人在屏幕上的位置,选择您喜欢的东西。 w and h the width and height of the image. wh图像的宽度和高度。 end I do not know (your class, you added it, you should know what is its purpose). end我不知道(您的课程,您添加了它,您应该知道它的用途是什么)。 You can add this line just after you create the avatar . 您可以在创建avatar后添加此行。
Then remove the display.update() from the class, and add a call window.blit(en, (en.x, en.y)) in the while loop. 然后从类中删除display.update() ,并在while循环中添加一个调用window.blit(en, (en.x, en.y)) Or in drawGrid() , which actually is drawing everithing and not the grid only. 或在drawGrid() ,它实际上是在绘制图形,而不仅仅是在绘制网格。

To move the enemy, you should edit its position ( en.x and en.y ) in the while loop as you do for the avatar, but not in response to a key press. 要移动敌人,您应该像使用化身一样在while循环中编辑敌人的位置( en.xen.y ),但不要响应按键。 Just set some sort of path. 只需设置某种路径即可。 At each iteration, the enemy coordinates should change to follow this path, something like en.x += en.vel or whatever. 在每次迭代中,敌人的坐标都应更改以遵循此路径,例如en.x += en.vel或类似内容。 The real path may be more complex, but this is up to you. 实际路径可能更复杂,但这取决于您。 The logic is: change the enemy coordinate each iteration of the loop. 逻辑是:在循环的每次迭代中更改敌人坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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