我一直在使用pygame开发游戏。到目前为止,我已经完成了拍摄工作。 这是代码:
def game_loop():
global x
global y
x=0
y=0
second_x=0
second_y=0
change_x=0
change_y=0
xlist=[]
ylist=[]
# paused=True
while True:
game_display.blit(background, (0, 0))
mouseclk=pygame.mouse.get_pressed()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
x, y = pygame.mouse.get_pos()
xlist.append(x)
ylist.append(y)
if x>=display_width-40:
x=display_width-40
if y>=display_height-48:
y=display_height-48
if event.type == pygame.MOUSEBUTTONUP :
if event.button==1:
bullets.append([x+16,y])
shoot.play()
for bullet in bullets:
game_display.blit(bulletpic, (bullet[0], bullet[1]+10))
for b in range(len(bullets)):
bullets[b][-1] -= 10
for bullet in bullets:
if bullet[1]>display_height:
bullets.remove(bullet)
mainship=Ship(spaceship, x, y, pygame.mouse.get_focused())
mainship.MakeShip()
if pygame.mouse.get_focused()==0:
pause()
pygame.display.update()
clock.tick(60)
拍摄效果很好,但我真的很想在游戏中使用课程。我尝试过制作这样的课程:
class Bullet:
def __init__(self, xpos, ypos, sprite, speed, llist):
self.xpos=xpos
self.ypos=ypos
self.sprite=sprite
self.speed=speed
self.list=llist
self.list.append([self.xpos+16,self.ypos])
for bullet in self.list:
game_display.blit(self.sprite, (bullet[0], bullet[1]+10))
for b in range(len(self.list)):
self.list[b][-1] -= self.speed
for bullet in self.list:
if bullet[1]>display_height:
self.list.remove(bullet)
现在,即使它看起来可行,也只会使子弹出现故障和缓慢,有时甚至无法正确产生子弹。
有什么办法可以使我工作?我是一个菜鸟程序员,还是新手,所以对您的帮助将不胜感激。
答案 0 :(得分:4)
我认为您的Bullet做得太多了。您可以像这样建模子弹:
class Bullet:
"""This is a bullet. It knows about its position, direction and speed.
It can move. It knows when it crosses the classvariables bounds."""
# bounds as class variable for _all_ bullets - set it to dimensions of your game field
bounds = (0,0,10,10) # if position is lower or higher that these it is out of bounds
# if you got only 1 spite for all bullets (no acid-, fire-, waterbullets) you could
# set it here. I would not, I would leave bullets simply to do the movement and tell
# me if they get out of bounds
def __init__(self, xpos, ypos, xdir, ydir, speed):
# where does the bullet start (f.e. muzzle position)
self.xpos=xpos
self.ypos=ypos
# direction that the bullet flies
self.xdir=xdir
self.ydir=ydir
# initial speed of the bullet
self.speed=speed
def moveBullet(self):
self.xpos += int(self.xdir * self.speed) # speed may be 0.8 or so
self.ypos += int(self.ydir * self.speed)
def outOfBounds(self):
minX,minY,maxX,maxY = Bullet.bounds
return self.xpos < minX or self.ypos < minY or self.xpos > maxX or self.ypos > maxY
def __repr__(self):
"""Pretty print stuff"""
return "({},{}){}".format(self.xpos,self.ypos,"" if
not self.outOfBounds()
else " : Out Of Bounds {}".format(Bullet.bounds))
您的游戏然后设置所有项目符号的边界,并跟踪项目符号列表。如果您需要新子弹,只需将其放入列表
Bullet.bounds = (0,0,90,90)
bullets = [ Bullet(10,10,12,0,3), Bullet(10,10,11,9,3), Bullet(10,10,0,5,1)]
while bullets:
# you should move this into some kind of `def moveBullets(bulletlist): ...`
# that you then call inside your game loop
for b in bullets:
b.moveBullet()
print(b) # instead draw a spirte at b.xpos, b.ypos
print("")
# return from moveBullets(..)
# remove all out of bounds bullets from list
bullets = filter(lambda b: not b.outOfBounds(),bullets)
输出:
(46,10)
(43,37)
(10,15)
(82,10)
(76,64)
(10,20)
(118,10) : Out Of Bounds (0, 0, 90, 90)
(109,91) : Out Of Bounds (0, 0, 90, 90)
(10,25)
(10,30)
(10,35)
(10,40)
(10,45)
(10,50)
(10,55)
(10,60)
(10,65)
(10,70)
(10,75)
(10,80)
(10,85)
(10,90)
(10,95) : Out Of Bounds (0, 0, 90, 90)