gpt4 book ai didi

python - 每次我摧毁一个但不断获得时,也尝试添加额外的敌人。如果 enemy_y[i] > 400 : IndexError: list index out of range. 在我的 python 代码中

转载 作者:行者123 更新时间:2023-12-04 17:17:01 25 4
gpt4 key购买 nike

我一直在尝试添加 num_of_enemies += 1,并且我在不同的地方尝试过,但我一直收到错误消息:如果 enemy_y[i] > 400:IndexError:列表索引超出范围。请帮助这是我的代码!

import pygame
import random
import math
from pygame import mixer

# Inialize the pygame
pygame.init()

# create the screen
screen = pygame.display.set_mode((800, 600))
running = True

# Backgorund
background = pygame.image.load('background.png')


# Background sound
mixer.music.load('background.wav')
mixer.music.play(-1)

# Title
pygame.display.set_caption("SpaceVader")

# Player
player_img = pygame.image.load('spaceship-2.png')
player_x = 370
player_y = 480
player_x_change = 0
player_y_change = 0

# Enemy
# The [] square brackets creates an empty list for the enemys to go inside
enemy_img = []
enemy_x = []
enemy_y = []
enemy_x_change = []
enemy_y_change = []
num_of_enemies = 6
for i in range(num_of_enemies):
enemy_img.append(pygame.image.load('ufo-2.png'))
enemy_x.append(random.randint(0, 735))
enemy_y.append(random.randint(50, 150))
enemy_x_change.append(2)
enemy_y_change.append(40)

# Bullet

# ready - You cant' see the bullet on the screen
# Fire - the bullet is fired
bullet_img = pygame.image.load('bullet.png')
bullet_x = 0
b_y = player_y
bullet_y = b_y
bullet_x_change = 0
bullet_y_change = 10
bullet_state = "ready"

# Font

score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
text_x = 10
text_y = 10

# Game Over Text

over_font = pygame.font.Font('freesansbold.ttf', 64)

# creates the score board and puts it on the screen
def show_score(x, y):
score = font.render("Score : " + str(score_value), True, (255, 255, 255))
screen.blit(score, (x, y))

# creates the game over message
def game_over_text():
over_text = over_font.render("GAME OVER", True, (255, 255, 255))
screen.blit(over_text, (200, 250))

#puts the player on the screen
def player(x, y):
screen.blit(player_img, (x, y))

#puts the enemy on the screen
def enemy(x, y, i):
screen.blit(enemy_img[i], (x, y))

# creates the ability to fire the buller
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bullet_img, (x + 16, y + 10))

# creates the collision between the bullet and the enemy
def is_collision(enemy_x, enemy_y, bullet_x, bullet_y,):
distance = math.sqrt((math.pow(enemy_x - bullet_x, 2)) + (math.pow(enemy_y - bullet_y, 2)))
if distance < 27:
return True
else:
return False


# Game Loop
while running:

screen.fill((0, 0, 0))
# background image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# if key stroke is pressed check for right and left and up and down
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x_change += -5
if event.key == pygame.K_RIGHT:
player_x_change += 5
if event.key == pygame.K_UP:
player_y_change += -5
if event.key == pygame.K_DOWN:
player_y_change += 5
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_sound = mixer.Sound("laser.wav")
bullet_sound.play()
# get x of player
bullet_x = player_x
# get y of player
bullet_y = player_y
fire_bullet(bullet_x, bullet_y)

# allows the key release to stop the action
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
player_x_change = 0
player_y_change = 0

# create walls to restrict leaving screen
player_x += player_x_change

if player_x <= 0:
player_x = 0
elif player_x >= 736:
player_x = 736
if player_y <= 0:
player_y = 0
elif player_y >= 536:
player_y = 536

# enemy movment
for i in range(num_of_enemies):

# Game Over

if enemy_y[i] > 400:
for j in range(num_of_enemies):
enemy_y[j] = 2000
game_over_text()
explosion_sound = mixer.Sound("explosion.wav")
explosion_sound.play()
scream_sound = mixer.Sound("Game scream 2.wav")
scream_sound.play()
break

#creates movment of enemy
enemy_x[i] += enemy_x_change[i]
if enemy_x[i] <= 0:
enemy_x_change[i] = 2
enemy_x[i] += enemy_x_change[i]
enemy_y[i] += enemy_y_change[i]
elif enemy_x[i] >= 736:
enemy_x_change[i] = -2
enemy_x[i] += enemy_x_change[i]
enemy_y[i] += enemy_y_change[i]

# Collision
collision = is_collision(enemy_x[i], enemy_y[i], bullet_x, bullet_y)
if collision:
explosion_sound = mixer.Sound("explosion.wav")
explosion_sound.play()
bullet_y = player_y
bullet_state = "ready"
score_value += 1
enemy_x[i] = random.randint(0, 735)
enemy_y[i] = random.randint(50, 150)



enemy(enemy_x[i], enemy_y[i], i)


# Bullet Movement
if bullet_y <= 0:
bullet_y = player_y
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bullet_x, bullet_y)
bullet_y -= bullet_y_change


player_y += player_y_change
player(player_x, player_y)
show_score(text_x, text_y)
pygame.display.update()

这是我的第一篇文章,我是学校项目的编码新手,如有任何帮助,我们将不胜感激!

最佳答案

随着敌人数量的增加,您还需要向列表中添加新项目:

if collision:
# [...]

num_of_enemies += 1
enemy_img.append(pygame.image.load('ufo-2.png'))
enemy_x.append(random.randint(0, 735))
enemy_y.append(random.randint(50, 150))
enemy_x_change.append(2)
enemy_y_change.append(40)

关于python - 每次我摧毁一个但不断获得时,也尝试添加额外的敌人。如果 enemy_y[i] > 400 : IndexError: list index out of range. 在我的 python 代码中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68489675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com