当前位置:   article > 正文

python从入门到实践 第12章 武装飞船 之 调整飞船的速度程序实践_python编程飞船速度代码

python编程飞船速度代码

第一个文件,main.py是主程序 

  1. #main.py
  2. import sys
  3. import pygame
  4. from settings import Settings
  5. from ship import Ship
  6. import update as pt
  7. def run_game():
  8. my_set = Settings()
  9. pygame.init()
  10. screen = pygame.display.set_mode(my_set.set_mode)
  11. my_ship = Ship(screen,my_set)
  12. pygame.display.set_caption(my_set.set_caption)
  13. while True:
  14. pt.update(my_ship,screen,my_set)
  15. run_game()

第二个文件:settings.py

  1. class Settings():
  2. def __init__(self):
  3. self.color = (255,255,255)
  4. self.set_mode = (1000,800)
  5. self.new_speed_factor = 1.5
  6. self.set_caption = 'ship invasion'

第三个文件:update.py

  1. #update.py
  2. import pygame
  3. import sys
  4. def check_events(ship):
  5. for event in pygame.event.get():
  6. if event.type == pygame.QUIT:
  7. sys.exit()
  8. elif event.type == pygame.KEYDOWN:
  9. if event.key == pygame.K_RIGHT:
  10. ship.moving_right = True
  11. if event.key == pygame.K_LEFT:
  12. ship.moving_left = True
  13. if event.key == pygame.K_UP:
  14. ship.moving_up = True
  15. if event.key == pygame.K_DOWN:
  16. ship.moving_down = True
  17. elif event.type == pygame.KEYUP:
  18. if event.key == pygame.K_RIGHT:
  19. ship.moving_right = False
  20. if event.key == pygame.K_LEFT:
  21. ship.moving_left = False
  22. if event.key == pygame.K_UP:
  23. ship.moving_up = False
  24. if event.key == pygame.K_DOWN:
  25. ship.moving_down = False
  26. def update(ship,screen,settings):
  27. check_events(ship)
  28. ship.update()
  29. #特别要注意:填充颜色的下面这句话一定要在绘图之前进行(也就是要放在ship.blitme之前,否则
  30. #颜色会覆盖掉前面的绘好的图形)
  31. screen.fill(settings.color)
  32. ship.blitme()
  33. pygame.display.flip()

第四个文件:ship.py

  1. import pygame
  2. import sys
  3. class Ship():
  4. def __init__(self,screen,settings):
  5. self.settings = settings
  6. self.screen = screen
  7. self.image = pygame.image.load("ship.bmp")
  8. self.rect = self.image.get_rect()
  9. self.screen_rect = self.screen.get_rect()
  10. self.rect.centerx = self.screen_rect.centerx
  11. self.rect.bottom = self.screen_rect.bottom
  12. self.center = float(self.rect.centerx)
  13. self.moving_left = False
  14. self.moving_right = False
  15. self.moving_up = False
  16. self.moving_down = False
  17. def blitme(self):
  18. self.screen.blit(self.image,self.rect)
  19. def update(self):
  20. if self.moving_right == True:
  21. self.center += self.settings.new_speed_factor
  22. if self.moving_left == True:
  23. self.center -= self.settings.new_speed_factor
  24. if self.moving_up == True:
  25. self.rect.bottom -= 1
  26. if self.moving_down == True:
  27. self.rect.bottom += 1
  28. self.rect.centerx = self.center

本文所用到的ship.bmp和alien.bmp的下载   链接: https://pan.baidu.com/s/1Dn5yaQGZLIsN0BohM8PzoA  密码: 38ie

 

注:以上程序只是提高了横向的速度,纵向移动速度并没有改变,如果要改变纵向移动速度,必须改变ship.py文件,自己改动的代码如下(其它文件不变):

  1. import pygame
  2. import sys
  3. class Ship():
  4. def __init__(self,screen,settings):
  5. self.settings = settings
  6. self.screen = screen
  7. self.image = pygame.image.load("ship.bmp")
  8. self.rect = self.image.get_rect()
  9. self.screen_rect = self.screen.get_rect()
  10. self.rect.centerx = self.screen_rect.centerx
  11. self.rect.bottom = self.screen_rect.bottom
  12. self.center_x = float(self.rect.centerx)
  13. self.center_y = float(self.rect.bottom)
  14. self.moving_left = False
  15. self.moving_right = False
  16. self.moving_up = False
  17. self.moving_down = False
  18. def blitme(self):
  19. self.screen.blit(self.image,self.rect)
  20. def update(self):
  21. if self.moving_right == True:
  22. self.center_x += self.settings.new_speed_factor
  23. if self.moving_left == True:
  24. self.center_x -= self.settings.new_speed_factor
  25. if self.moving_up == True:
  26. self.center_y -= self.settings.new_speed_factor
  27. if self.moving_down == True:
  28. self.center_y += self.settings.new_speed_factor
  29. self.rect.centerx = self.center_x
  30. self.rect.bottom = self.center_y

随着代码越来越长,我们队update.py文件进行重构,把check_events()的部分代码放在不同的文件中,update.py文件重新布置代码如下:

  1. import pygame
  2. import sys
  3. def check_keydown_events(event,ship):
  4. if event.type == pygame.KEYDOWN:
  5. if event.key == pygame.K_RIGHT:
  6. ship.moving_right = True
  7. if event.key == pygame.K_LEFT:
  8. ship.moving_left = True
  9. if event.key == pygame.K_UP:
  10. ship.moving_up = True
  11. if event.key == pygame.K_DOWN:
  12. ship.moving_down = True
  13. def check_keyup_events(event,ship):
  14. if event.type == pygame.KEYUP:
  15. if event.key == pygame.K_RIGHT:
  16. ship.moving_right = False
  17. if event.key == pygame.K_LEFT:
  18. ship.moving_left = False
  19. if event.key == pygame.K_UP:
  20. ship.moving_up = False
  21. if event.key == pygame.K_DOWN:
  22. ship.moving_down = False
  23. def check_events(ship):
  24. for event in pygame.event.get():
  25. if event.type == pygame.QUIT:
  26. sys.exit()
  27. elif event.type == pygame.KEYDOWN:
  28. #因为上面已经定义过函数check_keydown_events,所以可以直接使用
  29. check_keydown_events(event,ship)
  30. elif event.type == pygame.KEYUP:
  31. #因为上面已经定义过函数check_up_events,所以下面也可以直接使用
  32. check_keyup_events(event,ship)
  33. def update(ship,screen,settings):
  34. check_events(ship)
  35. ship.update()
  36. screen.fill(settings.color)
  37. ship.blitme()
  38. pygame.display.flip()

注意:在update.py文件中,的update函数里,screen.fill函数一定要放在blitme函数之前,否则颜色会覆盖掉绘好的图形,也就是尽早填充颜色

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

闽ICP备14008679号