当前位置:   article > 正文

python-pygame小游戏之球球大作战_pygame球球大作战

pygame球球大作战

这是我第一次写文章,和大家分享我的python程序。请大家多多点赞,觉得我写的好的话还可以关注一下我。后期我会继续多发一些文章的哦!

今天我要来介绍介绍我自己做的游戏——球球大作战!大家来看看吧!

---------------------------------------------------开始写代码了!---------------------------------------------------------

做代码前的小贴士:

首先要用cmd安装好pygame模块哦!

pip install pygame

一、初始化

首先要导入很重要的pygame,random和math模块(我用了as),初始化程序,做好界面,写上标题

  1. # import pygame, random and math
  2. import pygame as pg
  3. import random as rd
  4. import math
  5. # init program
  6. pg.init()
  7. # set screen
  8. screen = pg.display.set_mode((1000, 500))
  9. screen.fill((255, 255, 255))
  10. # set title
  11. pg.display.set_caption("球球大作战", "4.0")

初始化好了程序才能进行下一步。

二、函数、方法和类的定义

程序少不了函数与类。我们得先定义好函数circle,这样方便画圆圈。

  1. # def circle
  2. def circle(color, point, r, size):
  3. pg.draw.circle(screen, color, point, r, size)

接下来就要做球的类了。为了可以被吃,所以要用类(懂了吗?)

  1. # class ball
  2. class Ball():
  3. def __init__(self):
  4. self.color = (rd.randint(0, 255), rd.randint(0, 255), rd.randint(0, 255))
  5. self.x =rd.randint(0, 1000)
  6. self.y = rd.randint(0, 500)
  7. self.r = rd.randint(5, 15)
  8. self.size = rd.randint(5, 15)

然后设置好玩家操控的球和画球的列表。

  1. # make a balllist
  2. balllist = []
  3. for i in range(600):
  4. balllist.append(Ball())
  5. # creat myball
  6. myball = Ball()
  7. myball.color = (0, 0, 0)
  8. myball.x = 500
  9. myball.y = 250
  10. myball.size = 5
  11. myball.speed = 10

这一段的最后要做好判断按下,吃球和生成。

这里要运用上math模块,还用了勾股定理!

每过十秒生成30个球,这样就“永远”也吃不完球了

为了更真实一点,我还加了一些其它代码。越吃得多,速度就越慢。这里推荐*=0.(),想要多少可以自己调。为什么要*=呢?因为/或//要么除不进或者除到零了就麻烦了。

  1. # def check touch
  2. # use the pythagorean theorem
  3. def touch(myX, myY, fX, fY, myR, fR):
  4. distance = math.sqrt((myX - fX) ** 2 + (myY - fY) ** 2)
  5. if distance <= myR + fR:
  6. # just return True
  7. return True
  8. else:
  9. # return False
  10. return False
  11. # def foodDelivery
  12. def foodDelivery():
  13. time = pg.time.get_ticks()
  14. # every 10 seconds put 30 foods
  15. if time % 10000 >= 9000 and time % 10000 <= 9020:
  16. for i in range(30):
  17. balllist.append(Ball())
  18. # def draw
  19. # use "Ball" and for range to append in the balllist
  20. def draw():
  21. for ball in balllist:
  22. if touch(myball.x, myball.y, ball.x, ball.y, myball.size, ball.size):
  23. balllist.remove(ball)
  24. myball.size += 0.1
  25. # make the speed to be smaller than the last one
  26. # use the multiplier scale decreases and inverse proportional function
  27. myball.speed *= 0.992
  28. else:
  29. circle(ball.color, (ball.x, ball.y), ball.size, 0)
  30. circle(myball.color, (myball.x, myball.y), myball.size, 0)

这样这一段就做好了!

三、主程序运行

接下来必须得认真了,马上就要运行主程序了。

小贴士:fps(帧率)一定要控制好,否则电脑显卡和cpu可能会卡,还要写好防卡程序,可以避免程序忽然报错或者暂停退出。

  1. # check fps, do not quit program
  2. fps = pg.time.Clock()
  3. # check quit and play program
  4. while True:
  5. # do not make the fps so high
  6. # if the fps is high, the computer will ~"bomb!"
  7. fps.tick(60)
  8. event = pg.event.poll()
  9. if event.type == pg.QUIT:
  10. pg.quit()
  11. exit()

接下来要做球的移动了,要用pygame.key.get_pressed()来判断哦。

  1. keys = pg.key.get_pressed()
  2. # make the ball to move
  3. # I use the "wasd"
  4. # also can use up down right left
  5. if keys[pg.K_w]:
  6. myball.y -= myball.speed

“wasd”上下左右都可以,注意x,y坐标轴的加减。

  1. # check quit and play program
  2. while True:
  3. # do not make the fps so high
  4. # if the fps is high, the computer will ~"bomb!"
  5. fps.tick(60)
  6. event = pg.event.poll()
  7. if event.type == pg.QUIT:
  8. pg.quit()
  9. exit()
  10. keys = pg.key.get_pressed()
  11. # make the ball to move
  12. # I use the "wasd"
  13. # also can use up down right left
  14. if keys[pg.K_w]:
  15. myball.y -= myball.speed
  16. if keys[pg.K_a]:
  17. myball.x -= myball.speed
  18. if keys[pg.K_s]:
  19. myball.y += myball.speed
  20. if keys[pg.K_d]:
  21. myball.x += myball.speed
  22. if keys[pg.K_UP]:
  23. myball.y -= myball.speed
  24. if keys[pg.K_DOWN]:
  25. myball.y += myball.speed
  26. if keys[pg.K_LEFT]:
  27. myball.x -= myball.speed
  28. if keys[pg.K_RIGHT]:
  29. myball.x += myball.speed
  30. # the e is to update ball's xy
  31. elif keys[pg.K_e]:
  32. myball.x, myball.y = 500, 250

最后写上更新和画图的方法,整个程序就写好了!(别忘了加一次fill,这样易于更新)

  1. while True:
  2. # do not make the fps so high
  3. # if the fps is high, the computer will ~"bomb!"
  4. fps.tick(60)
  5. event = pg.event.poll()
  6. if event.type == pg.QUIT:
  7. pg.quit()
  8. exit()
  9. keys = pg.key.get_pressed()
  10. # make the ball to move
  11. # I use the "wasd"
  12. # also can use up down right left
  13. if keys[pg.K_w]:
  14. myball.y -= myball.speed
  15. if keys[pg.K_a]:
  16. myball.x -= myball.speed
  17. if keys[pg.K_s]:
  18. myball.y += myball.speed
  19. if keys[pg.K_d]:
  20. myball.x += myball.speed
  21. if keys[pg.K_UP]:
  22. myball.y -= myball.speed
  23. if keys[pg.K_DOWN]:
  24. myball.y += myball.speed
  25. if keys[pg.K_LEFT]:
  26. myball.x -= myball.speed
  27. if keys[pg.K_RIGHT]:
  28. myball.x += myball.speed
  29. # the e is to update ball's xy
  30. elif keys[pg.K_e]:
  31. myball.x, myball.y = 500, 250
  32. # draw and check
  33. draw()
  34. foodDelivery()
  35. # display program
  36. pg.display.update()
  37. screen.fill((255, 255, 255))

四、完整代码

最后奉上全部代码:

  1. # import pygame, random and math
  2. import pygame as pg
  3. import random as rd
  4. import math
  5. # init program
  6. pg.init()
  7. # set screen
  8. screen = pg.display.set_mode((1000, 500))
  9. screen.fill((255, 255, 255))
  10. # set title
  11. pg.display.set_caption("BallFight_Avaritia", "4.0")
  12. # Chinese:pg.display.set_caption("球球大作战_无尽贪婪", "4.0")
  13. # def circle
  14. def circle(color, point, r, size):
  15. pg.draw.circle(screen, color, point, r, size)
  16. # class ball
  17. class Ball():
  18. def __init__(self):
  19. self.color = (rd.randint(0, 255), rd.randint(0, 255), rd.randint(0, 255))
  20. self.x =rd.randint(0, 1000)
  21. self.y = rd.randint(0, 500)
  22. self.r = rd.randint(5, 15)
  23. self.size = rd.randint(5, 15)
  24. # make a balllist
  25. balllist = []
  26. for i in range(600):
  27. balllist.append(Ball())
  28. # creat myball
  29. myball = Ball()
  30. myball.color = (0, 0, 0)
  31. myball.x = 500
  32. myball.y = 250
  33. myball.size = 5
  34. myball.speed = 10
  35. # def check touch
  36. # use the pythagorean theorem
  37. def touch(myX, myY, fX, fY, myR, fR):
  38. distance = math.sqrt((myX - fX) ** 2 + (myY - fY) ** 2)
  39. if distance <= myR + fR:
  40. # just return True
  41. return True
  42. else:
  43. # return False
  44. return False
  45. # def foodDelivery
  46. def foodDelivery():
  47. time = pg.time.get_ticks()
  48. # every 10 seconds put 30 foods
  49. if time % 10000 >= 9000 and time % 10000 <= 9020:
  50. for i in range(30):
  51. balllist.append(Ball())
  52. # def draw
  53. # use "Ball" and for range to append in the balllist
  54. def draw():
  55. for ball in balllist:
  56. if touch(myball.x, myball.y, ball.x, ball.y, myball.size, ball.size):
  57. balllist.remove(ball)
  58. myball.size += 0.1
  59. # make the speed to be smaller than the last one
  60. # use the multiplier scale decreases and inverse proportional function
  61. myball.speed *= 0.992
  62. else:
  63. circle(ball.color, (ball.x, ball.y), ball.size, 0)
  64. circle(myball.color, (myball.x, myball.y), myball.size, 0)
  65. # check fps, do not quit program
  66. fps = pg.time.Clock()
  67. # check quit and play program
  68. while True:
  69. # do not make the fps so high
  70. # if the fps is high, the computer will ~"bomb!"
  71. fps.tick(60)
  72. event = pg.event.poll()
  73. if event.type == pg.QUIT:
  74. pg.quit()
  75. exit()
  76. keys = pg.key.get_pressed()
  77. # make the ball to move
  78. # I use the "wasd"
  79. # also can use up down right left
  80. if keys[pg.K_w]:
  81. myball.y -= myball.speed
  82. if keys[pg.K_a]:
  83. myball.x -= myball.speed
  84. if keys[pg.K_s]:
  85. myball.y += myball.speed
  86. if keys[pg.K_d]:
  87. myball.x += myball.speed
  88. if keys[pg.K_UP]:
  89. myball.y -= myball.speed
  90. if keys[pg.K_DOWN]:
  91. myball.y += myball.speed
  92. if keys[pg.K_LEFT]:
  93. myball.x -= myball.speed
  94. if keys[pg.K_RIGHT]:
  95. myball.x += myball.speed
  96. # the e is to update ball's xy
  97. elif keys[pg.K_e]:
  98. myball.x, myball.y = 500, 250
  99. # draw and check
  100. draw()
  101. foodDelivery()
  102. # display program
  103. pg.display.update()
  104. screen.fill((255, 255, 255))

五、效果图

六、知识总结

通过这次编程,我们了解了如何使用pygame做游戏,也用到了许多模块和复杂的代码。

我们也从中学习到了random, math和pygame模块的运用方法,了解了pygme模块的厉害。

我们操控的球,就好像大白们和白衣天使,他们竭尽全力,与病毒(其他可以吃的球)战斗,让我们更加安全。

希望大家多多学习python,借助在家隔离的这段期间学习编程,分享程序,共克时艰,战胜疫情。

大家可以多多点赞哦~谢谢阅读!下次再见

彩蛋:(下期透露:极光迷宫)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/386663
推荐阅读
相关标签
  

闽ICP备14008679号