gpt4 book ai didi

python - 不知道如何在我的 pygame 中乘以外星人?

转载 作者:行者123 更新时间:2023-11-30 22:52:31 25 4
gpt4 key购买 nike

所以我刚刚使用 pygame 和 python 编写了一个 dodger 游戏来练习。我读了很多教程并查看了其他类似的游戏来帮助我。基本上,外星人从屏幕顶部落下(它们的大小和速度应该是随机的,它们的位置也应该是随机的),玩家使用鼠标移动宇宙飞船以躲避坠落的外星人。

我有一个 if 语句,该语句应该使从顶部下来的外星人成倍增加,但是当我运行游戏时,只有一个外星人掉落,当它到达屏幕末尾时,它就消失了。没有其他外星人出现。程序仍在运行,所以游戏还没有结束。

这是我的完整代码。

import pygame
import random
import sys
from pygame.locals import *
alienimg = pygame.image.load('C:\\Python27\\alien.png')
playerimg = pygame.image.load('C:\\Python27\\spaceship.png')

def playerCollision(playerRect, aliens): # a function for when the player hits an alien
for a in aliens:
if playerRect.colliderect(a['rect']):
return True
return False

def screenText(text, font, screen, x, y): #text display function
textobj = font.render(text, 1, (255, 255, 255))
textrect = textobj.get_rect()
textrect.topleft = (x,y)
screen.blit(textobj, textrect)

def main(): #this is the main function that starts the game

pygame.init()

screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
pygame.display.set_caption('Dodge the Aliens')
font = pygame.font.SysFont("monospace", 45)


pressed = pygame.key.get_pressed()

aliens = []
score = 0
alienAdd = 0
addedaliens = 0
gameOver = False
moveLeft = moveRight = moveUp = moveDown = False
topScore = 0
while gameOver==False: #while loop that actually runs the game
score += 1

playerImage = pygame.image.load('C:\\Python27\\spaceship.png').convert() # the player images
playerRect = playerImage.get_rect()
screen.blit(playerImage, [300, 250])
alienImage = pygame.image.load('C:\\Python27\\alien.png').convert() #alien images
alienRect = alienImage.get_rect()
screen.blit(alienImage, [ 50, 50 ])
pygame.display.flip()


for event in pygame.event.get(): #key controls
if event.type == KEYDOWN and event.key == pygame.K_ESCAPE: #escape ends the game
gameOver = True
elif event.type == MOUSEMOTION:
playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery)
screen.fill((0,0,0))
pygame.display.flip()


if not gameOver:
alienAdd += 1
if alienAdd == 6: # randomly adding aliens of different sizes and speeds
aliendAdd = 0
alienSize = random.randint(8, 25)
newAlien = {'rect': pygame.Rect(random.randint(0, 500 - alienSize), 0 -alienSize, alienSize, alienSize),
'speed': random.randint(1, 8),
'surface':pygame.transform.scale(alienImage, (alienSize, alienSize)),
}
aliens.append(newAlien)

if moveLeft and playerRect.left > 0:
playerRect.move_ip(-1 * 5, 0)
if moveRight and playerRect.right < 500:
playerRect.move_ip(5, 0)
if moveUp and playerRect.top > 0:
playerRect.move_ip(0, -1 * 5)
if moveDown and playerRect.bottom < 500:
playerRect.move_ip(0, 5)

for a in aliens:
if not gameOver:
a['rect'].move_ip(0, a['speed'])


for a in aliens[:]:
if a['rect'].top > 500:
aliens.remove(a) #removes the aliens when they get to the bottom of the screen

screenText('Score %s' % (score), font, screen, 10, 0)
screen.blit(playerImage, playerRect)

pygame.display.update()

for a in aliens:
screen.blit(a['surface'], a['rect'])
pygame.display.flip()


if playerCollision(playerRect, aliens):
if score > topScore:
topScore = score
gameOver = True


clock.tick(30)

screenText('Game Over!', font, screen, (750 / 6), ( 750 / 6))
screenText('Press ENTER To Play Again.', font, screen, ( 750 / 6) - 80, (750 / 6) + 50)
pygame.display.update()

if __name__ == "__main__":
main()

此外,旁注,每当我将代码放在这里时,我都会向下移动每一行并缩进它。有更简单的方法吗?

最佳答案

aliendAdd = 0 中有拼写错误,应该是 alienAdd = 0。这会导致您不断增加 alienAdd ,而不会再次命中 alienAdd == 6 的 if 语句。

关于python - 不知道如何在我的 pygame 中乘以外星人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38534458/

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