微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

敌人不是 pygame

如何解决敌人不是 pygame

import random 
import pygame 
import time 
import os 
import keyboard

pygame.init() 
pygame.font.init() 
pygame.mixer.init() 

screen = pygame.display.set_mode((800,600))
shot = pygame.mixer.sound(os.path.join('sgb12c.mp3')) 
pygame.display.set_caption('Space Invaders') 
icons = pygame.image.load("uno.png") 
pygame.display.set_icon(icons) 
player_image = pygame.image.load("spaceship.png") 
running = True 
shot_1_img = pygame.image.load("shott.png") 
enemy1_img = pygame.image.load("f_zombie.png") 
enemy2_img = pygame.image.load("s_zombie.png") 
enemy3_img = pygame.image.load("t_zombie.png") 
enemys = (enemy2_img,enemy2_img,enemy3_img)

places_enms = [300,150,400,450,350,550,500,600,650,700] 
places_enm = random.choice(places_enms)

place_en = [150,100,50,200,250] 
place_ens = random.choice(place_en)
pri = random.choice(enemys) 
def enemy():
    screen.blit(pri,(places_enm,place_ens))

    def player(x,y):
        screen.blit(player_image,(x,y))
    
        shooting = True
    
        shot.set_volume(0.9) 

        player_image_pos_w = int(pygame.Surface.get_width(player_image)) 
        player_image_pos_h = int(pygame.Surface.get_height(player_image)) 
        while running is True:
            for event in pygame.event.get():
                mouse_ps = pygame.mouse.get_pos()
                x_m,y_m = mouse_ps
                screen.fill((0,204,204))
                u_x = x_m - 20
                u_y = 400
    
                # noinspection PyArgumentList
                player(u_x,u_y)
                enemy()
                if keyboard.is_pressed('w'):
                
                    pygame.mixer.sound.play(shot)
                
                    screen.blit(shot_1_img,(u_x,u_y))
    
                    u_y += 1
    
                    time.sleep(0.20)
                    print('bahjat')

            pygame.display.update()

我的敌人没有出现在我的屏幕上。

解决方法

使用正确的缩进,我们可以看到错误:您在 event for 循环中更新播放器:

while running:
    for event in pygame.event.get():
        ...
        player(...)

  1. 像这样缩进你的游戏循环:
while running:
    for event in pygame.event.get():
        pass # only get the events here

    # outside of the for loop,update the player
    screen.fill((0,204,204))

    player(pygame.mouse.get_pos()[0] - 20,400)
    enemy()

    pygame.display.update()

否则,如果未检测到事件,则播放器不会出现。

  1. 您还必须获取 pygame.QUIT 事件以检测用户何时关闭窗口,例如:
while running:

    for event in pygame.event.get():
        if event.type == QUIT: # if the user clicks on the X,close PyGame
            pygame.quit()
                exit()

    # next code here

有关 pygame.event 的更多信息可以在 documentation 中找到。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。