生活随笔
收集整理的這篇文章主要介紹了
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
= xself
.__y
= y
def getx(self
): return self
.__x
def setx(self
, x
): self
.__x
= xx
= property(getx
, setx
)def gety(self
): return self
.__y
def sety(self
, y
): self
.__y
= yy
= 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
% 360pygame
.init
()
screen
= pygame
.display
.set_mode
((800,600))
pygame
.display
.set_caption
("Orbit Demo")
font
= pygame
.font
.Font
(None, 18)
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)
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
()screen
.blit
(space
, (0,0)) width
,height
= planet
.get_size
() screen
.blit
(planet
, (400-width
/2,300-height
/2))angle
= wrap_angle
(angle
- 0.1) pos
.x
= math
.sin
( math
.radians
(angle
) ) * radiuspos
.y
= math
.cos
( math
.radians
(angle
) ) * radiusdelta_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
)width
,height
= scratch_ship
.get_size
()x
= 400+pos
.x
-width
//2y
= 300+pos
.y
-height
//2screen
.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
()old_pos
.x
= pos
.x old_pos
.y
= pos
.y
總結
以上是生活随笔為你收集整理的Pygame游戏之 飞船绕行的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。