四项pygame程序测试

最后都变了- 提交于 2020-03-16 17:22:06

某厂面试归来,发现自己落伍了!>>>

本次进行五项pygame的程序代码测试

测试1(简单的hello world运行框显示)

代码如下:

import pygame
from pygame.locals import *
import sys
def hello_w():
    pygame.init()
    pygame.display.set_mode((680,480))
    pygame.display.set_caption('Hello World!')
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.update()
if __name__ == '__main__':
    hello_w()

运行结果:

测试2 (坦克能进行移动(自由移动))

代码如下:

import pygame,sys
from pygame.locals import *
def play_tank():
    pygame.init()
    window_size = (width,height)=(640,480)
    speed = [1,1]
    color_white = (255,255,255)
    screen = pygame.display.set_mode(window_size)
    pygame.display.set_caption("坦克大战")
    tank_image = pygame.image.load('tankU.bmp')
    tank_rect = tank_image.get_rect()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        tank_rect = tank_rect.move(speed)
        if(tank_rect.left<0) or (tank_rect.right>width):
            speed[0] = -speed[0]
        if(tank_rect.top<0) or (tank_rect.bottom>height):
            speed[1] = -speed[1]
        screen.fill(color_white)
        screen.blit(tank_image,tank_rect)
        pygame.display.update()
if __name__ =='__main__':
    play_tank()

运行结果:

测试3 (让坦克能根据操作进行移动)

代码如下:

import os,sys,pygame
from pygame.locals import *
def control_tank(event):
    speed = [x,y] = [0,0]
    speed_offset = 10
    if event.type ==pygame.KEYDOWN:
        if event.key == pygame.K_a:
            speed[0] -= speed_offset
        if event.key == pygame.K_d:
            speed[0] = speed_offset
        if event.key == pygame.K_w:
            speed[1] -= speed_offset
        if event.key == pygame.K_s:
            speed[1] = speed_offset
    if event.type == pygame.KEYUP:
        if event.type in [pygame.K_a,pygame.K_d,pygame.K_w,pygame.K_s]:
            speed = [0,0]
    return speed
def play_tank():
    pygame.init()
    windows_size = Rect(0,0,640,480)
    speed = [1,1]
    color_whiht = (255,255,255)
    screen = pygame.display.set_mode(windows_size.size)
    pygame.display.set_caption('坦克大战')
    tank_image = pygame.image.load('tankU.bmp')
    back_image = pygame.image.load('back_image.jpg')
    tank_rect = tank_image.get_rect()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        cur_speed = control_tank(event)
        tank_rect = tank_rect.move(cur_speed).clamp(windows_size)
        screen.blit(back_image,(0,0))
        screen.blit(tank_image,tank_rect)
        pygame.display.update()
if __name__ == '__main__':
    play_tank()

运行结果:

测试4 (让人物能自动展示不同动作)

代码如下:

import pygame
from pygame.locals import *
class MySprite(pygame.sprite.Sprite):
    def __init__(self,target):
        pygame.sprite.Sprite.__init__(self)
        self.sprite_surface=target
        self.image=None
        self.master_image=None
        self.rect=None
        self.topleft=0,0
        self.frame=0
        self.old_fram=-1
        self.fram_width=1
        self.fram_height=1
        self.first_fram=0
        self.last_fram=0
        self.columns=1
        self.last_time=0
    def load(self,filename,width,height,columns):
        self.master_image=pygame.image.load(filename).convert_alpha()
        self.fram_width=width
        self.fram_height=height
        self.rect=0,0,width,height
        self.columns=columns
        rect=self.master_image.get_rect()
        self.last_fram=(rect.width//width)*(rect.height//height)-1
    def update(self,current_time,rate=60):
        if current_time>self.last_time+rate:
            self.frame+=1
        if self.frame>self.last_fram:
            self.frame=self.first_fram
            self.last_time=current_time
        if self.frame!=self.old_fram:
            frame_x=(self.frame%self.columns)*self.fram_width
            frame_y=(self.frame//self.columns)*self.fram_height
            rect=(frame_x,frame_y,self.fram_width,self.fram_height)
            self.image=self.master_image.subsurface(rect)
            self.old_fram=self.frame

pygame.init()
screen=pygame.display.set_mode((400,300),0,32)
pygame.display.set_caption("精灵类测试")
font=pygame.font.Font(None,18)
framerate=pygame.time.Clock()
cat=MySprite(screen)
cat.load("sprite2.png",92,95,4)
group=pygame.sprite.Group()
group.add(cat)
while True:
    framerate.tick(10)
    ticks=pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            exit()
        key=pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            exit()
    screen.fill((0,0,255))
    cat.update(ticks)
    screen.blit(cat.image,cat.rect)
    pygame.display.update()

运行结果(随机截取两张图):

至此本次测试结束
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!