我如何在游戏中随机拍摄敌人?

发布于 2025-01-31 06:13:16 字数 7476 浏览 4 评论 0原文

因此,我目前正在尝试创建太空入侵者,并且我正在尝试随机地向玩家拍摄外星人。我已经做到了,以便向玩家射击一个弹丸或敌人的错误,但是我不知道如何从外星人所在的随机位置进行这种情况,以及我如何使敌人反复射击导弹,而不是像现在这样做一次。我正在使用的代码下方(与敌人射击有关的变量是敌人大火和敌人)。...

import pygame
import sys
import time

#initialize pygame
pygame.init()
width = 800
height = 600
# set the size for the surface (screen)

screen = pygame.display.set_mode((width, height),pygame.FULLSCREEN)
width = screen.get_width()
height = screen.get_height()
print(width)
print(height)
# set the caption for the screen
pygame.display.set_caption("Space Invaders")




def checkCollision(missles, type, score):
    for missle in missles:
      collision = missle.collidelist((type))
      if collision > -1:
        type.pop(collision) 
        missles.remove(missle)
      missle.move_ip(0,missleSpeed)
      pygame.draw.rect(screen, WHITE, missle,0)

# define colours  you will be using
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
YELLOW = (255,255,0)



clock = pygame.time.Clock()
FPS = 60
s = 25
#load and scale images
smallInvaderImg = pygame.image.load("images/smallinvader.png")
smallInvaderImg = pygame.transform.scale(smallInvaderImg,(s,s))
medInvaderImg = pygame.image.load("images/crabinvader.png")
medInvaderImg = pygame.transform.scale(medInvaderImg, (s,s))
bigInvaderImg = pygame.image.load("images/biginvader.png")
bigInvaderImg = pygame.transform.scale(bigInvaderImg, (s,s))
shipImg = pygame.image.load("images/ship.png")
shipImg = pygame.transform.scale(shipImg, (60,60))

smallInvaders = []
medInvaders = []
bigInvaders = []
enemiesMap = ["sssssssssss",
             "mmmmmmmmmmm",
             "mmmmmmmmmmm",
           "bbbbbbbbbbb"]
invadertype = [smallInvaders,medInvaders,bigInvaders]
dx = 1
dy = 0
x = 240
y = 0
gap = 10
for element in enemiesMap:
  for char in element:
    if char == "s":
      smallInvaders.append(pygame.Rect(x,y,s,s))
    elif char == "m":
      medInvaders.append(pygame.Rect(x,y,s,s))
    elif char == "b":
      bigInvaders.append(pygame.Rect(x,y,s,s))
    x += s + gap 
  y += s + gap  
  x = 240

score = 2


ship = pygame.Rect(width/2,525,60,60)

if ship.right == width:   
  ship.right = width

#missles
maxMissles = 3
missleSpeed = -6
missleWidth = 5
missleHeight = 30
enemmissleWidth = 5
enemmissleHeight = 25
missles = []
missleFired = False
lives = 3

playbutton = pygame.Rect(width/2,height/2,155,90)
playbutton.center = (width/2,height/2)
quitbutton = pygame.Rect(width/2,height/2,155,90)
quitbutton.center = (width/2,height/2+110)
playagn = pygame.Rect(width/2,height/2,155,90)
playagn.center = (width/2,height/2)

playword = pygame.font.SysFont("comicsanms", 35)
title = pygame.font.SysFont("comicsanms", 90)
quitword = pygame.font.SysFont("comicsanms",35)
endscreen = pygame.font.SysFont("comicsanms", 90)
playagaintext = pygame.font.SysFont("comicsanms", 35)

enemMissleFire = False
enemmislist = []
enemymissles = (pygame.Rect(ship.centerx,y,enemmissleWidth,enemmissleHeight))

invaderDirSwapped = False
screenControl = 0

main = True
while main:
  clock.tick(60)
  for event in pygame.event.get():
     if event.type == pygame.QUIT:
      main = False


  for element in smallInvaders:
    element.move_ip(dx,dy)
    
  for element in smallInvaders:    
    if element.right >= width or element.left <= 0:
      dx *= -1
      invaderDirSwapped = True
     

  for element in medInvaders:
    element.move_ip(dx,dy)

  if not invaderDirSwapped:  
    for element in medInvaders:
      if element.right >= width or element.left <= 0:
        dx *= -1
        invaderDirSwapped = True
     

  for element in bigInvaders:
    element.move_ip(dx,0)

  if not invaderDirSwapped:  
    for element in bigInvaders:
      if element.right >= width or element.left <=0:
        dx *= -1
        invaderDirSwapped = True
      
  


  key_input = pygame.key.get_pressed()
  if key_input[pygame.K_RIGHT] and ship.right < width:
    ship.move_ip(4,0)
  if key_input[pygame.K_LEFT] and ship.left > 0:
    ship.move_ip(-4,0)
  if key_input[pygame.K_SPACE] and not missleFired:
    missleFired = True
    missles.append(pygame.Rect(ship.centerx,ship.top,missleWidth,missleHeight))
  
  if screenControl == 0:
    screen.fill(BLACK)
    
    texttitle = title.render("SPACE INVADERS", True, WHITE)
    textrect = texttitle.get_rect()
    textrect.center = (width/2, 100)
    screen.blit(texttitle,textrect)

    
    if playbutton.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
      screenControl = 1
    pygame.draw.rect(screen,WHITE,(playbutton), 0)

    if playbutton.collidepoint(pygame.mouse.get_pos()):
      pygame.draw.rect(screen,BLUE, (playbutton), 4)

    if quitbutton.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
      main = False
    pygame.draw.rect(screen,WHITE,(quitbutton), 0)

    if quitbutton.collidepoint(pygame.mouse.get_pos()):
      pygame.draw.rect(screen,BLUE,quitbutton,4)

    textplay = playword.render("PLAY", True, BLUE)
    textrect2 = textplay.get_rect()
    textrect2.center = (width/2,height/2)
    screen.blit(textplay,textrect2)

    textquit = quitword.render("QUIT",True,BLUE)
    textrect3 = textquit.get_rect()
    textrect3.center = (width/2,height/2+110)
    screen.blit(textquit,textrect3)
  
                           
  if screenControl == 1:
    screen.fill(BLACK)

    if len(missles) > 0:
      if missleFired and missles[-1].bottom < (ship.top - 120) and not key_input[pygame.K_SPACE]:
        missleFired = False
    if len(missles) == 0:
      missleFired = False 
   
    
    if enemMissleFire:
      enemymissles.colliderect(ship)
      enemymissles.move_ip(0,-missleSpeed)
      pygame.draw.rect(screen, WHITE, enemymissles,0)
      
      
    for invader in smallInvaders:
      screen.blit(smallInvaderImg, invader)
    for invader in medInvaders:
      screen.blit(medInvaderImg, invader)
    for invader in bigInvaders:
      screen.blit(bigInvaderImg, invader)
    screen.blit(shipImg,ship)


  
  
  #move and draw missles 

  
  checkCollision(missles,smallInvaders,score)
  checkCollision(missles,medInvaders,score)
  checkCollision(missles,bigInvaders,score)

  if smallInvaders == [] and medInvaders == [] and bigInvaders == []:
    screenControl = 2

  if screenControl == 2:
    screen.fill(BLACK)

    if playagn.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
      screenControl = 0
    pygame.draw.rect(screen,WHITE,(playagn),0)

    if quitbutton.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
     main = False
    pygame.draw.rect(screen,WHITE, (quitbutton),0)

    if playagn.collidepoint(pygame.mouse.get_pos()):
      pygame.draw.rect(screen,BLUE,(playagn), 4)

    if quitbutton.collidepoint(pygame.mouse.get_pos()):
      pygame.draw.rect(screen,BLUE,(quitbutton),4)
    
    textend = endscreen.render("YOU WON!", True, WHITE)
    textrect4 = textend.get_rect()
    textrect4.center = (width/2, 150)
    screen.blit(textend,textrect4)

    textplayagn = playagaintext.render("PLAY AGAIN", True, BLUE)
    textrect5 = textplayagn.get_rect()
    textrect5.center = (width/2,height/2)
    screen.blit(textplayagn,textrect5)

    textquit = quitword.render("QUIT",True,BLUE)
    textrect3 = textquit.get_rect()
    textrect3.center = (width/2,height/2+110)
    screen.blit(textquit,textrect3)


  pygame.display.update()

不管怎么说,我无法弄清楚我如何制造它,以便可以从随机外星人中拍摄弹丸在球员拍摄过程中,任何帮助都会被施加。

So I'm trying to create space Invaders currently and i'm trying to make the aliens shoot at the player randomnly. I've made it so that one projectile or enemy missle is shot at the player, but I can't figure out how I can make this happen from random places where the aliens are located, and how I can make the enemy repeatedly shoot missles,as opposed to just once as it's doing right now. My code that i'm using is down below (variables related to the shooting of the enemy are enemMissleFire and enemymissles)....

import pygame
import sys
import time

#initialize pygame
pygame.init()
width = 800
height = 600
# set the size for the surface (screen)

screen = pygame.display.set_mode((width, height),pygame.FULLSCREEN)
width = screen.get_width()
height = screen.get_height()
print(width)
print(height)
# set the caption for the screen
pygame.display.set_caption("Space Invaders")




def checkCollision(missles, type, score):
    for missle in missles:
      collision = missle.collidelist((type))
      if collision > -1:
        type.pop(collision) 
        missles.remove(missle)
      missle.move_ip(0,missleSpeed)
      pygame.draw.rect(screen, WHITE, missle,0)

# define colours  you will be using
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
YELLOW = (255,255,0)



clock = pygame.time.Clock()
FPS = 60
s = 25
#load and scale images
smallInvaderImg = pygame.image.load("images/smallinvader.png")
smallInvaderImg = pygame.transform.scale(smallInvaderImg,(s,s))
medInvaderImg = pygame.image.load("images/crabinvader.png")
medInvaderImg = pygame.transform.scale(medInvaderImg, (s,s))
bigInvaderImg = pygame.image.load("images/biginvader.png")
bigInvaderImg = pygame.transform.scale(bigInvaderImg, (s,s))
shipImg = pygame.image.load("images/ship.png")
shipImg = pygame.transform.scale(shipImg, (60,60))

smallInvaders = []
medInvaders = []
bigInvaders = []
enemiesMap = ["sssssssssss",
             "mmmmmmmmmmm",
             "mmmmmmmmmmm",
           "bbbbbbbbbbb"]
invadertype = [smallInvaders,medInvaders,bigInvaders]
dx = 1
dy = 0
x = 240
y = 0
gap = 10
for element in enemiesMap:
  for char in element:
    if char == "s":
      smallInvaders.append(pygame.Rect(x,y,s,s))
    elif char == "m":
      medInvaders.append(pygame.Rect(x,y,s,s))
    elif char == "b":
      bigInvaders.append(pygame.Rect(x,y,s,s))
    x += s + gap 
  y += s + gap  
  x = 240

score = 2


ship = pygame.Rect(width/2,525,60,60)

if ship.right == width:   
  ship.right = width

#missles
maxMissles = 3
missleSpeed = -6
missleWidth = 5
missleHeight = 30
enemmissleWidth = 5
enemmissleHeight = 25
missles = []
missleFired = False
lives = 3

playbutton = pygame.Rect(width/2,height/2,155,90)
playbutton.center = (width/2,height/2)
quitbutton = pygame.Rect(width/2,height/2,155,90)
quitbutton.center = (width/2,height/2+110)
playagn = pygame.Rect(width/2,height/2,155,90)
playagn.center = (width/2,height/2)

playword = pygame.font.SysFont("comicsanms", 35)
title = pygame.font.SysFont("comicsanms", 90)
quitword = pygame.font.SysFont("comicsanms",35)
endscreen = pygame.font.SysFont("comicsanms", 90)
playagaintext = pygame.font.SysFont("comicsanms", 35)

enemMissleFire = False
enemmislist = []
enemymissles = (pygame.Rect(ship.centerx,y,enemmissleWidth,enemmissleHeight))

invaderDirSwapped = False
screenControl = 0

main = True
while main:
  clock.tick(60)
  for event in pygame.event.get():
     if event.type == pygame.QUIT:
      main = False


  for element in smallInvaders:
    element.move_ip(dx,dy)
    
  for element in smallInvaders:    
    if element.right >= width or element.left <= 0:
      dx *= -1
      invaderDirSwapped = True
     

  for element in medInvaders:
    element.move_ip(dx,dy)

  if not invaderDirSwapped:  
    for element in medInvaders:
      if element.right >= width or element.left <= 0:
        dx *= -1
        invaderDirSwapped = True
     

  for element in bigInvaders:
    element.move_ip(dx,0)

  if not invaderDirSwapped:  
    for element in bigInvaders:
      if element.right >= width or element.left <=0:
        dx *= -1
        invaderDirSwapped = True
      
  


  key_input = pygame.key.get_pressed()
  if key_input[pygame.K_RIGHT] and ship.right < width:
    ship.move_ip(4,0)
  if key_input[pygame.K_LEFT] and ship.left > 0:
    ship.move_ip(-4,0)
  if key_input[pygame.K_SPACE] and not missleFired:
    missleFired = True
    missles.append(pygame.Rect(ship.centerx,ship.top,missleWidth,missleHeight))
  
  if screenControl == 0:
    screen.fill(BLACK)
    
    texttitle = title.render("SPACE INVADERS", True, WHITE)
    textrect = texttitle.get_rect()
    textrect.center = (width/2, 100)
    screen.blit(texttitle,textrect)

    
    if playbutton.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
      screenControl = 1
    pygame.draw.rect(screen,WHITE,(playbutton), 0)

    if playbutton.collidepoint(pygame.mouse.get_pos()):
      pygame.draw.rect(screen,BLUE, (playbutton), 4)

    if quitbutton.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
      main = False
    pygame.draw.rect(screen,WHITE,(quitbutton), 0)

    if quitbutton.collidepoint(pygame.mouse.get_pos()):
      pygame.draw.rect(screen,BLUE,quitbutton,4)

    textplay = playword.render("PLAY", True, BLUE)
    textrect2 = textplay.get_rect()
    textrect2.center = (width/2,height/2)
    screen.blit(textplay,textrect2)

    textquit = quitword.render("QUIT",True,BLUE)
    textrect3 = textquit.get_rect()
    textrect3.center = (width/2,height/2+110)
    screen.blit(textquit,textrect3)
  
                           
  if screenControl == 1:
    screen.fill(BLACK)

    if len(missles) > 0:
      if missleFired and missles[-1].bottom < (ship.top - 120) and not key_input[pygame.K_SPACE]:
        missleFired = False
    if len(missles) == 0:
      missleFired = False 
   
    
    if enemMissleFire:
      enemymissles.colliderect(ship)
      enemymissles.move_ip(0,-missleSpeed)
      pygame.draw.rect(screen, WHITE, enemymissles,0)
      
      
    for invader in smallInvaders:
      screen.blit(smallInvaderImg, invader)
    for invader in medInvaders:
      screen.blit(medInvaderImg, invader)
    for invader in bigInvaders:
      screen.blit(bigInvaderImg, invader)
    screen.blit(shipImg,ship)


  
  
  #move and draw missles 

  
  checkCollision(missles,smallInvaders,score)
  checkCollision(missles,medInvaders,score)
  checkCollision(missles,bigInvaders,score)

  if smallInvaders == [] and medInvaders == [] and bigInvaders == []:
    screenControl = 2

  if screenControl == 2:
    screen.fill(BLACK)

    if playagn.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
      screenControl = 0
    pygame.draw.rect(screen,WHITE,(playagn),0)

    if quitbutton.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
     main = False
    pygame.draw.rect(screen,WHITE, (quitbutton),0)

    if playagn.collidepoint(pygame.mouse.get_pos()):
      pygame.draw.rect(screen,BLUE,(playagn), 4)

    if quitbutton.collidepoint(pygame.mouse.get_pos()):
      pygame.draw.rect(screen,BLUE,(quitbutton),4)
    
    textend = endscreen.render("YOU WON!", True, WHITE)
    textrect4 = textend.get_rect()
    textrect4.center = (width/2, 150)
    screen.blit(textend,textrect4)

    textplayagn = playagaintext.render("PLAY AGAIN", True, BLUE)
    textrect5 = textplayagn.get_rect()
    textrect5.center = (width/2,height/2)
    screen.blit(textplayagn,textrect5)

    textquit = quitword.render("QUIT",True,BLUE)
    textrect3 = textquit.get_rect()
    textrect3.center = (width/2,height/2+110)
    screen.blit(textquit,textrect3)


  pygame.display.update()

Regardless, I can't figure out how I can make it so that projectiles can be shot from random aliens, over the course of the player shooting, and so any help would be appreicated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2025-02-07 06:13:16

导入随机和时间,

这将获得内置的随机和时间函数,从而

from random import randint as ran
import time

获得数字

,这将获得上一张镜头和下一个

t = ran(Min_Time,Max_Time)

获得倒计时

之间计数第二个

time.sleep(1)
t -= 1
print(t)

之间的时间,这将在数字射击

!拍摄

if t == 0:
print('shot!')

最终代码(小号大张旗鼓)

from random import randint as ran
import time

def randshot(Min_Time,Max_Time):
    t = ran(Min_Time,Max_Time)
    print(t)
    while t:
        time.sleep(1)
        t -= 1
        print(t)
    if t == 0:
        print('shot!')
while True:
    randshot(5,10)

希望这有所帮助!

Import Random and Time

this will get the random and time functions built in

from random import randint as ran
import time

Get the Number

this will get the time between the previous shot and the next

t = ran(Min_Time,Max_Time)

Get The Countdown

this will count a second between numbers

time.sleep(1)
t -= 1
print(t)

Shot!

shoot

if t == 0:
print('shot!')

FINAL CODE (trumpet fanfare)

from random import randint as ran
import time

def randshot(Min_Time,Max_Time):
    t = ran(Min_Time,Max_Time)
    print(t)
    while t:
        time.sleep(1)
        t -= 1
        print(t)
    if t == 0:
        print('shot!')
while True:
    randshot(5,10)

Hope this Helped!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文