简体   繁体   English

如何使图像留在pygame的屏幕上?

[英]How to make image stay on screen in pygame?

I am a beginner我是初学者

import pygame as py
from math import sqrt, pow

nuclear = py.transform.scale(py.image.load('nuclear.png'),(300, 300))
def collosion(enemyX,enemyY,fireX,fireY):
    distance = sqrt(pow(enemyX - fireX, 2) + pow(fireY - enemyY, 2))
    if distance <= 100:
        return True
    else:
        return False

collosion1 = collosion(enemy1_x,enemy2_y,shoot1X + 50, shoot1Y + 60)
    if collosion1:
        window.blit(nuclear,(enemy1_x,enemy2_y))
        enemy1_x = 1400
        enemy2_y = 530
        shoot1X = tankX
        shoot1Y = tankY
        shoot_now = 'not fired'
        shoot1 = py.transform.scale(py.image.load('shoot1.png'), (70, 30))

how I make 'nuclear' image to stay for some time(At least 2 seconds).我如何使“核”图像停留一段时间(至少 2 秒)。


Thanking you for helping谢谢你的帮助

This is how you can display a text.这是您可以显示文本的方式。 It's not exactly what you want but may help:这不完全是您想要的,但可能会有所帮助:

BASICFONT = pygame.font.Font('freesansbold.ttf', 16)
WHITE = (255, 255, 255)

instructionSurf = BASICFONT.render('Arrows to move. Hold shift to run.', True, WHITE)
instructionRect = instructionSurf.get_rect()
instructionRect.bottomleft = (10, WINDOWHEIGHT - 10)

display.blit(instrutcionSurf, instructionRect)

Pygame needs to run in a loop. Pygame 需要循环运行。 And you need to blit this image in every iteration of this loop.并且您需要在此循环的每次迭代中对该图像进行 blit。 There you can also process the keystrokes and other events.您还可以在那里处理击键和其他事件。 In each iteration of the loop you also need to update the display.在循环的每次迭代中,您还需要更新显示。 So in essence this loop represents your Framerate.所以本质上这个循环代表你的帧率。

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((100, 100))

nuclear = pygame.transform.scale(pygame.image.load('nuclear.png'),(300, 300))

while True:
    for event in pygame.event.get(): # iterate through all events
        if event.type == pygame.QUIT: sys.exit()

        elif event.type == pygame.KEYDOWN: # example event KEYDOWN
            if event.key == pygame.K_a: # if key A was pressed
                print("Key 'A' was pressed")

    screen.blit(nuclear,(1,1)) # Your image needs to be blitet every frame
    pygame.display.flip() # then the screen needs to be updated

And if you want to get rid of the image, then you can just stop bliting the image.如果你想摆脱图像,那么你可以停止对图像进行闪烁。 You can look at the documentation for more details and tutorials.您可以查看文档以获取更多详细信息和教程。 https://www.pygame.org/docs/tut/MakeGames.html https://www.pygame.org/docs/tut/MakeGames.html

how I make 'nuclear' image to stay for some time(At least 2 seconds).我如何使“核”图像停留一段时间(至少 2 秒)。

You have to draw the image in the main application loop.您必须在主应用程序循环中绘制图像。 Usepygame.time.get_ticks() to get the number of milliseconds since pygame.init() was called.使用pygame.time.get_ticks()获得毫秒自号pygame.init()被调用。 When the bullet collides with the enemy, then calculate the point in time until the image has to be displayed.当子弹与敌人碰撞时,然后计算时间点,直到必须显示图像。 Display the image as long the current time is smaller than the calculated point of time:只要当前时间小于计算出的时间点就显示图像:

show_nuclear_until = 0
nuclear_pos = (0, 0)

while run:
    current_time = pygame.time.get_ticks()

    # [...]

    collosion1 = collosion(enemy1_x, enemy2_y, shoot1X + 50, shoot1Y + 60)
    if collosion1:
        nuclear_pos = (enemy1_x, enemy2_y)
        show_nuclear_until = current_time + 2000 # 2000 milliseconds = 2 seconds
        # [...]

    # [...]

    # clear display
    # [...]

    # draw
    # [...]

    if current_time < show_nuclear_until:
        window.blit(nuclear, nuclear_pos)

    pygame.display.flip()

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

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