赞
踩
第一个文件,main.py是主程序
- #main.py
- import sys
- import pygame
- from settings import Settings
- from ship import Ship
- import update as pt
- def run_game():
- my_set = Settings()
-
- pygame.init()
- screen = pygame.display.set_mode(my_set.set_mode)
- my_ship = Ship(screen,my_set)
- pygame.display.set_caption(my_set.set_caption)
- while True:
- pt.update(my_ship,screen,my_set)
- run_game()
第二个文件:settings.py
- class Settings():
- def __init__(self):
- self.color = (255,255,255)
- self.set_mode = (1000,800)
- self.new_speed_factor = 1.5
- self.set_caption = 'ship invasion'
第三个文件:update.py
- #update.py
- import pygame
- import sys
- def check_events(ship):
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RIGHT:
- ship.moving_right = True
- if event.key == pygame.K_LEFT:
- ship.moving_left = True
- if event.key == pygame.K_UP:
- ship.moving_up = True
- if event.key == pygame.K_DOWN:
- ship.moving_down = True
- elif event.type == pygame.KEYUP:
- if event.key == pygame.K_RIGHT:
- ship.moving_right = False
- if event.key == pygame.K_LEFT:
- ship.moving_left = False
- if event.key == pygame.K_UP:
- ship.moving_up = False
- if event.key == pygame.K_DOWN:
- ship.moving_down = False
- def update(ship,screen,settings):
- check_events(ship)
- ship.update()
- #特别要注意:填充颜色的下面这句话一定要在绘图之前进行(也就是要放在ship.blitme之前,否则
- #颜色会覆盖掉前面的绘好的图形)
- screen.fill(settings.color)
- ship.blitme()
- pygame.display.flip()
第四个文件:ship.py
- import pygame
- import sys
- class Ship():
- def __init__(self,screen,settings):
- self.settings = settings
- self.screen = screen
- self.image = pygame.image.load("ship.bmp")
- self.rect = self.image.get_rect()
- self.screen_rect = self.screen.get_rect()
-
- self.rect.centerx = self.screen_rect.centerx
- self.rect.bottom = self.screen_rect.bottom
- self.center = float(self.rect.centerx)
- self.moving_left = False
- self.moving_right = False
- self.moving_up = False
- self.moving_down = False
- def blitme(self):
- self.screen.blit(self.image,self.rect)
- def update(self):
- if self.moving_right == True:
- self.center += self.settings.new_speed_factor
- if self.moving_left == True:
- self.center -= self.settings.new_speed_factor
- if self.moving_up == True:
- self.rect.bottom -= 1
- if self.moving_down == True:
- self.rect.bottom += 1
- self.rect.centerx = self.center
本文所用到的ship.bmp和alien.bmp的下载 链接: https://pan.baidu.com/s/1Dn5yaQGZLIsN0BohM8PzoA 密码: 38ie
- import pygame
- import sys
- class Ship():
- def __init__(self,screen,settings):
- self.settings = settings
- self.screen = screen
- self.image = pygame.image.load("ship.bmp")
- self.rect = self.image.get_rect()
- self.screen_rect = self.screen.get_rect()
-
- self.rect.centerx = self.screen_rect.centerx
- self.rect.bottom = self.screen_rect.bottom
- self.center_x = float(self.rect.centerx)
- self.center_y = float(self.rect.bottom)
- self.moving_left = False
- self.moving_right = False
- self.moving_up = False
- self.moving_down = False
- def blitme(self):
- self.screen.blit(self.image,self.rect)
- def update(self):
- if self.moving_right == True:
- self.center_x += self.settings.new_speed_factor
- if self.moving_left == True:
- self.center_x -= self.settings.new_speed_factor
- if self.moving_up == True:
- self.center_y -= self.settings.new_speed_factor
- if self.moving_down == True:
- self.center_y += self.settings.new_speed_factor
- self.rect.centerx = self.center_x
- self.rect.bottom = self.center_y
随着代码越来越长,我们队update.py文件进行重构,把check_events()的部分代码放在不同的文件中,update.py文件重新布置代码如下:
- import pygame
- import sys
- def check_keydown_events(event,ship):
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RIGHT:
- ship.moving_right = True
- if event.key == pygame.K_LEFT:
- ship.moving_left = True
- if event.key == pygame.K_UP:
- ship.moving_up = True
- if event.key == pygame.K_DOWN:
- ship.moving_down = True
- def check_keyup_events(event,ship):
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_RIGHT:
- ship.moving_right = False
- if event.key == pygame.K_LEFT:
- ship.moving_left = False
- if event.key == pygame.K_UP:
- ship.moving_up = False
- if event.key == pygame.K_DOWN:
- ship.moving_down = False
- def check_events(ship):
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- elif event.type == pygame.KEYDOWN:
- #因为上面已经定义过函数check_keydown_events,所以可以直接使用
- check_keydown_events(event,ship)
- elif event.type == pygame.KEYUP:
- #因为上面已经定义过函数check_up_events,所以下面也可以直接使用
- check_keyup_events(event,ship)
- def update(ship,screen,settings):
- check_events(ship)
- ship.update()
- screen.fill(settings.color)
- ship.blitme()
- pygame.display.flip()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。