【问题标题】:enemy isn't apearing pygame敌人没有出现 pygame
【发布时间】:2021-05-18 14:55:48
【问题描述】:
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()

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

【问题讨论】:

  • 请将您的代码格式化为代码,而不是报价。如果人们可以复制并粘贴您的代码进行尝试,您将获得更好的答案。
  • 尝试将enemy 更改为硬编码值,而不是选择随机图像/位置。如果它有效,那么问题在于设置随机值。如果不起作用,则说明游戏循环逻辑有问题。混乱的格式使得无法破译游戏循环逻辑以查看是否存在明显问题。

标签: python pygame


【解决方案1】:

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

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找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    相关资源
    最近更新 更多