How to make image stay on screen in pygame?

筅森魡賤 提交于 2021-02-02 09:28:12

问题


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).


Thanking you for helping


回答1:


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)



回答2:


Pygame needs to run in a loop. And you need to blit this image in every iteration of this loop. 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




回答3:


how I make 'nuclear' image to stay for some time(At least 2 seconds).

You have to draw the image in the main application loop. Use pygame.time.get_ticks() to get the number of milliseconds since pygame.init() was called. 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()


来源:https://stackoverflow.com/questions/63718110/how-to-make-image-stay-on-screen-in-pygame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!