【发布时间】: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更改为硬编码值,而不是选择随机图像/位置。如果它有效,那么问题在于设置随机值。如果不起作用,则说明游戏循环逻辑有问题。混乱的格式使得无法破译游戏循环逻辑以查看是否存在明显问题。