设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 59047|回复: 1
打印 上一主题 下一主题

【Pygame 第5课】 游戏中的运动

[复制链接]

6

主题

0

好友

166

积分

注册会员

Rank: 2

楼主
发表于 2018-11-13 16:59:55 |显示全部楼层
  1. # !/usr/bin/python
  2. import pygame
  3. from sys import exit

  4. pygame.init()
  5. screen = pygame.display.set_mode((450,800),0,32)
  6. pygame.display.set_caption('plane')

  7. background = pygame.image.load('back.jpg').convert()
  8. plane = pygame.image.load('plane.png').convert_alpha()
  9. bullet = pygame.image.load('bullet.png').convert_alpha()
  10. bullet1=bullet
  11. #加载子弹图像

  12. width=plane.get_width()/2
  13. height=plane.get_height()/2

  14. bullet_x=0
  15. bullet_y=-1
  16. bullet1_x=0
  17. bullet1_y=-1

  18. #初始化子弹位置
  19. while True:
  20.     for event in pygame.event.get():
  21.         if event.type == pygame.QUIT:
  22.             pygame.quit()
  23.             exit()
  24.     screen.blit(background, (0,0))

  25.     x, y = pygame.mouse.get_pos()
  26.     if bullet_y <0:
  27.         #如果子弹位置超出了屏幕上端
  28.         bullet_x = x - bullet.get_width() / 2 -23
  29.         bullet_y = y - bullet.get_height() / 2
  30.         bullet1_x = x - bullet.get_width() / 2 + 25
  31.         bullet1_y = y - bullet.get_height() / 2
  32.         #把子弹的中心位置设为鼠标坐标
  33.     else:
  34.         bullet_y -= 5
  35.         bullet_y -= 5
  36.         #子弹位置上移
  37.     screen.blit(bullet,(bullet_x,bullet_y))
  38.     screen.blit(bullet1,(bullet1_x,bullet1_y))
  39.     #把子弹画到屏幕上
  40.     if y>=800-height:
  41.         if x<width:
  42.             screen.blit(plane,(0,800-2*height))
  43.         elif x>450-width:
  44.             screen.blit(plane,(450-2*width,800-2*height))
  45.         else:
  46.             screen.blit(plane,(x-width,800-2*height))
  47.     elif y<height:
  48.         if x<width:
  49.             screen.blit(plane,(0,0))
  50.         elif x>450-width:
  51.             screen.blit(plane,(450-2*width,0))
  52.         else:
  53.             screen.blit(plane,(x-width,0))
  54.     else:
  55.         if x<width:
  56.             screen.blit(plane,(0,y-height))
  57.         elif x>450-width:
  58.             screen.blit(plane,(450-2*width,y-height))
  59.         else:
  60.             screen.blit(plane,(x-width,y-height))  
  61.     pygame.display.update()
复制代码
老师,为什么我左右两边的子弹的射速不一样,左边的快
回复

使用道具 举报

6

主题

0

好友

166

积分

注册会员

Rank: 2

沙发
发表于 2018-11-14 15:25:44 |显示全部楼层
crossin先生 发表于 2018-11-13 20:23
else:
        bullet_y -= 5
        bullet_y -= 5

粗心了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-18 16:01 , Processed in 0.016146 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部