标签:
# -*- coding: utf-8 -*- import pygame from sys import exit class Bullet: def __init__(self): #初始化成员变量,x,y,image self.x = 0 self.y = -1 self.image = pygame.image.load(‘bullet.png‘).convert_alpha() def move(self): #处理子弹的运动 if self.y < 0: mouseX, mouseY = pygame.mouse.get_pos() self.x = mouseX - self.image.get_width() / 2 self.y = mouseY - self.image.get_height() / 2 else: self.y -= 5 pygame.init() screen = pygame.display.set_mode((450, 800), 0, 32) pygame.display.set_caption("Hello, World!") background = pygame.image.load(‘back.jpg‘).convert() plane = pygame.image.load(‘plane.png‘).convert_alpha() bullet = Bullet() #加载子弹图像 #初始化子弹位置 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() screen.blit(background, (0,0)) bullet.move() screen.blit(bullet.image, (bullet.x, bullet.y)) #把子弹画到屏幕上 x, y = pygame.mouse.get_pos() x-= plane.get_width() / 2 y-= plane.get_height() / 2 screen.blit(plane, (x, y)) pygame.display.update()
# -*- coding: utf-8 -*- import pygame #导入pygame库 from sys import exit #向sys模块借一个exit函数用来退出程序 pygame.init() #初始化pygame,为使用硬件做准备 screen = pygame.display.set_mode((600, 170), 0, 32) #创建了一个窗口,窗口大小和背景图片大小一样 pygame.display.set_caption("Hello, World!") #设置窗口标题 background = pygame.image.load(‘0.jpg‘).convert() #加载并转换图像 plane = pygame.image.load(‘2.jpg‘).convert() while True: #游戏主循环 for event in pygame.event.get(): if event.type == pygame.QUIT: #接收到退出事件后退出程序 pygame.quit() exit() screen.blit(background, (0,0)) x, y = pygame.mouse.get_pos() #获取鼠标位置 x-= plane.get_width() / 2 y-= plane.get_height() / 2 #计算飞机的左上角位置 screen.blit(plane, (x,y)) #把飞机画到屏幕上 pygame.display.update() #刷新一下画面
# -*- coding: utf-8 -*- import pygame from sys import exit pygame.init() screen = pygame.display.set_mode((450, 800), 0, 32) pygame.display.set_caption("Hello, World!") background = pygame.image.load(‘back.jpg‘).convert() plane = pygame.image.load(‘plane.png‘).convert_alpha() bullet = pygame.image.load(‘bullet.png‘).convert_alpha() #加载子弹图像 bullet_x = 0 bullet_y = -1 #初始化子弹位置 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() screen.blit(background, (0,0)) x, y = pygame.mouse.get_pos() if bullet_y < 0: #如果子弹位置超出了屏幕上端 bullet_x = x - bullet.get_width() / 2 bullet_y = y - bullet.get_height() / 2 #把子弹的中心位置设为鼠标坐标 else: bullet_y -= 5 #子弹的位置往上移 screen.blit(bullet, (bullet_x, bullet_y)) #把子弹画到屏幕上 x-= plane.get_width() / 2 y-= plane.get_height() / 2 screen.blit(plane, (x, y)) pygame.display.update()
标签:
原文地址:http://www.cnblogs.com/zhuyaguang/p/4612757.html