当前位置:   article > 正文

pygame写游戏

pygame写游戏

1.安装pygame

这个链接里面安装问题我真的遇到了!!!!

2.想想我要写点什么……

参考:

one:
我其实超级想用用王者荣耀的东西的,趁机熟悉熟悉王者世界观什么的,百度查到一个用LOL地图的

two:

基于pygame的蚂蚁打蜘蛛嘤嘤嘤

three:
这个博客里面有一大堆,我觉得有的思路真的很难

还有这个(应该是一样的就是这个可能是总结好的)

four:

应该是很简单的一个球球的游戏

five:

勇闯黑暗迷宫

3.我有一点点想写一个双人对战的游戏,就用王者的角色

参考点:

塔防游戏(兔子和guan)

这个可能比较详细:http://blog.jobbole.com/46308/

英文的:https://www.raywenderlich.com/2795-beginning-game-programming-for-teens-with-python

植物大战僵尸

4.确定好辽,写一个塔防游戏,就模仿那个兔子

  1. #妲己:512*528
  2. #娜可露露:256*256
  3. #蓝方防御塔128*128
  4. #红方防御塔:128*128
  5. import pygame
  6. from pygame.locals import *
  7. pygame.init()
  8. width,height = 1280,720
  9. screen=pygame.display.set_mode((width,height))
  10. Blue_player = pygame.image.load("G:/fan/Python/游戏/王者/image/Hero_Blue.png")
  11. while 1:
  12. screen.fill(0)
  13. screen.blit(Blue_player,(100,100))
  14. pygame.display.flip()
  15. for event in pygame.event.get():
  16. if event.type==pygame.QUIT:
  17. pygame.quit()
  18. exit(0)

效果图

可以插入图片了,就先布局好?

5.使蓝方玩家可以移动

在#2设置一个变量Hero_Blue_pos=[30,30]

WASFD键:keys = [False, False, False, False]

  1. #妲己:512*528 156*156
  2. #娜可露露:156*156
  3. #蓝方防御塔128*128
  4. #红方防御塔:128*128
  5. # 1
  6. import pygame
  7. from pygame.locals import *
  8. # 2
  9. pygame.init()
  10. width,height = 1280,720
  11. screen=pygame.display.set_mode((width,height))
  12. keys = [False, False, False, False]
  13. Hero_Blue_pos=[30,30]
  14. # 3 加载图片
  15. Blue_player = pygame.image.load("image/Hero_Blue.png")
  16. bg_haidu = pygame.image.load("image/bg_haidu.jpg")
  17. Hero_Blue_ = pygame.image.load("image/Hero_Blue_.png")
  18. Hero_Red_ = pygame.image.load("image/Hero_Red_.png")
  19. Blue_Tower1 = pygame.image.load("image/Blue_Tower1.png")
  20. Blue_Tower2 = pygame.image.load("image/Blue_Tower2.png")
  21. Blue_Tower3 = pygame.image.load("image/Blue_Tower3.png")
  22. Red_tower1 = pygame.image.load("image/Red_tower1.png")
  23. Red_tower2 = pygame.image.load("image/Red_tower2.png")
  24. Red_tower3 = pygame.image.load("image/Red_tower3.png")
  25. #4
  26. while 1:
  27. # 5
  28. screen.fill(0)
  29. # 6
  30. screen.blit(bg_haidu,(0,0))
  31. #screen.blit(Blue_player,(100,100))
  32. #大一点的小妲己(这个后面的图片会盖在前面的图片上
  33. screen.blit(Hero_Blue_,Hero_Blue_pos)
  34. screen.blit(Blue_Tower1,(33,227.25))
  35. screen.blit(Blue_Tower2,(33,391.5))
  36. screen.blit(Blue_Tower3,(33,555.75))
  37. screen.blit(Hero_Red_,(1119,30))
  38. screen.blit(Red_tower1,(1119,227.25))
  39. screen.blit(Red_tower2,(1119,391.5))
  40. screen.blit(Red_tower3,(1119,555.75))
  41. # 7
  42. pygame.display.flip()
  43. # 8
  44. for event in pygame.event.get():
  45. if event.type==pygame.QUIT:
  46. pygame.quit()
  47. exit(0)

接下来,根据按下的键来更新按键记录数组。PyGame里用给按键添加事件的方法来检测按键。

在#8结尾,就在event.py == pygame.QUIT后面,添加一下的代码(与pygame.QUIT保持同级别缩进):

  1. if event.type == pygame.KEYDOWN:
  2. if event.key==K_w:
  3. keys[0]=True
  4. elif event.key==K_a:
  5. keys[1]=True
  6. elif event.key==K_s:
  7. keys[2]=True
  8. elif event.key==K_d:
  9. keys[3]=True
  10. if event.type == pygame.KEYUP:
  11. if event.key==pygame.K_w:
  12. keys[0]=False
  13. elif event.key==pygame.K_a:
  14. keys[1]=False
  15. elif event.key==pygame.K_s:
  16. keys[2]=False
  17. elif event.key==pygame.K_d:
  18. keys[3]=False

 

首先,你检查是否有一个键被按下或放开。然后,检查是哪一个键被按下或放开了,如果被按下或放开的键是你使用的,你就更新记录按键的变量。

最终,你需要更新playerpos变量作为按键后的反应。这实际上是非常简单的。

把一下的代码加到game.py的结尾:(让它与for 循环保持同级别缩进)

  1. # 9 - Move player
  2. if keys[0]:
  3. Hero_Blue_pos[1]-=5
  4. elif keys[2]:
  5. Hero_Blue_pos[1]+=5
  6. if keys[1]:
  7. Hero_Blue_pos[0]-=5
  8. elif keys[3]:
  9. Hero_Blue_pos[0]+=5

 可以了?

 6.转向

  1. import math

把#6部分的最后一行用一下代码替换:

  1. # 6.1 - Set player position and rotation
  2. position = pygame.mouse.get_pos()
  3. angle = math.atan2(position[1]-(Hero_Blue_pos[1]+88),position[0]-(Hero_Blue_pos[0]+80.5))
  4. Hero_Blue_rot = pygame.transform.rotate(Hero_Blue_, 360-angle*57.29)
  5. Hero_Blue_pos1 = (Hero_Blue_pos[0]-Hero_Blue_rot.get_rect().width/2, Hero_Blue_pos[1]-Hero_Blue_rot.get_rect().height/2)
  6. screen.blit(Hero_Blue_rot, Hero_Blue_pos1)

7.射箭

在#2声明的部分加上必要的变量的声明。

  1. acc=[0,0]
  2. arrows=[]

第一个变量会跟踪玩家的精度,第二个变量会跟踪箭头。这个精度的变量本质上是一个数字组成的列表,记录了射出的箭头数和被击中的獾的数量。之后我们会用到这些信息用来计算射击精确度。

接下来,在#3部分结尾加载箭头的图片。

arrow = pygame.image.load("image/bullet.png")

在#8部分加上以下代码:

  1. #点击鼠标射出箭头
  2. if event.type==pygame.MOUSEBUTTONDOWN:
  3. position=pygame.mouse.get_pos()
  4. acc[1]+=1
  5. arrows.append([math.atan2(position[1]-(Hero_Blue_pos1[1]+88),position[0]-(Hero_Blue_pos1[0]+80.5)),Hero_Blue_pos1[0]+88,Hero_Blue_pos1[1]+80.5])

这段代码会检查是否鼠标被点击了,如果点击了,它就会得到鼠标的位置并且根据玩家和光标的位置计算出箭头旋转角度。旋转角度的值存放在arrows这个数组里。

接下来,你需要真的在屏幕上画出箭头来。在#6部分加上以下代码:

  1. # 6.2 - Draw arrows
  2. for bullet in arrows:
  3. index=0
  4. velx=math.cos(bullet[0])*10 #速度
  5. vely=math.sin(bullet[0])*10
  6. bullet[1]+=velx
  7. bullet[2]+=vely
  8. #参数991表示不会射出塔
  9. if bullet[1]<-156 or bullet[1]>991 or bullet[2]<-156 or bullet[2]>720:
  10. arrows.pop(index)
  11. index+=1
  12. for projectile in arrows:
  13. arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29)
  14. screen.blit(arrow1, (projectile[1], projectile[2]))

8.愤怒的娜可露露守塔

在这一步,你将会随机创建出一些小兵冲向城堡。在游戏的进程中会有越来越多的兵冒出来。所以,我们来列个接下来要做的事情的清单。

  1. 添加一个兵的列表
  2. 更新兵的信息,并且检查它们是否超出屏幕范围
  3. 展示这些兵

第一步,在#2部分加上一下代码:

  1. badtimer=100
  2. badtimer1=0
  3. badguys=[[640,100]]
  4. healthvalue=194

以上的代码里定义了一个定时器,使得游戏里可以经过一段时间后就新建一只兵

在#3部分结尾处添加以下代码:

  1. badguyimg1 = pygame.image.load("image/badguy.png")
  2. badguyimg=badguyimg1

第一行跟前面加载图片的代码很相似。第二行声明了一个图片的复制。

接下来,你需要更新并且显示这些坏蛋了。在#6.2部分加上以下代码:

  1. # 6.3 - Draw badgers
  2. if badtimer==0:
  3. badguys.append([1091, random.randint(191,529)])
  4. badtimer=100-(badtimer1*2)
  5. if badtimer1>=35:
  6. badtimer1=35
  7. else:
  8. badtimer1+=5
  9. index=0
  10. for badguy in badguys:
  11. if badguy[0]<-64:
  12. badguys.pop(index)
  13. badguy[0]-=7
  14. index+=1
  15. for badguy in badguys:
  16. screen.blit(badguyimg, badguy)

上面的代码看起来有不少。第一行是来检查badtime是否为0,如果为0,创建一个兵然后重新设置badtime。第一个循环更新兵的x坐标,检查兵是否超出屏幕范围,如果超出范围,将兵删掉。第二个循环是来画出所有的兵。

为了在以上代码里用到随机的功能,你需要导入random库。所以在#1部分加上导入的代码:

import random

最后,把一行代码添加到#4部分的while表达式后面:

badtimer-=1

把以下代码加到#6.3部分index+=1之前的第一个循环里:

  1. # 6.3.1 - Attack castle
  2. badrect=pygame.Rect(badguyimg.get_rect())
  3. badrect.top=badguy[1]
  4. badrect.left=badguy[0]
  5. if badrect.left<64:
  6. healthvalue -= random.randint(5,20)
  7. badguys.pop(index)
  8. # 6.3.3 - Next bad guy

这段代码相当简单。如果兵的x坐标离右边小于64,就删除坏蛋并且减少游戏里的健康值,减少的大小为5至20里的一个随机数。

再次运行这个游戏,你就会有一些兵冲过来并且在碰到城堡的时候会消失。尽管你看不到,兵实际上会降低你的健康值。

9.兵与箭头的碰撞

在#6.3.1部分后面加这些:

  1. #6.3.2 - Check for collisions
  2. index1=0
  3. for bullet in arrows:
  4. bullrect=pygame.Rect(arrow.get_rect())
  5. bullrect.left=bullet[1]
  6. bullrect.top=bullet[2]
  7. if badrect.colliderect(bullrect):
  8. acc[0]+=1
  9. badguys.pop(index)
  10. arrows.pop(index1)
  11. index1+=1

这段代码里面只有一个地方需要提一下,这个if表达式使用了PyGame内建功能来检查两个矩形是否交叉。接下来的一些代码跟上面说的一样。

再运行下代码,现在你就可以用箭头来杀死兵了。

10.添加健康值和时间的显示

现在游戏运行起来相当不错了。现在有攻击者,有防守者。现在,你需要的就是通过一个方法来显示兔子的得分。

最简单的方法就是添加一个HUD来显示当前城堡的生命值。你也可以加一个计时来记录城堡存活下来的时间。

首先,添加一个计时。在#7部分加上下面的代码:

  1. # 6.4 - Draw clock
  2. font = pygame.font.Font(None, 24)
  3. survivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)+":"+str((90000-pygame.time.get_ticks())/1000%60).zfill(2), True, (0,0,0))
  4. textRect = survivedtext.get_rect()
  5. textRect.topright=[1275,5]
  6. screen.blit(survivedtext, textRect)

上面的代码使用了PyGame默认的大小为24的字体。这个字体用来显示时间信息。

  1. healthbar = pygame.image.load("image/healthbar.png")
  2. health = pygame.image.load("image/health.png")

接下来添加代码到#6.4部分后面:

  1. # 6.5 - Draw health bar
  2. screen.blit(healthbar, (5,5))
  3. for health1 in range(healthvalue):
  4. screen.blit(health, (health1+8,8))

这段代码首先画了一个全红色的生命值条。然后根据城堡的生命值往生命条里面添加绿色。

运行下代码,现在就会有计时和生命值条了。

11.赢或输

我们来加上胜利或者失败的条件。你可以通过终止主循环,进入胜利/失败的循环来实现它。你需要指出玩家是否胜利,并将其显示在屏幕上。

下面是一些胜利/失败的一些基本条件:

如果时间到了(90秒)那么:

  • 停止运行游戏
  • l设置游戏的输出

如果城堡被毁,那么:

  • 停止运行游戏
  • l设置游戏的输出

精确度是一直都需要计算的。

在game.py 结尾添加这些代码

  1. #10 - Win/Lose check
  2. if pygame.time.get_ticks()>=90000:
  3. running=0
  4. exitcode=1
  5. if healthvalue<=0:
  6. running=0
  7. exitcode=0
  8. if acc[1]!=0:
  9. accuracy=acc[0]*1.0/acc[1]*100
  10. else:
  11. accuracy=0
  12. # 11 - Win/lose display
  13. if exitcode==0:
  14. pygame.font.init()
  15. font = pygame.font.Font(None, 24)
  16. text = font.render("Accuracy: "+str(accuracy)+"%", True, (255,0,0))
  17. textRect = text.get_rect()
  18. textRect.centerx = screen.get_rect().centerx
  19. textRect.centery = screen.get_rect().centery+24
  20. screen.blit(defeat, (0,0))
  21. screen.blit(text, textRect)
  22. else:
  23. pygame.font.init()
  24. font = pygame.font.Font(None, 24)
  25. text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0))
  26. textRect = text.get_rect()
  27. textRect.centerx = screen.get_rect().centerx
  28. textRect.centery = screen.get_rect().centery+24
  29. screen.blit(victory, (0,0))
  30. screen.blit(text, textRect)
  31. while 1:
  32. for event in pygame.event.get():
  33. if event.type == pygame.QUIT:
  34. pygame.quit()
  35. exit(0)
  36. pygame.display.flip()

第一个ifb表达式是检查是否时间到了。第二个是检查城堡是否被摧毁了。第三个计算你的精准度。之后,一个if表达式是检查你是赢了还是输了,然后显示出相应的图片。

当然,如果你想在赢或输的时候显示图片,这些图片首先需要加载。所以在#3部分加上这些代码:

  1. defeat = pygame.image.load("image/defeat.png")
  2. victory = pygame.image.load("image/victory.png")

还有需要改的地方,把#4部分从:

  1. # 4 - keep looping through
  2. while 1:
  3. badtimer-=1

改成:

  1. # 4 - keep looping through
  2. running = 1
  3. exitcode = 0
  4. while running:
  5. badtimer-=1

这个running变量会跟踪游戏是否结束,exitcode变量会跟踪玩家是否胜利。

12.添加声音

在#2部分结尾加上这些代码:

pygame.mixer.init()

然后在#3部分加载声音然后设置声音:

  1. # 3.1 - Load audio
  2. hit = pygame.mixer.Sound("audio/explode.wav")
  3. enemy = pygame.mixer.Sound("audio/enemy.wav")
  4. shoot = pygame.mixer.Sound("audio/shoot.wav")
  5. hit.set_volume(0.05)
  6. enemy.set_volume(0.05)
  7. shoot.set_volume(0.05)
  8. #pygame.mixer.music.load("audio/moonlight.wav")
  9. pygame.mixer.music.load("audio/wangzhe.wav")
  10. pygame.mixer.music.play(-1, 0.0)
  11. pygame.mixer.music.set_volume(0.25)

上面大部分代码就是加载声音文件并且配置音量。但是注意 pygame.mixer.music.load这行代码——这一行加载游戏的背景音乐然后下一行让背景音乐一直不停的播放。

现在注意声音的配置。现在你所有需要做的就是在需要的时候播放不同的音乐。

  1. # section 6.3.1 after if badrect.left<64:
  2. hit.play()
  3. # section 6.3.2 after if badrect.colliderect(bullrect):
  4. enemy.play()
  5. # section 8, after if event.type==pygame.MOUSEBUTTONDOWN:
  6. shoot.play()

再次运行游戏,你会发现现在有背景音乐了,并且在射出箭头和碰撞的时候会有音效。

全部代码

  1. #妲己:512*528 156*156
  2. #娜可露露:156*156
  3. #蓝方防御塔128*128
  4. #红方防御塔:128*128
  5. # 1
  6. import pygame
  7. from pygame.locals import *
  8. import math
  9. import random
  10. # 2
  11. pygame.init()
  12. width,height = 1280,720
  13. screen=pygame.display.set_mode((width,height))
  14. keys = [False, False, False, False]
  15. #Hero_Blue_pos=[30,30]
  16. Hero_Blue_pos=[130,130]
  17. acc=[0,0]
  18. arrows=[]
  19. badtimer=100
  20. badtimer1=0
  21. badguys=[[640,100]]
  22. healthvalue=194
  23. pygame.mixer.init()
  24. # 3 加载图片
  25. #Blue_player = pygame.image.load("image/xiaodaji.png")
  26. bg_haidu = pygame.image.load("image/bg_haidu.jpg")
  27. Hero_Blue_ = pygame.image.load("image/Hero_Blue_.png")
  28. Hero_Red_ = pygame.image.load("image/Hero_Red_.png")
  29. Blue_Tower1 = pygame.image.load("image/Blue_Tower1.png")
  30. Blue_Tower2 = pygame.image.load("image/Blue_Tower2.png")
  31. Blue_Tower3 = pygame.image.load("image/Blue_Tower3.png")
  32. Red_tower1 = pygame.image.load("image/Red_tower1.png")
  33. Red_tower2 = pygame.image.load("image/Red_tower2.png")
  34. Red_tower3 = pygame.image.load("image/Red_tower3.png")
  35. arrow = pygame.image.load("image/bullet.png")
  36. badguyimg1 = pygame.image.load("image/badguy.png")
  37. badguyimg=badguyimg1
  38. healthbar = pygame.image.load("image/healthbar.png")
  39. health = pygame.image.load("image/health.png")
  40. fefeat = pygame.image.load("image/defeat.png")
  41. victory = pygame.image.load("image/victory.png")
  42. #添加声音
  43. # 3.1 - Load audio
  44. hit = pygame.mixer.Sound("audio/explode.wav")
  45. enemy = pygame.mixer.Sound("audio/enemy.wav")
  46. shoot = pygame.mixer.Sound("audio/shoot.wav")
  47. hit.set_volume(0.05)
  48. enemy.set_volume(0.05)
  49. shoot.set_volume(0.05)
  50. #pygame.mixer.music.load("audio/moonlight.wav")
  51. pygame.mixer.music.load("audio/wangzhe.wav")
  52. pygame.mixer.music.play(-1, 0.0)
  53. pygame.mixer.music.set_volume(0.25)
  54. #4
  55. running = 1
  56. exitcode = 0
  57. while running:
  58. badtimer-=1
  59. # 5
  60. screen.fill(0)
  61. # 6
  62. screen.blit(bg_haidu,(0,0))
  63. #screen.blit(Blue_player,(100,100))
  64. #大一点的小妲己(这个后面的图片会盖在前面的图片上
  65. screen.blit(Blue_Tower1,(33,227.25))
  66. screen.blit(Blue_Tower2,(33,391.5))
  67. screen.blit(Blue_Tower3,(33,555.75))
  68. screen.blit(Hero_Red_,(1119,30))
  69. screen.blit(Red_tower1,(1119,227.25))
  70. screen.blit(Red_tower2,(1119,391.5))
  71. screen.blit(Red_tower3,(1119,555.75))
  72. #screen.blit(Hero_Blue_,Hero_Blue_pos)
  73. # 6.1 - Set player position and rotation
  74. position = pygame.mouse.get_pos()
  75. angle = math.atan2(position[1]-(Hero_Blue_pos[1]+88),position[0]-(Hero_Blue_pos[0]+80.5))
  76. Hero_Blue_rot = pygame.transform.rotate(Hero_Blue_, 360-angle*57.29)
  77. Hero_Blue_pos1 = (Hero_Blue_pos[0]-Hero_Blue_rot.get_rect().width/2, Hero_Blue_pos[1]-Hero_Blue_rot.get_rect().height/2)
  78. screen.blit(Hero_Blue_rot, Hero_Blue_pos1)
  79. # 6.2 - Draw arrows
  80. for bullet in arrows:
  81. index=0
  82. velx=math.cos(bullet[0])*10 #速度
  83. vely=math.sin(bullet[0])*10
  84. bullet[1]+=velx
  85. bullet[2]+=vely
  86. #参数991表示不会射出塔
  87. if bullet[1]<-156 or bullet[1]>991 or bullet[2]<-156 or bullet[2]>720:
  88. arrows.pop(index)
  89. index+=1
  90. for projectile in arrows:
  91. arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29)
  92. screen.blit(arrow1, (projectile[1], projectile[2]))
  93. # 6.3 - Draw badgers
  94. if badtimer==0:
  95. badguys.append([1091, random.randint(191,529)])
  96. badtimer=100-(badtimer1*2)
  97. if badtimer1>=35:
  98. badtimer1=35
  99. else:
  100. badtimer1+=5
  101. index=0
  102. for badguy in badguys:
  103. # 6.3.1 - Attack castle
  104. badrect=pygame.Rect(badguyimg.get_rect())
  105. badrect.top=badguy[1]
  106. badrect.left=badguy[0]
  107. if badrect.left<64:
  108. hit.play()
  109. healthvalue -= random.randint(5,20)
  110. badguys.pop(index)
  111. #6.3.2 - Check for collisions
  112. index1=0
  113. for bullet in arrows:
  114. bullrect=pygame.Rect(arrow.get_rect())
  115. bullrect.left=bullet[1]
  116. bullrect.top=bullet[2]
  117. if badrect.colliderect(bullrect):
  118. enemy.play()
  119. acc[0]+=1
  120. badguys.pop(index)
  121. arrows.pop(index1)
  122. index1+=1
  123. # 6.3.3 - Next bad guy
  124. if badguy[0]<-64:
  125. badguys.pop(index)
  126. badguy[0]-=7
  127. index+=1
  128. for badguy in badguys:
  129. screen.blit(badguyimg, badguy)
  130. # 6.4 - Draw clock
  131. font = pygame.font.Font(None, 24)#使用PyGame默认的大小为24的字体
  132. survivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)[:1]+":"+str((90000-pygame.time.get_ticks())/1000%60)[:4].zfill(2), True, (255,255,255))
  133. #上一行代码使用字符串切片操作保留整数;(255,255,255)把颜色设置为白色
  134. textRect = survivedtext.get_rect()
  135. textRect.topright=[1250,5]
  136. screen.blit(survivedtext, textRect)
  137. # 6.5 - Draw health bar
  138. screen.blit(healthbar, (5,5))
  139. for health1 in range(healthvalue):
  140. screen.blit(health, (health1+8,8))
  141. # 7
  142. pygame.display.flip()
  143. # 8
  144. for event in pygame.event.get():
  145. #if event.type==pygame.QUIT:
  146. # pygame.quit()
  147. # exit(0)
  148. if event.type == pygame.KEYDOWN:
  149. if event.key==K_w:
  150. keys[0]=True
  151. elif event.key==K_a:
  152. keys[1]=True
  153. elif event.key==K_s:
  154. keys[2]=True
  155. elif event.key==K_d:
  156. keys[3]=True
  157. if event.type == pygame.KEYUP:
  158. if event.key==pygame.K_w:
  159. keys[0]=False
  160. elif event.key==pygame.K_a:
  161. keys[1]=False
  162. elif event.key==pygame.K_s:
  163. keys[2]=False
  164. elif event.key==pygame.K_d:
  165. keys[3]=False
  166. #点击鼠标射出箭头
  167. if event.type==pygame.MOUSEBUTTONDOWN:
  168. shoot.play()
  169. position=pygame.mouse.get_pos()
  170. acc[1]+=1
  171. arrows.append([math.atan2(position[1]-(Hero_Blue_pos1[1]+88),position[0]-(Hero_Blue_pos1[0]+80.5)),Hero_Blue_pos1[0]+88,Hero_Blue_pos1[1]+80.5])
  172. # 9 - Move player
  173. if keys[0]:
  174. Hero_Blue_pos[1]-=5
  175. elif keys[2]:
  176. Hero_Blue_pos[1]+=5
  177. if keys[1]:
  178. Hero_Blue_pos[0]-=5
  179. elif keys[3]:
  180. Hero_Blue_pos[0]+=5
  181. if event.type==pygame.QUIT:
  182. pygame.quit()
  183. exit(0)
  184. #10 - Win/Lose check
  185. if pygame.time.get_ticks()>=90000:
  186. running=0
  187. exitcode=1
  188. if healthvalue<=0:
  189. running=0
  190. exitcode=0
  191. if acc[1]!=0:
  192. accuracy=acc[0]*1.0/acc[1]*100
  193. accuracy = round(accuracy,2)#保留小数点后两位
  194. else:
  195. accuracy=0
  196. # 11 - Win/lose display
  197. if exitcode==0:
  198. pygame.font.init()
  199. font = pygame.font.Font(None, 24)
  200. text = font.render("Accuracy: "+str(accuracy)+"%", True, (255,0,0))
  201. textRect = text.get_rect()
  202. textRect.centerx = screen.get_rect().centerx
  203. textRect.centery = screen.get_rect().centery+24
  204. screen.blit(fefeat, (0,0))
  205. screen.blit(text, textRect)
  206. else:
  207. pygame.font.init()
  208. font = pygame.font.Font(None, 24)
  209. text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0))
  210. textRect = text.get_rect()
  211. textRect.centerx = screen.get_rect().centerx
  212. textRect.centery = screen.get_rect().centery+24
  213. screen.blit(victory, (0,0))
  214. screen.blit(text, textRect)
  215. while 1:
  216. for event in pygame.event.get():
  217. if event.type == pygame.QUIT:
  218. pygame.quit()
  219. exit(0)
  220. pygame.display.flip()

文件的话emmmmm……自己找吧

 

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

闽ICP备14008679号