The second homework in the sixth week of Python

This chapter starts to learn pygame

12-1 Blue Sky

At first, I wrote it completely based on the example in the book, but I deleted the unnecessary code in 12-1. As a result, the Pygame window did not respond. After checking, I found out that it was because I deleted the code that monitors the input. , thus not responding to my external input...

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 Game Characters

Just find a picture, but I don't know how to set the screen background color to the image background color!

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 Rockets

Due to the relatively small number of homework operations, I did not divide too many projects

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()

The result is normal operation:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520667&siteId=291194637