日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pygame游戏之 飞船绕行

發布時間:2024/1/18 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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#X propertydef getx(self): return self.__xdef setx(self, x): self.__x = xx = property(getx, setx)#Y propertydef gety(self): return self.__ydef 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) #設置字體#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 backgroundscreen.blit(space, (0,0)) #繪制背景#draw planetwidth,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) ) * radiuspos.y = math.cos( math.radians(angle) ) * radius#rotate the shipdelta_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 shipwidth,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()#remember positionold_pos.x = pos.x #不斷讓坐標進行變化old_pos.y = pos.y

總結

以上是生活随笔為你收集整理的Pygame游戏之 飞船绕行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。