我是初学程序员,我的第一语言是Python 2.7,我正在尝试制作一个太空入侵者类型的游戏,但我想要同时拥有多个子弹并且无法找到一种方法来做到这一点。所以我在这里做了我自己的方式是我的代码在你看到之后解释得更多
if event.type ==pygame.KEYDOWN and event.key==K_SPACE:
if m < 5:
m+=1
if m==1:
m1a=1
m1x=ls
if m==2:
m2a=1
m2x=ls
if m==3:
m3a=1
m3x=ls
if m==4:
m4a=1
m4x=ls
if m==5:
m5a=1
m5x=ls
print m
#missle 1
if m1a==1:
screen.blit(rship,(m1x,m1))
if m1>=0:
m1-=1
else:
m-=1
m1a=0
m1=460
#missle 2
if m2a==1:
screen.blit(rship,(m2x,m2))
if m2>=0:
m2-=1
else:
m-=1
m2a=0
m2=460
#missle 3
if m3a==1:
screen.blit(rship,(m3x,m3))
if m3>=0:
m3-=1
else:
m-=1
m3a=0
m3=460
#missle 4
if m4a==1:
screen.blit(rship,(m4x,m4))
if m4>=0:
m4-=1
else:
m-=1
m4a=0
m4=460
#missle 5
if m5a==1:
screen.blit(rship,(m5x,m5))
if m5>=0:
m5-=1
else:
m-=1
m5a=0
m5=460
我敢肯定它可笑的但是我只是在学习,但问题是第一和第二导弹很好,它的第三个以及超出它的混乱。当你发射第三个时,它将第二个移动到你射击的地方,然后如果再次击中,则代码不会再回到1,它会保持在2并且会出现更多故障。如果你需要我尝试更好地解释它,我很乐意。只是想学习。
完整代码:pastebin.com/FnPaME6N
答案 0 :(得分:1)
您应该制作“子弹”sprites,然后将其添加到名为bullets
的{{3}}或其他内容中。在组中调用update
方法将一次性更新所有项目符号。
答案 1 :(得分:0)
这是一些有效的代码,但我不会称它为好。
import pygame
import math
from pygame.locals import *
background_colour = (122, 100, 155)
(width, height) = (500, 500)
pygame.init()
#ship = pygame.image.load('lolol.jpeg')\
rship = pygame.image.load('js.jpg')
mis = pygame.image.load('lot.jpg')
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('A.N.T.O.N.I.A.')
screen.fill(background_colour)
pygame.display.flip()
running = True
MAX_MISSILES = 5
ls = 250 # Horizontal ship location
LEFT = False
RIGHT = False
def move(ls, RIGHT, LEFT):
'''Moves the ship 3 pixels to the left or right.
Only moves if just one direction key is down, and not both.
Also, will not move if the ship is at either horizontal screen edge.
ls is the ship location.
'''
if LEFT and not RIGHT:
if ls >= 10:
ls -= 3
elif RIGHT and not LEFT:
if ls <= 440:
ls += 3
return ls
def fire_missile(ls, missiles):
'''Fire a missile, as long as we are not at the maximum number.
We use a list to hold our missile locations.
'''
if len(missiles) >= MAX_MISSILES:
return
else:
missiles.append((ls, 460))
def draw_missiles(missiles):
'''Draw all the missiles.'''
if missiles:
for missile in missiles:
screen.blit(mis, (missile))
def move_missiles(missiles):
'''If there are any missiles, move them up 1 pixel, and append
them to the newlist. The new list will replace the old list.
'''
if missiles:
newmissiles = []
for missile in missiles:
# Do not append the missile to the new list if it is going
# off the screen
if missile[1] > 0:
newmissiles.append((missile[0], missile[1] - 1))
return newmissiles
else:
return missiles
missiles = []
while running:
screen.blit(rship, (ls, 450))
pygame.display.flip()
screen.fill(background_colour)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
if event.type == pygame.KEYDOWN and event.key == K_ESCAPE:
# You can now quit with the escape key.
pygame.quit()
running = False
if event.type == pygame.KEYDOWN and event.key == K_LEFT:
LEFT = True
# LEFT is True untli you let up in the LEFT key
if event.type == pygame.KEYUP and event.key == K_LEFT:
LEFT = False
if event.type == pygame.KEYDOWN and event.key == K_RIGHT:
RIGHT = True
if event.type == pygame.KEYUP and event.key == K_RIGHT:
RIGHT = False
if event.type == pygame.KEYDOWN and event.key == K_SPACE:
fire_missile(ls, missiles)
ls = move(ls, RIGHT, LEFT)
draw_missiles(missiles)
missiles = move_missiles(missiles)
任何时候你发现自己使用数字生成变量,这是代码味道,并且意味着你可能应该使用不同的数据类型。
正如已经建议的那样,你可能想要查看sprite和group模块,但这至少会做你到目前为止所做的事情。