• 用Python Pygame做的一些好玩的小游戏


    有些游戏的代码比较长就不公布了

    1.简简单单

    1.疯狂的鸡哥

     你要准备的图片:

    命名为:ji.png 

     代码:

    1. import pygame
    2. import random as r
    3. pygame.init()
    4. pygame.display.set_caption('aaa')
    5. pm = pygame.display.set_mode((800,600))
    6. class Ls(pygame.sprite.Sprite):
    7. def __init__(self,pos):#pos 鼠标点击的位置
    8. super().__init__()
    9. self.image = pygame.image.load('ji.png')
    10. self.dx = r.randint(20,50)
    11. self.dx2 = r.randint(20, 50)
    12. self.image = pygame.transform.scale(self.image,(self.dx,self.dx2))
    13. self.rect = self.image.get_rect()
    14. self.rect.top = pos[0]
    15. self.rect.left =pos[1]
    16. self.vx = r.randint(-3,11)
    17. self.vy = r.randint(-2,12)
    18. # update方法
    19. def update(self):
    20. self.rect.left += self.vx
    21. self.rect.top += self.vy
    22. if self.rect.left <= 0 or self.rect.left >= 750:
    23. self.vx = -self.vx
    24. self.image = pygame.transform.scale(self.image,(self.dx,self.dx2))
    25. if self.rect.top <= 0 or self.rect.top >= 550:
    26. self.vy = -self.vy
    27. group = pygame.sprite.Group()
    28. while True:
    29. pm.fill((120,205,255))
    30. zt = pygame.mouse.get_pressed()
    31. zy = pygame.font.Font('叶根友刀锋黑草.ttf',50).render('左键增加滚轮碰到哪个哪个删除 ',True,(0,0,0))
    32. pm.blit(zy,(0,0))
    33. if zt[0]:
    34. group.add(Ls([100,200]))
    35. if zt[1]:
    36. wz = pygame.mouse.get_pos()
    37. for i in group:
    38. if i.rect.collidepoint(wz): #给定点是否在给定矩形范围中,前面的是给定矩形,括号中的是点.
    39. i.kill()
    40. group.draw(pm)
    41. group.update()
    42. for event in pygame.event.get():
    43. if event.type == pygame.QUIT:
    44. exit()
    45. pygame.display.update()
    46. pygame.time.Clock().tick(20)

     2.智慧的随机分数系统

     

    1. while True:
    2. import pygame
    3. pygame.init()
    4. pygame.display.set_caption('aaa')
    5. screen = pygame.display.set_mode((800,600))
    6. jts = pygame.image.load('照片\jts.png')
    7. jtx = pygame.image.load('照片\jtx.png')
    8. qd = pygame.image.load('照片\qd.png')
    9. a = 100
    10. mm = 1
    11. while mm == 1:
    12. screen.fill((255,255,255))
    13. ju = pygame.font.Font('字体\叶根友刀锋黑草.ttf', 100)
    14. s = ju.render(str(a), True, (1, 2, 200))
    15. ju2 = pygame.font.Font('字体\叶根友刀锋黑草.ttf', 100)
    16. s2 = ju2.render('满分:', True, (1, 2, 200))
    17. for event in pygame.event.get():
    18. if event.type == pygame.QUIT:
    19. exit()
    20. screen.blit(s,(300,300))
    21. screen.blit(qd, (620, 10))
    22. screen.blit(s2,(100,100))
    23. screen.blit(jts,(500,200))
    24. screen.blit(jtx,(500,400))
    25. x, y = pygame.mouse.get_pos()
    26. for i in range(111):
    27. for ii in range(101):
    28. if x == 500+i and y == 200 + ii and pygame.mouse.get_pressed()[0] == True:
    29. a = a+1
    30. for i in range(97):
    31. for ii in range(117):
    32. if x == 500+i and y == 400 + ii and pygame.mouse.get_pressed()[0] == True:
    33. a = a-1
    34. for i in range(154):
    35. for ii in range(79):
    36. if x == 620+i and y == 10 + ii and pygame.mouse.get_pressed()[0] == True:
    37. mm = 1333
    38. pygame.display.update()
    39. pygame.time.Clock().tick(20)
    40. import pygame
    41. import random as r
    42. pygame.init()
    43. pygame.display.set_caption('aaa')
    44. screen = pygame.display.set_mode((800,600))
    45. qd = pygame.image.load('照片\qd.png')
    46. b = str(r.randint(0,a)) + '分'
    47. mm = 1
    48. while mm == 1:
    49. screen.fill((255,255,255))
    50. ju = pygame.font.Font('字体\叶根友刀锋黑草.ttf', 50)
    51. s = ju.render(b, True, (1, 2, 200))
    52. ju2 = pygame.font.Font('字体\叶根友刀锋黑草.ttf', 50)
    53. s2 = ju2.render('我预测你这次考试会考:', True, (1, 2, 200))
    54. for event in pygame.event.get():
    55. if event.type == pygame.QUIT:
    56. exit()
    57. screen.blit(s,(100,200))
    58. screen.blit(qd, (620, 500))
    59. screen.blit(s2,(100,100))
    60. x, y = pygame.mouse.get_pos()
    61. for i in range(154):
    62. for ii in range(79):
    63. if x == 620+i and y == 500 + ii and pygame.mouse.get_pressed()[0] == True:
    64. print(1)
    65. mm = 1333
    66. pygame.display.update()
    67. pygame.time.Clock().tick(20)

    要准备的图片:

    jts.png  #向上的箭头
    jtx.png   #向下的箭头
    qd.png  #确定

    2.平平淡淡也挺好玩

    1.极速赛车

    准备一个汽车图片叫做:

    qc.png

    代码:

    1. import pygame,sys
    2. from random import*
    3. while True:
    4. js = ''
    5. class Block(pygame.sprite.Sprite):
    6. def __init__(self):
    7. super().__init__()
    8. self.kd = randint(90,180)
    9. self.image = pygame.Surface((self.kd,20))
    10. self.image.fill((255,255,255))
    11. self.rect = self.image.get_rect()
    12. self.rect.top = 0
    13. self.rect.left = 0
    14. def update(self,v):
    15. self.rect.top += v
    16. if self.rect.top >= 600:
    17. self.kill()
    18. class Block2(Block):
    19. def __init__(self):
    20. super().__init__()
    21. self.rect.left = 400-self.kd
    22. class Car(pygame.sprite.Sprite):
    23. def __init__(self):
    24. super().__init__()
    25. self.image = pygame.image.load('qc.png')
    26. self.image = pygame.transform.scale(self.image, (20, 50))
    27. self.rect = self.image.get_rect()
    28. self.rect.top = 400
    29. self.rect.left = 150
    30. def update(self):
    31. k = pygame.key.get_pressed()
    32. if k[pygame.K_RIGHT]:
    33. self.rect.left += 5
    34. elif k[pygame.K_LEFT]:
    35. self.rect.left -= 5
    36. if self.rect.left >= 400:
    37. self.rect.left = 400
    38. if self.rect.left <= 0:
    39. self.rect.left = 0
    40. def zi(dx,x,y):
    41. xz = pygame.font.Font('叶根友刀锋黑草.ttf', 50).render(str(dx), True, (0, 0, 0))
    42. screen.blit(xz,(x,y))
    43. pygame.init()
    44. screen = pygame.display.set_mode((400,600))
    45. pygame.display.set_caption("qc")
    46. group = pygame.sprite.Group()
    47. group2 = pygame.sprite.Group()
    48. group3 = pygame.sprite.Group()
    49. group3.add(Car())
    50. i = 0
    51. v = 3
    52. while True:
    53. screen.fill((0,0,255))
    54. for event in pygame.event.get():
    55. if event.type == pygame.QUIT:
    56. sys.exit()
    57. t = pygame.time.get_ticks()
    58. if js == '':
    59. miao = str(t / 1000)
    60. miao2 = int(miao[0])
    61. zi('速度:' + str(int(v)), 0, 0)
    62. if miao2 == 1 and miao2 != 0:
    63. v += 0.001
    64. i += 1
    65. jg = randint(20,50) #间隔
    66. if i % jg == 0:
    67. group.add(Block())
    68. group.draw(screen)
    69. group.update(v)
    70. jg2 = randint(20, 50) # 间隔2
    71. if i % jg2 == 0:
    72. group2.add(Block2())
    73. group2.draw(screen)
    74. group2.update(v)
    75. group3.draw(screen)
    76. group3.update()
    77. else:
    78. zi('Game Over',0,100)
    79. zi('按滚轮回去',50,200)
    80. #碰撞
    81. a = pygame.sprite.groupcollide(group,group3,False,False)
    82. b = pygame.sprite.groupcollide(group2, group3, False, False)
    83. if a or b:
    84. js = '结束'
    85. i = 0
    86. v = 3
    87. if pygame.mouse.get_pressed()[1] and js == '结束':
    88. break
    89. pygame.display.update()
    90. pygame.time.Clock().tick(60)

    2.滑雪 

    代码太长了:294行

     提供一个创意思路

    3.制作困难的游戏

    提供思路

    1.风叶穿行

    被我爆改成了火山穿行

    网站链接(B站的灵感):风叶穿行 (bilibili.com)

     2.鱿鱼游戏系列

     

     

    4.我还有很多有意思的pygame游戏 

     

  • 相关阅读:
    C语言之自定义类型------枚举和联合体
    金融帝国实验室(CapLab)官方更新_V9.1.15版本(2023年第64次)
    Linux下的基本指令
    Docker-Compose 使用
    机器学习8线性回归法Linear Regression
    怎样做好金融投资翻译
    Linux》yum与vim
    [附源码]计算机毕业设计springboot基于vuejs的爱宠用品销售app
    Docker快速极简配置nginx实现不同域名访问分流
    【LeetCode】136. 只出现一次的数 python
  • 原文地址:https://blog.csdn.net/pythonwyw123321/article/details/139201606