###进击的坦克###
import pygame
import sys
from pygame.locals import *
#初始化Pygame
pygame.init()
size = width, height = 600, 400
speed = [-2, 0]
bg = (255, 255, 255)
fullscreen = False
#pygame.time.Clock()定义图像移动的帧率
clock = pygame.time.Clock()
#创建指定大小的窗口Surface
screen = pygame.display.set_mode(size, RESIZABLE)
#设置窗口标题
pygame.display.set_caption("进击的坦克!")
#加载坦克图片
tanks = pygame.image.load("tank.png")
#获得图像的位置矩形
position = tanks.get_rect()
l_head = tanks
r_head = pygame.transform.flip(tanks, True, False)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
tanks = l_head
speed = [-1, 0]
if event.key == K_RIGHT:
tanks = r_head
speed = [1, 0]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]
#全屏F11
if event.key == K_F11:
fullscreen = not fullscreen
if fullscreen:
#获取用户端的最大分辨率
pygame.init()#初始化
screen_c = pygame.display.list_modes()#获取当前显示器分辨率的列表,并赋值给screen_c
screen = pygame.display.set_mode(screen_c[0], FULLSCREEN | HWSURFACE)#screen_c[0]调用第一个分辨率
else:
screen = pygame.display.set_mode(size)
screen.blit(tanks,(0,0))####初始化图片位置
position = tanks.get_rect()###获得初始化位置后的矩形位置
#用户调整窗口尺寸
if event.type == VIDEORESIZE:
screen.blit(tanks,(0,0))####初始化图片位置
position = tanks.get_rect()###获得初始化位置后的矩形位置
size = event.size
width, hight = size
print(size)
screen = pygame.display.set_mode(size, RESIZABLE)
#移动图像不能跨越定义好的边框
position = position.move(speed)
width, height = screen.get_size()#获取当前SIZE,让坦克的活动范围跟随边框变化
if position.left < 0 or position.right > width:
#翻转图像
tanks = pygame.transform.flip(tanks, True, False)
#反向移动
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
#填充背景
screen.fill(bg)
#更新图像
screen.blit(tanks, position)
#更新界面
pygame.display.flip()
#延迟10毫秒
#pygame.time.delay(10)
clock.tick(100)
import pygame
import sys
from pygame.locals import *
#初始化Pygame
pygame.init()
size = width, height = 600, 400
speed = [-2, 0]
bg = (255, 255, 255)
fullscreen = False
#pygame.time.Clock()定义图像移动的帧率
clock = pygame.time.Clock()
#创建指定大小的窗口Surface
screen = pygame.display.set_mode(size, RESIZABLE)
#设置窗口标题
pygame.display.set_caption("进击的坦克!")
#加载坦克图片
tanks = pygame.image.load("tank.png")
#获得图像的位置矩形
position = tanks.get_rect()
l_head = tanks
r_head = pygame.transform.flip(tanks, True, False)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
tanks = l_head
speed = [-1, 0]
if event.key == K_RIGHT:
tanks = r_head
speed = [1, 0]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]
#全屏F11
if event.key == K_F11:
fullscreen = not fullscreen
if fullscreen:
#获取用户端的最大分辨率
pygame.init()#初始化
screen_c = pygame.display.list_modes()#获取当前显示器分辨率的列表,并赋值给screen_c
screen = pygame.display.set_mode(screen_c[0], FULLSCREEN | HWSURFACE)#screen_c[0]调用第一个分辨率
else:
screen = pygame.display.set_mode(size)
screen.blit(tanks,(0,0))####初始化图片位置
position = tanks.get_rect()###获得初始化位置后的矩形位置
#用户调整窗口尺寸
if event.type == VIDEORESIZE:
screen.blit(tanks,(0,0))####初始化图片位置
position = tanks.get_rect()###获得初始化位置后的矩形位置
size = event.size
width, hight = size
print(size)
screen = pygame.display.set_mode(size, RESIZABLE)
#移动图像不能跨越定义好的边框
position = position.move(speed)
width, height = screen.get_size()#获取当前SIZE,让坦克的活动范围跟随边框变化
if position.left < 0 or position.right > width:
#翻转图像
tanks = pygame.transform.flip(tanks, True, False)
#反向移动
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
#填充背景
screen.fill(bg)
#更新图像
screen.blit(tanks, position)
#更新界面
pygame.display.flip()
#延迟10毫秒
#pygame.time.delay(10)
clock.tick(100)