pygame运行窗户时黑色
我是Python新手,只是按照视频创建一个游戏,我做的一切都是正确的,但游戏窗口/屏幕又变黑了。
我期待游戏能够以正确的方式运行,但是,我似乎做错了一些事情,因为它不适合我。
我能够在 [i] 代码之前玩游戏,我可以返回并玩它,但我想要有多个敌人,一旦我关闭程序,我就能很快看到它,黑屏显示游戏,但随后它关闭。
import pygame
import random
import math
# initialize the pygame
pygame.init()
# create the screen
screen = pygame.display.set_mode((800, 600))
# enemy to many anemy multiplicar los enemigos
enemyimg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6
for i in range(num_of_enemies):
# Player(enemy)
enemyimg.append(pygame.image.load("enemy.png"))
enemyX.append(random.randint(0, 735))
enemyY.append(random.randint(50, 150))
enemyX_change.append(0.2)
enemyY_change.append(40)
# (bullet)
# ready_state means you cant see the bullet before firing
# fire - the bullet is currently moving
bulletimg = pygame.image.load("bullet.png")
bulletX = 0
bulletY = 480
bulletX_change = 0.2
bulletY_change = 1
bullet_state = ("ready")
score = 0
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletimg, (x + 16, y + 10))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
if distance < 27:
return True
else:
return False
# enemy movement
for i in range(num_of_enemies):
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 0.3
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 745:
enemyX_change[i] = -0.3
enemyY[i] += enemyY_change[i]
# Collission
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
bulletY = 480
bullet_state = "ready"
score += 1
print(score)
enemyX[i] = random.randint(0, 735)
enemyY[i] = random.randint(50, 150)
enemy(enemyX[i], enemyY[i], i)
# bullet movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
pygame.display.update()
I'm new to python just following a video to create a game im doing everything right but the game windows/screen comes back black.
I'm expecting the game to function the right way but, it seems that I'm doing something wrong because it's not working for me.
i was able to play the game before the [i] codes i can go back and play it but i want to have multiple enemy, im able to see it real quick once i close the program the black sceen shows the games but then it closes.
import pygame
import random
import math
# initialize the pygame
pygame.init()
# create the screen
screen = pygame.display.set_mode((800, 600))
# enemy to many anemy multiplicar los enemigos
enemyimg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6
for i in range(num_of_enemies):
# Player(enemy)
enemyimg.append(pygame.image.load("enemy.png"))
enemyX.append(random.randint(0, 735))
enemyY.append(random.randint(50, 150))
enemyX_change.append(0.2)
enemyY_change.append(40)
# (bullet)
# ready_state means you cant see the bullet before firing
# fire - the bullet is currently moving
bulletimg = pygame.image.load("bullet.png")
bulletX = 0
bulletY = 480
bulletX_change = 0.2
bulletY_change = 1
bullet_state = ("ready")
score = 0
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletimg, (x + 16, y + 10))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
if distance < 27:
return True
else:
return False
# enemy movement
for i in range(num_of_enemies):
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 0.3
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 745:
enemyX_change[i] = -0.3
enemyY[i] += enemyY_change[i]
# Collission
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
bulletY = 480
bullet_state = "ready"
score += 1
print(score)
enemyX[i] = random.randint(0, 735)
enemyY[i] = random.randint(50, 150)
enemy(enemyX[i], enemyY[i], i)
# bullet movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
pygame.display.update()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该在代码中添加一个 while 循环,否则它只会运行一次。据我所知,人们通常这样做:
当然,再加上在此 for 循环之前您拥有的所有内容,您可以了解有关游戏循环的更多信息 此处
I think you should add a while loop to your code, otherwise it will run just once. From what I can see, people usually do it like this:
plus of course everything that you had before this for loop you can learn more about game loops here