当前位置:   article > 正文

python实现小游戏——飞机大战_register_shape('enemy1.gif') 出现黄色告警

register_shape('enemy1.gif') 出现黄色告警

这里主要使用turtle来开发,pygame来添加音效

参考:跟麦叔学Python - 1小时开发太空侵略者游戏

游戏介绍_哔哩哔哩_bilibili

安装

安装turtle
pip install turtle
安装pygame
pip install pygame

程序的末尾

在末尾处写上这两句

  1. turtle.done()
  2. game.mainloop()

创建游戏背景

设置700*700大小的背景

  1. import turtle
  2. game = turtle.Screen()
  3. game.setup(700.700)
  4. game.title('飞机大战')
  5. game.bgpic('bg.png')
  6. turtle.done()

创建玩家

注意:这里的图片都要用gif格式的

  1. # 创建玩家
  2. turtle.register_shape('player2.gif')
  3. player = turtle.Turtle()
  4. player.ht()
  5. player.speed(0)
  6. player.up()
  7. player.shape('player2.gif')
  8. player.setpos(0,-230)
  9. player.st()

玩家左右移动

  1. # 设置步长
  2. player_step = 20
  3. # 玩家左移和右移函数
  4. def go_left():
  5. x = player.xcor()
  6. x = x - player_step
  7. if x < -300:
  8. x = -300
  9. player.setx(x)
  10. def go_right():
  11. x = player.xcor()
  12. x = x + player_step
  13. if x > 300:
  14. x = 300
  15. player.setx(x)
  16. turtle.listen()
  17. # 左移和右移
  18. turtle.onkey(go_left,'Left')
  19. turtle.onkey(go_right,'Right')

创建敌人

这里创建6个敌人,每个敌人的初始位置随机

  1. # 添加敌人
  2. num = 6
  3. inv_list = []
  4. turtle.register_shape('enemy.gif')
  5. for i in range(0,6):
  6. inv = turtle.Turtle()
  7. inv_list.append(inv)
  8. inv.ht()
  9. inv.speed(0)
  10. inv.up()
  11. inv.shape('enemy.gif')
  12. x = random.randint(-200,200)
  13. y = random.randint(100,220)
  14. inv.setpos(x,y)
  15. inv.st()

让敌人动起来

这个while循环是死循环,要写在后面

  1. # 敌人动起来
  2. inv_step = 5
  3. go_back = False
  4. while True:
  5. for inv in inv_list:
  6. x = inv.xcor()
  7. x += inv_step
  8. inv.setx(x)
  9. if inv.xcor() > 300 or inv.xcor() < -300:
  10. go_back = True
  11. # 掉头
  12. if go_back:
  13. inv_step *= -1
  14. go_back = False
  15. for inv in inv_list:
  16. y = inv.ycor()
  17. y -= 40
  18. inv.sety(y)

创建子弹

  1. # 添加子弹
  2. bomb = turtle.Turtle()
  3. bomb.ht()
  4. bomb.speed(0)
  5. bomb.up()
  6. bomb.shape('triangle')
  7. bomb.color('yellow')
  8. bomb.shapesize(0.5,0.5)
  9. bomb.seth(90)
  10. bomb.setpos(-350,-350)

按空格发射子弹

发射子弹

  1. # 发射子弹
  2. is_fired = False
  3. def fire():
  4. global is_fired
  5. if is_fired == False:
  6. bomb.setpos(player.xcor(), player.ycor() + 30)
  7. bomb.st()
  8. is_fired = True
  9. turtle.onkey(fire,'space')

子弹射出:

  1. # 敌人动起来
  2. inv_step = 5
  3. go_back = False
  4. bomb_step = 50
  5. while True:
  6. for inv in inv_list:
  7. x = inv.xcor()
  8. x += inv_step
  9. inv.setx(x)
  10. if inv.xcor() > 300 or inv.xcor() < -300:
  11. go_back = True
  12. # 掉头
  13. if go_back:
  14. inv_step *= -1
  15. go_back = False
  16. for inv in inv_list:
  17. y = inv.ycor()
  18. y -= 40
  19. inv.sety(y)
  20. # 子弹被发射
  21. if is_fired:
  22. y = bomb.ycor()
  23. y += bomb_step
  24. bomb.sety(y)
  25. if y > 250:
  26. is_fired = False
  27. bomb.setpos(-350, -350)
  28. bomb.ht()

击杀敌人

打中后的敌人再次随机生成位置

  1. # 敌人动起来
  2. inv_step = 5
  3. go_back = False
  4. bomb_step = 50
  5. while True:
  6. for inv in inv_list:
  7. x = inv.xcor()
  8. x += inv_step
  9. inv.setx(x)
  10. if inv.xcor() > 300 or inv.xcor() < -300:
  11. go_back = True
  12. # 打中敌人,并重新生成敌人位置
  13. if inv.distance(bomb) < 30:
  14. x = random.randint(-200, 200)
  15. y = random.randint(-50, 220)
  16. inv.setpos(x,y)
  17. is_fired = False
  18. bomb.setpos(-350,-350)
  19. bomb.ht()
  20. # 掉头
  21. if go_back:
  22. inv_step *= -1
  23. go_back = False
  24. for inv in inv_list:
  25. y = inv.ycor()
  26. y -= 40
  27. inv.sety(y)
  28. # 子弹被发射
  29. if is_fired:
  30. y = bomb.ycor()
  31. y += bomb_step
  32. bomb.sety(y)
  33. if y > 250:
  34. is_fired = False
  35. bomb.setpos(-350,-350)
  36. bomb.ht()

添加分数

  1. # 添加分数
  2. score = 0
  3. pen = turtle.Turtle()
  4. pen.color('white')
  5. pen.speed(0)
  6. pen.up()
  7. pen.ht()
  8. pen.setpos(-340,265)
  9. score_string = "分数:%s" %score
  10. pen.write(score_string,align="left",font=('Arial',12,'normal'))
  1. # 敌人动起来
  2. inv_step = 5
  3. go_back = False
  4. bomb_step = 50
  5. while True:
  6. for inv in inv_list:
  7. x = inv.xcor()
  8. x += inv_step
  9. inv.setx(x)
  10. if inv.xcor() > 300 or inv.xcor() < -300:
  11. go_back = True
  12. # 打中敌人,并重新生成敌人位置
  13. if inv.distance(bomb) < 30:
  14. x = random.randint(-200, 200)
  15. y = random.randint(-50, 220)
  16. inv.setpos(x,y)
  17. is_fired = False
  18. bomb.setpos(-350,-350)
  19. bomb.ht()
  20. score += 10
  21. score_string = "分数:%s" % score
  22. pen.clear()
  23. pen.write(score_string, align="left", font=('Arial', 12, 'normal'))
  24. # 掉头
  25. if go_back:
  26. inv_step *= -1
  27. go_back = False
  28. for inv in inv_list:
  29. y = inv.ycor()
  30. y -= 40
  31. inv.sety(y)
  32. # 子弹被发射
  33. if is_fired:
  34. y = bomb.ycor()
  35. y += bomb_step
  36. bomb.sety(y)
  37. if y > 250:
  38. is_fired = False
  39. bomb.setpos(-350,-350)
  40. bomb.ht()

判定游戏结束

添加分界线

  1. # 画一条线,敌人越过这条线游戏结束
  2. t = turtle.Turtle()
  3. t.pencolor("white")
  4. t.penup()
  5. t.goto(-350, -200)
  6. t.pendown()
  7. for i in range(35):
  8. t.forward(10)
  9. t.penup()
  10. t.forward(10)
  11. t.pendown()

敌人越过分界线,游戏结束

  1. # 敌人动起来
  2. inv_step = 5
  3. go_back = False
  4. bomb_step = 50
  5. game_over = False
  6. while True:
  7. # 游戏结束
  8. if game_over:
  9. pen2 = turtle.Turtle()
  10. pen2.color('red')
  11. pen2.ht()
  12. pen2.write('游戏结束',align='center',font=('Arial',18,'normal'))
  13. explosion.play()
  14. break
  15. for inv in inv_list:
  16. x = inv.xcor()
  17. x += inv_step
  18. inv.setx(x)
  19. if inv.xcor() > 300 or inv.xcor() < -300:
  20. go_back = True
  21. # 打中敌人,并重新生成敌人位置
  22. if inv.distance(bomb) < 30:
  23. x = random.randint(-200, 200)
  24. y = random.randint(-50, 220)
  25. inv.setpos(x,y)
  26. is_fired = False
  27. bomb.setpos(-350,-350)
  28. bomb.ht()
  29. score += 10
  30. score_string = "分数:%s" % score
  31. pen.clear()
  32. pen.write(score_string, align="left", font=('Arial', 12, 'normal'))
  33. # 判定游戏结束
  34. if inv.ycor() < -180:
  35. game_over = True
  36. # 掉头
  37. if go_back:
  38. inv_step *= -1
  39. go_back = False
  40. for inv in inv_list:
  41. y = inv.ycor()
  42. y -= 40
  43. inv.sety(y)
  44. # 子弹被发射
  45. if is_fired:
  46. y = bomb.ycor()
  47. y += bomb_step
  48. bomb.sety(y)
  49. if y > 250:
  50. is_fired = False
  51. bomb.setpos(-350,-350)
  52. bomb.ht()

添加音效

  1. import pygame
  2. # 音乐
  3. pygame.init()
  4. laser = pygame.mixer.Sound('laser.wav')
  5. explosion = pygame.mixer.Sound('exp.wav')
  6. bg = pygame.mixer.Sound('bg.wav')
  1. # 使用bg音乐
  2. bg.play()

完整代码

  1. import turtle
  2. import random
  3. import pygame
  4. # 音乐
  5. pygame.init()
  6. laser = pygame.mixer.Sound('laser.wav')
  7. explosion = pygame.mixer.Sound('exp.wav')
  8. bg = pygame.mixer.Sound('bg.wav')
  9. bg.play()
  10. # 设置背景
  11. game = turtle.Screen()
  12. game.setup(700.700)
  13. game.title('飞机大战')
  14. game.bgpic('bg.png')
  15. # 画一条线,敌人越过这条线游戏结束
  16. t = turtle.Turtle()
  17. t.pencolor("white")
  18. t.penup()
  19. t.goto(-350, -200)
  20. t.pendown()
  21. for i in range(35):
  22. t.forward(10)
  23. t.penup()
  24. t.forward(10)
  25. t.pendown()
  26. # 创建玩家
  27. turtle.register_shape('player2.gif')
  28. player = turtle.Turtle()
  29. player.ht()
  30. player.speed(0)
  31. player.up()
  32. player.shape('player2.gif')
  33. player.setpos(0,-230)
  34. player.st()
  35. # 设置步长
  36. player_step = 20
  37. # 玩家左移和右移函数
  38. def go_left():
  39. x = player.xcor()
  40. x = x - player_step
  41. if x < -300:
  42. x = -300
  43. player.setx(x)
  44. def go_right():
  45. x = player.xcor()
  46. x = x + player_step
  47. if x > 300:
  48. x = 300
  49. player.setx(x)
  50. turtle.listen()
  51. # 左移和右移
  52. turtle.onkey(go_left,'Left')
  53. turtle.onkey(go_right,'Right')
  54. # 添加敌人
  55. num = 6
  56. inv_list = []
  57. turtle.register_shape('enemy.gif')
  58. for i in range(0,6):
  59. inv = turtle.Turtle()
  60. inv_list.append(inv)
  61. inv.ht()
  62. inv.speed(0)
  63. inv.up()
  64. inv.shape('enemy.gif')
  65. x = random.randint(-200,200)
  66. y = random.randint(100,220)
  67. inv.setpos(x,y)
  68. inv.st()
  69. # 添加子弹
  70. bomb = turtle.Turtle()
  71. bomb.ht()
  72. bomb.speed(0)
  73. bomb.up()
  74. bomb.shape('triangle')
  75. bomb.color('yellow')
  76. bomb.shapesize(0.5,0.5)
  77. bomb.seth(90)
  78. bomb.setpos(-350,-350)
  79. # 发射子弹
  80. is_fired = False
  81. def fire():
  82. global is_fired
  83. if is_fired == False:
  84. bomb.setpos(player.xcor(), player.ycor() + 30)
  85. bomb.st()
  86. is_fired = True
  87. laser.play()
  88. turtle.onkey(fire,'space')
  89. # 添加分数
  90. score = 0
  91. pen = turtle.Turtle()
  92. pen.color('white')
  93. pen.speed(0)
  94. pen.up()
  95. pen.ht()
  96. pen.setpos(-340,265)
  97. score_string = "分数:%s" %score
  98. pen.write(score_string,align="left",font=('Arial',12,'normal'))
  99. # 敌人动起来
  100. inv_step = 5
  101. go_back = False
  102. bomb_step = 50
  103. game_over = False
  104. while True:
  105. # 游戏结束
  106. if game_over:
  107. pen2 = turtle.Turtle()
  108. pen2.color('red')
  109. pen2.ht()
  110. pen2.write('游戏结束',align='center',font=('Arial',18,'normal'))
  111. explosion.play()
  112. break
  113. for inv in inv_list:
  114. x = inv.xcor()
  115. x += inv_step
  116. inv.setx(x)
  117. if inv.xcor() > 300 or inv.xcor() < -300:
  118. go_back = True
  119. # 打中敌人,并重新生成敌人位置
  120. if inv.distance(bomb) < 30:
  121. x = random.randint(-200, 200)
  122. y = random.randint(-50, 220)
  123. inv.setpos(x,y)
  124. is_fired = False
  125. bomb.setpos(-350,-350)
  126. bomb.ht()
  127. score += 10
  128. score_string = "分数:%s" % score
  129. pen.clear()
  130. pen.write(score_string, align="left", font=('Arial', 12, 'normal'))
  131. explosion.play()
  132. # 判定游戏结束
  133. if inv.ycor() < -180:
  134. game_over = True
  135. # 掉头
  136. if go_back:
  137. inv_step *= -1
  138. go_back = False
  139. for inv in inv_list:
  140. y = inv.ycor()
  141. y -= 40
  142. inv.sety(y)
  143. # 子弹被发射
  144. if is_fired:
  145. y = bomb.ycor()
  146. y += bomb_step
  147. bomb.sety(y)
  148. if y > 250:
  149. is_fired = False
  150. bomb.setpos(-350,-350)
  151. bomb.ht()
  152. turtle.done()
  153. game.mainloop()
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号