Python第六周第二次作业

本章开始学习pygame

12-1 蓝色天空

一开始我完全是根据书上的例子写的,然而将12-1不需要的代码删除,结果Pygame窗口发生了未响应的状况,检查后得知是因为我将监视输入的代码也删掉了,因此无法响应我的外部输入……

import sys  #use this module to quit
import pygame

def run_game():
    #initialize the background
    pygame.init()
    #create a window
    screen = pygame.display.set_mode((500, 500))
    #set window title
    pygame.display.set_caption("BLUE!")
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            
        screen.fill((35, 70, 188))
        pygame.display.flip()

run_game()

12-2 游戏角色

随便找一张图好了,可是我并不知道要如何将屏幕背景色设置为图像背景色!

import pygame 

class Character():
    def __init__(self, screen):
        #get the screen
        self.screen = screen
        #load the image
        self.image = pygame.image.load('HTF.bmp')
        #get the object: rect
        #rect has parameters: center, centerx, centery
        #                     top, bottom, left, right
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        #put the ship on the bottom
        self.rect.center = self.screen_rect.center
        
    #draw the image
    def blitme(self):
        self.screen.blit(self.image, self.rect)
import sys  #use this module to quit
import pygame
from character import Character

def run_game():
    #initialize the background
    pygame.init()
    #create a window
    screen = pygame.display.set_mode((236, 264))
    #set window title
    pygame.display.set_caption("None")
    char = Character(screen)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            
        screen.fill((255, 194, 136))
        char.blitme()
        pygame.display.flip()
        

run_game()

12-3 火箭

由于作业操作比较少,于是我没有分太多的项目

import pygame 

class Character():
    def __init__(self, screen):
        #get the screen
        self.screen = screen
        #load the image
        self.image = pygame.image.load('rocket.bmp')
        #get the object: rect
        #rect has parameters: center, centerx, centery
        #                     top, bottom, left, right
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        #put the ship on the bottom
        self.rect.center = self.screen_rect.center
        
        #moving symbol
        self.moving_right = False
        self.moving_left = False
        self.moving_top = False
        self.moving_bottom = False
    
    def update(self):
        #check moving symbol
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.rect.centerx += 1
        if self.moving_left and self.rect.left > 0:
            self.rect.centerx -= 1
        if self.moving_top and self.rect.top > 0:
            self.rect.centery -= 1
        if self.moving_bottom and self.rect.bottom < self.screen_rect.bottom:
            self.rect.centery += 1
        
    #draw the image
    def blitme(self):
        self.screen.blit(self.image, self.rect)
import sys  #use this module to quit
import pygame
from character import Character

def run_game():
    #initialize the background
    pygame.init()
    #create a window
    screen = pygame.display.set_mode((500, 500))
    #set window title
    pygame.display.set_caption("None")
    char = Character(screen)
    
    while True:
        #spy the keyboard and mouse to quit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            #if put down one key
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    char.moving_right = True
                elif event.key == pygame.K_LEFT:
                    char.moving_left = True
                elif event.key == pygame.K_UP:
                    char.moving_top = True
                elif event.key == pygame.K_DOWN:
                    char.moving_bottom = True
            #if release this key
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    char.moving_right = False
                elif event.key == pygame.K_LEFT:
                    char.moving_left = False
                elif event.key == pygame.K_UP:
                    char.moving_top = False
                elif event.key == pygame.K_DOWN:
                    char.moving_bottom = False
        
        char.update()    
        screen.fill((255, 255, 255))
        char.blitme()
        pygame.display.flip()
        

run_game()

结果是正常运行:


猜你喜欢

转载自blog.csdn.net/baidu_40392316/article/details/79942718
今日推荐