我想知道我是否可以根据pyinvaders.py项目获得一些帮助在太空入侵者克隆中实施奖励。我的想法是,经过一段时间后,应该出现一个玩家可以射击的精灵。在此之后,玩家精灵应该换成另一个精灵。但是,我不确定如何实现这一点,所以我希望学习如何去做一些帮助。奖金船保存为“bonus.jpg”。
到目前为止的代码在这里:
import pygame, sys
import time
from pygame import *
import random
class Sprite:
def __init__(self,xpos,ypos,filename):
self.x = xpos
self.y = ypos
self.bitmap = image.load(filename)
self.bitmap.set_colorkey((0,0,0))
def set_position(self,xpos,ypos):
self.x = xpos
self.y = ypos
def render(self):
screen.blit(self.bitmap,(self.x,self.y))
def Intersect(s1_x,s1_y,s2_x,s2_y):
"""Collision detection code for sprites of size 32x32"""
if(s1_x>s2_x-32) and (s1_x<s2_x+32) and (s1_y>s2_y-32) and (s1_y<s2_y+32):
return 1
else:
return 0
init()
black = (0, 0, 0)
screen = display.set_mode((640,480))
key.set_repeat(1,1) #rapid keyboard repeat
display.set_caption('PyInvaders')
backdrop = image.load('data/backdrop.bmp')
enemies = []
enemies_row2 = []
bgp = [0, 0]
x = 0
for count in range(10):
enemies.append(Sprite(50*x+50,50,'data/baddie.bmp'))
x+= 1
x = 0
for count in range(10):
enemies_row2.append(Sprite(50*x+50,85,'data/baddie.bmp'))
x+=1
hero = Sprite(20,400, 'data/hero.bmp')
ourmissile = Sprite(0,480,'data/heromissile.bmp')
enemymissile = Sprite(0,480,'data/baddiemissile.bmp')
enemymissile_row2 = Sprite(0,480,'data/baddiemissile.bmp')
clock = pygame.time.Clock()
done = False
score = 0
scorefont = font.Font(None,60)
enemyspeed = 3
while done == False:
screen.blit(backdrop,(0,0))
for count in range(len(enemies)):
enemies[count].x += enemyspeed
enemies[count].render()
for count in range(len(enemies_row2)):
enemies_row2[count].x += enemyspeed
enemies_row2[count].render()
if len(enemies) != 0:
if enemies[len(enemies)-1].x > 590:
enemyspeed = -3
for count in range(len(enemies)):
enemies[count].y += 5
if enemies[0].x < 10:
enemyspeed = 3
for count in range(len(enemies)):
enemies[count].y += 5
if len(enemies_row2) != 0:
if enemies_row2[len(enemies_row2)-1].x > 590:
enemyspeed = -3
for count in range(len(enemies_row2)):
enemies_row2[count].y += 5
if enemies_row2[0].x < 10:
enemyspeed = 3
for count in range(len(enemies_row2)):
enemies_row2[count].y += 5
if ourmissile.y < 479 and ourmissile.y > 0:
ourmissile.render()
ourmissile.y -= 5
if enemymissile.y >= 480 and len(enemies) > 0:
enemymissile.x = enemies[random.randint(0,len(enemies)-1)].x
enemymissile.y = enemies[0].y
if enemymissile_row2.y >= 480 and len(enemies_row2) > 0:
enemymissile_row2.x = enemies_row2[random.randint(0,len(enemies_row2)-1)].x
enemymissile_row2.y = enemies_row2[0].y
if Intersect(hero.x,hero.y,enemymissile.x,enemymissile.y):
done = True #you were hit by the baddie missile
if Intersect(hero.x,hero.y,enemymissile_row2.x,enemymissile_row2.y):
done = True #you were hit by the baddie missile
if Intersect(ourmissile.x,ourmissile.y,enemymissile.x,enemymissile.y):
(ourmissile.x,ourmissile.y) = (0,480)
(enemymissile.x, enemymissile.y) = (0,480)
if Intersect(ourmissile.x,ourmissile.y,enemymissile_row2.x,enemymissile_row2.y):
(ourmissile.x,ourmissile.y) = (0,480)
(enemymissile_row2.x, enemymissile_row2.y) = (0,480)
for count in range(0,len(enemies)):
if Intersect(ourmissile.x, ourmissile.y, enemies[count].x, enemies[count].y):
del enemies[count]
score += 50
(ourmissile.x,ourmissile.y) = (0,480)
break
for count in range(0,len(enemies_row2)):
if Intersect(ourmissile.x, ourmissile.y, enemies_row2[count].x, enemies_row2[count].y):
del enemies_row2[count]
score += 50
(ourmissile.x,ourmissile.y) = (0,480)
break
if len(enemies) == 0 and len(enemies_row2) == 0:
done = True
scoretext = scorefont.render('Score:' + str(score),True, (255,255,255),(0,0,0))
screen.blit(scoretext,(5,5))
for ourevent in event.get():
if ourevent.type == QUIT:
done = True #if someone closes out via the X in the titlebar
if ourevent.type == KEYDOWN:
if ourevent.key == K_RIGHT and hero.x < 590:
hero.x += 5
if ourevent.key == K_LEFT and hero.x > 10:
hero.x -= 5
if ourevent.key == K_SPACE:
if (ourmissile.x,ourmissile.y) == (0,480) or ourmissile.y <=0:
(ourmissile.x,ourmissile.y) = (hero.x, hero.y)
enemymissile.render()
enemymissile_row2.render()
enemymissile.y += 5
enemymissile_row2.y += 5
hero.render()
display.update()
time.delay(5)
while done == True:
screen.blit(backdrop, bgp)
text = scorefont.render("You Scored: " + str(score), True, black)
screen.blit(text, (200, 200))
pygame.display.flip()
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
答案 0 :(得分:3)
您的代码目前的设置方式,您尝试实现的目标可能非常困难。
您应该尝试以下内容:
1)我建议你为主角创建一个单独的类,为你的敌人创建一个单独的类,而不是有一个名为Sprite
的公共类。然后,您可以为每个类添加个性化方法,例如__init__
和move
以及draw
2)您应该在主角色类中创建一个变量,其中包含您可以拥有的所有不同类型船舶的图像。这样,稍后您在主循环中的代码中就可以轻松地将发货类型从ships[0]
更改为ships[1]
。这是一个小样本片段:
class Player:
def __init__(self):
self.ships = [image.load("weak_ship.png"), image.load("upgraded_ship.png"), image.load("best_ship.png")]
self.current_ship = self.ships[0]
self.fire_rate = 5
然后您可以将播放器初始化为:
player = Player()
然后在您的主循环中,您可以通过执行以下操作轻松地将您的船型更改为更好的船舶:
player.current_ship = player.ships[1]
player.fire_rate = 30 #this increases the rate of fire
以及类似的事情:
player.draw() #in your Player class you will define a draw method that takes care of the drawing
轻松地用一条干净整洁的代码行绘制你的船
3)同样,对于敌人类,你可以有类似的方法
4)我建议在你的类中继承pygame.sprite.Sprite,如下所示,然后你可以使用它的一些功能,如下所示:
您可以在pygame中创建精灵组,如link here。
所示然后在主代码中,您可以通过调用sprite组的update
方法轻松绘制组中的所有精灵。然后,此update
方法将遍历您可以定义为移动或绘制的每个实例的更新方法。所有这一切只需几行代码。
5)最后,你的“奖金”船问题:
在我之前谈到self.ships
时,您可以将另一张图片添加到名为image.load("bonus.jpg")
然后,您可以轻松地在主代码中将player.current_ship
更改为player.ships[4]
,如下所示:
player.current_ship = player.ships[4]
player.fire_rate = 100
我知道我刚刚说了很多东西,但我真的建议如上所述修改代码会让你在开发过程中添加功能非常容易:
此外,只需快速搜索谷歌搜索pygame中的一些游戏示例,以便更好地了解代码的整齐结构。虽然你最初需要付出很多努力,但从长远来看,还有更多的回报。快乐的Pygaming:)
我还强烈建议您查看此link: