Pygame游戏之 飞船绕行

这是一个让飞船绕着地球的小游戏
在这里插入图片描述

我们得先知道如何加载图片和把图片显示在我们的窗口屏幕上
1、加载位图:space = pygame.image.load(“space.png”)
支持的文件类型有jpg,png,gif,bmp,pcx,tga,tif,lbm,pbm,pgm,ppm,xpm
2、绘制背景
screen.blit(space,(0,0)) 图片的左上角位于0,0位置

import pygame,sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("飞船绕行")

space = pygame.image.load("space.png")

while True:
	for event in pygame.event.get():
		if event.type == QUIT:
			sys.exit()
		elif event.type == KEYDOWN:
			if event.key == K_ESCAPE:
				sys.exit()
	screen.blit(space,(0,0))
	pygame.display.update()

在这里插入图片描述第二步同理绘制地球
我们可以使用get_size()获取图片的宽度,高度,方便我们在程序中调整大小,而不是通过绘图软件
如:
planet = pygame.image.load(“planet.png”)
width,height = planet.get_size()
screen.blit(planet,(400-width/2,300-height/2))

绘制飞船
我们可以使用缩放函数,把飞船调整为合适的大小
pygame.transform.smoothscale()

设置环绕地球的轨道
我们让飞船绕行星轨道飞行,并且让其在飞行的过程中使自身旋转,保持正面总是指向其移动的方向


import sys, random, math, pygame
from pygame.locals import *

#显示飞船的坐标
class Point(object):
    def __init__(self, x, y):
        self.__x = x
        self.__y = y

    #X property
    def getx(self): return self.__x
    def setx(self, x): self.__x = x
    x = property(getx, setx)

    #Y property
    def gety(self): return self.__y
    def sety(self, y): self.__y = y
    y = property(gety, sety)

    def __str__(self):
        return "{X:" + "{:.0f}".format(self.__x) + \
            ",Y:" + "{:.0f}".format(self.__y) + "}"
    
#绘制文本内容的函数
def print_text(font, x, y, text, color=(255,255,255)):
    imgText = font.render(text, True, color)
    screen.blit(imgText, (x,y))

#改变角度
def wrap_angle(angle):
    return angle % 360


pygame.init()								#初始化游戏
screen = pygame.display.set_mode((800,600))	#设置窗口大小
pygame.display.set_caption("Orbit Demo")	#设置标题
font = pygame.font.Font(None, 18)			#设置字体

#load bitmaps
space = pygame.image.load("space.png").convert_alpha()		#加载背景图片
planet = pygame.image.load("planet2.png").convert_alpha()	#加载地球图片
ship = pygame.image.load("freelance.png").convert_alpha()	#加载飞船图片
width,height = ship.get_size()								#获取飞船的宽度,高度
ship = pygame.transform.smoothscale(ship, (width//2, height//2))	#缩放飞船

radius = 250	#轨道的半径
angle = 0.0		#初始角度
pos = Point(0,0)	#记录飞船的位置	
old_pos = Point(0,0)

#repeating loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()

    #draw background
    screen.blit(space, (0,0))		#绘制背景

    #draw planet
    width,height = planet.get_size()	#绘制地球
    screen.blit(planet, (400-width/2,300-height/2))

    #move the ship	
    angle = wrap_angle(angle - 0.1)		#让飞船在圆周轨道上移动
    pos.x = math.sin( math.radians(angle) ) * radius
    pos.y = math.cos( math.radians(angle) ) * radius

    #rotate the ship
    delta_x = ( pos.x - old_pos.x )		#让飞船旋转
    delta_y = ( pos.y - old_pos.y )
    rangle = math.atan2(delta_y, delta_x)
    rangled = wrap_angle( -math.degrees(rangle) )
    scratch_ship = pygame.transform.rotate(ship, rangled)

    #draw the ship
    width,height = scratch_ship.get_size()
    x = 400+pos.x-width//2
    y = 300+pos.y-height//2
    screen.blit(scratch_ship, (x,y))

    print_text(font, 0, 0, "Orbit: " + "{:.0f}".format(angle))
    print_text(font, 0, 20, "Rotation: " + "{:.2f}".format(rangle))
    print_text(font, 0, 40, "Position: " + str(pos))
    print_text(font, 0, 60, "Old Pos: " + str(old_pos))
    
    pygame.display.update()
    
    #remember position
    old_pos.x = pos.x		#不断让坐标进行变化
    old_pos.y = pos.y
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值