我正在尝试创造一个游戏,我的xwing射击并杀死了tiefighters。当我按空格键时,我无法将拍摄的图像显示在xwing之外。我知道之前已经问过这个问题,但我无法得到它。我是python和pygame的新手。谢谢!
import sys
import random
import pygame as pg
pg.init()
screen = pg.display.set_mode((1280, 800))
display_width = 1280
display_height= 800
black= (0,0,0)
white= (255,255,255)
red = (255,0,0)
blue_violet = (138,43,226)
bullets= []
xwingImg = pg.image.load('X-Wing.bmp').convert()
tieImg= pg.image.load('tiefighter.png').convert()
space=pg.image.load('space.jpg').convert()
xbullet = pg.image.load('bullet.png').convert()
BG_image = pg.image.load('space.jpg').convert()
def rot_center(image, angle):
"""rotate a Surface, maintaining position."""
loc = image.get_rect().center #rot_image is not defined
rot_sprite = pg.transform.rotate(image, angle)
rot_sprite.get_rect().center = loc
return rot_sprite
# or return tuple: (Surface, Rect)
# return rot_sprite, rot_sprite.get_rect()
def main():
clock = pg.time.Clock()
# Surfaces/images have a `get_rect` method which
# returns a rect with the dimensions of the image.
player_rect = xwingImg.get_rect()
player_rect.center = ( 640,400 )
change_x = 0
change_y = 0
enemies = []
spawn_counter = 30
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == space:
shot.play()
for b in range(len(bullets)) :
bullets [b] [0] -=10
for bullet in bullets:
if bullet [0] <0:
bullet.remove(bullet)
bullets.append ([event.pos[0], 500])
if event.type == pg.KEYDOWN:
if event.key == pg.K_d:
change_x = 5
if event.key == pg.K_w:
change_y = -5
if event.key == pg.K_s:
change_y= 5
if event.key == pg.K_a:
change_x = -5
if event.type == pg.KEYUP:
if event.key == pg.K_d and change_x > 0:
change_x = 0
if event.key == pg.K_a and change_x < 0:
change_x = 0
if event.key == pg.K_w and change_y<0:
change_y=0
if event.key == pg.K_s and change_y>0:
change_y=0
# Spawn enemies if counter <= 0 then reset it.
spawn_counter -= 1
if spawn_counter <= 0:
# Append an enemy rect. You can pass the position directly as an argument.
enemies.append(tieImg.get_rect(topleft=(random.randrange(1280), -800 )))
spawn_counter = 30
# Update player_rect and enemies.
player_rect.x += change_x
player_rect.y += change_y
for enemy_rect in enemies:
enemy_rect.y += 5
# Collision detection with pygame.Rect.colliderect.
if player_rect.colliderect(enemy_rect):
done = True
# Draw everything.
screen.blit(BG_image, (0,0))
for bullet in bullets:
screen.blit(xbullet, pg.Rect (bullet[0], bullet [1], 0, 0))
for enemy_rect in enemies:
screen.blit(tieImg, enemy_rect)
screen.blit(xwingImg, player_rect)
if player_rect.x >display_width:
player_rect.x = 0
if player_rect.x < 0:
player_rect.x= 1280
if player_rect.y>display_height:
player_rect.y = 0
if player_rect.y < 0:
player_rect.y= 800
pg.display.flip()
clock.tick(40)
if __name__ == '__main__':
main()
pg.quit()
sys.exit()
答案 0 :(得分:2)
我认为最好使用pygame sprites和sprite组而不是rects列表。 (第12章解释了课程是如何运作的。)要用rects拍摄东西,你需要自己做更多的事情。
首先按空格拍摄单个子弹(将它们附加到bullets
列表)。将这三行添加到事件循环中:
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
bullets.append(xbullet.get_rect(center=player_rect.midtop))
在while循环中移动项目符号:
for bullet in bullets:
bullet.y -= 4
然后把他们搞砸了:
for bullet in bullets:
screen.blit(xbullet, bullet)
现在来了困难的部分。我们必须检查哪个敌人与子弹相撞,然后过滤enemies
列表,以便它只包含幸存的敌人。
# We'll use the hit_enemies list to check which enemy has survived.
hit_enemies = []
for enemy_rect in enemies:
enemy_rect.y += 5
# Collision detection with player.
if player_rect.colliderect(enemy_rect):
done = True
# Collision detection with bullets.
for bullet in bullets:
if bullet.colliderect(enemy_rect):
hit_enemies.append(enemy_rect)
# Filter the destroyed enemies out. We create a new list and
# append the surviving enemies to it.
survivors = []
for enemy in enemies:
if enemy not in hit_enemies:
survivors.append(enemy)
enemies = survivors # Now `enemies` is the list of survivors.
# You can also do it in one line with a list comprehension.
# enemies = [enemy for enemy in enemies if enemy not in hit_enemies]
你必须为子弹移除它们。此外,如果敌人和子弹离开屏幕,则应将其过滤掉,否则列表会一直在增长。
以下是完整的示例:
import sys
import random
import pygame as pg
pg.init()
screen = pg.display.set_mode((1280, 800))
display_width = 1280
display_height = 800
xwingImg = pg.image.load('X-Wing.bmp').convert()
tieImg= pg.image.load('tiefighter.png').convert()
space=pg.image.load('space.jpg').convert()
xbullet = pg.image.load('bullet.png').convert()
BG_image = pg.image.load('space.jpg').convert()
def main():
clock = pg.time.Clock()
player_rect = xwingImg.get_rect()
player_rect.center = (640, 400)
change_x = 0
change_y = 0
enemies = []
bullets = []
spawn_counter = 30
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
bullets.append(xbullet.get_rect(center=player_rect.midtop))
if event.key == pg.K_d:
change_x = 5
if event.key == pg.K_w:
change_y = -5
if event.key == pg.K_s:
change_y= 5
if event.key == pg.K_a:
change_x = -5
if event.type == pg.KEYUP:
if event.key == pg.K_d and change_x > 0:
change_x = 0
if event.key == pg.K_a and change_x < 0:
change_x = 0
if event.key == pg.K_w and change_y<0:
change_y=0
if event.key == pg.K_s and change_y>0:
change_y=0
# Spawn enemies if counter <= 0 then reset it.
spawn_counter -= 1
if spawn_counter <= 0:
# Append an enemy rect. You can pass the position directly as an argument.
enemies.append(tieImg.get_rect(topleft=(random.randrange(1280), -100)))
spawn_counter = 30
# Update player_rect.
player_rect.x += change_x
player_rect.y += change_y
# Update bullets.
for bullet in bullets:
bullet.y -= 4
# Detect collisions and move the enemies.
hit_enemies = []
for enemy_rect in enemies:
enemy_rect.y += 5
# Collision detection with pygame.Rect.colliderect.
if player_rect.colliderect(enemy_rect):
done = True
# Collision detection with bullets.
for bullet in bullets:
if bullet.colliderect(enemy_rect):
hit_enemies.append(enemy_rect)
# Filter the destroyed enemies out.
survivors = []
for enemy in enemies:
if enemy not in hit_enemies:
survivors.append(enemy)
enemies = survivors # Now `enemies` is the list of survivors.
# Draw everything.
screen.blit(BG_image, (0,0))
for bullet in bullets:
screen.blit(xbullet, bullet)
for enemy_rect in enemies:
screen.blit(tieImg, enemy_rect)
screen.blit(xwingImg, player_rect)
if player_rect.x >display_width:
player_rect.x = 0
if player_rect.x < 0:
player_rect.x= 1280
if player_rect.y>display_height:
player_rect.y = 0
if player_rect.y < 0:
player_rect.y= 800
pg.display.flip()
clock.tick(40)
if __name__ == '__main__':
main()
pg.quit()
sys.exit()
答案 1 :(得分:1)
您正在测试此
if event.type == space:
但
space = pg.image.load('space.jpg').convert()
所以event.type
永远不会匹配。
你需要这样的东西:
if event.type == pg.KEYUP:
if event.key == pg.K_SPACE:
就像你在测试其他键一样。
您的下一个问题是您尚未在任何地方{/ 1}分配