<span style="font-family:Times New Roman;font-size:12px;">import pygame
from pygame.locals import *
#set the screen
screen=pygame.set_mode((600,500))
#set the font
myfont=pygame.font.Font(None,60)
#place an image on aplate
white=255,255,255 #type tuple\
#the text is unmodifiable
#first param:test second param:anti-saw third:color(namely RGB value)
textImage=myFont.render("Hello pygame",True,white)
screen.fill(blue)
screen.blit(testImage,(100,100))
#update the animation
pygame.display.update()
#get the coming event
for event in pygame.event.get():
if event.type in(QUIT,KEYDOWN):
sys.exit()
#draw a circle
"""
:color in RGB value
:position is put with x,y
:width specifies the length of the line
"""
pygame.draw.circle(screen,color,position,radius,width)
#set the caption
pygame.display.set_caption("Drawing")
#drawing the rect
#param 3:pos consists of(x,y,length,width)
#params 4:width=0 stands for solid fill
pygame.draw.rect(screen,color,pos,width)
#drawing line
pygame.draw.line(screen,color,(100,100),(500,400),width)
#drawing arcs
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
#built-in methods
story.replace("GUY",guy)
#pack the data into a data structure as cache
import structure
file=open("binary.dat","rb")
size=struct.calsize("i")
byte_read=file.read(size)
while byte_read:
value=struct.unpack("i",byte_read)
value=value[0]
bytes_read=file.read(size)
file.close()
#list the pygame event
QUIT
ACTIVEEVENT
KEYDOWN
KEYUP
MOUSEMOTION
MOUSEBUTTONUP
MOUSEBUTTONDOWN
VIDEORESIZE
VIDEOEXPOSE
USEREVENT
JOYAXISMOTION
JOYBALLMOTION
JOYHATMOTION
JOYBUTTONUP
JOYBUTTONDOWN
#this function avoid repeatable reponse to the event
pygame.key.set_repeat(10)
#e.g get the event data
if event.type==MOUSEBUTTONDOWN:
mouse_down=event.butthon
mouse_down_x,mouse_down_y=evnet.pos
elif event.type==MOUSEBUTTONUP:
mouse_up=event.butthon
mouse_up_x,mouse_up_y=evnet.pos
#poll the device
keys=pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
#a useful API
font=pygame.font.Font("Arial",24)
def print_test(font,x,y,text,color=(255,255,255)):
imgTest=font.render(text,True,color)
screen.blit(imgTest,(x,y))
#try to convert your postion into float type
#math.degrees()&math.radians() can convert from arcs to degrees
math.cos(math.radians(90))
#load the image
pygame.image.load("space.png").convert()
ship=pygame.image.load("space.png").convert_alpha()
#after loading the image.get the length and width
width,height=ship.get_size()
ship.get_width()&ship.get_length()
ship=pygame.transform.scale(ship,(width//2,height//2))
#a better solution to handle the image,which cost more time
ship=pygame.transform.smoothscale(ship,(width//2,height//2))
#calculate the anti-tanget value,given two parameters
rangle=math.atan2(deltay,deltax)
ragled=wrap_angle(-math.degrees(rangle)) #wrap()get the floor-div results
scrath_ship=pygame.transform.rotate(ship,rangled)
#use the pygame.sprite
"""
pygame.sprite is based on rect and image.sprite will update itself
"""
#e.g.
def load(self,filename,width,height,columns):
self.master_image=pygame.image.load(filename).convert_alpha()
self.frame_width=width
self.frame_height=height
self.rect=0,0,width,height
self.columns=columns
#set an inner timer
framerate.pygame.time.Clock()
framerate.tick(30)
ticks=pygame.time.get_ticks()
#build a sprite-group
group=pygame.sprite.Group()
group.add(sprite)
#pygame collision dectection
"""
this call requires that arrow&dragon are single one,and are
derived from pygame.sprite.Sprite.More specially,a Rect
distribution named rect should be built-in.
this function dectect left.rect and right.rect
"""
pygame.sprite.collide_rect(arrow,dragon)
"""
a distribution named radius is better to be available
"""
pygame.sprite.collide_rect_ratio(0.75)(arrow,dragon)
"""
:arrow,a single sprite
:flock_of_birds,a sprite group
:True:will delete all of the sprites in this collision in the group
:return,all of the sprites deleted
"""
pygame.sprite.spritecollide(arrow,flock_of_birds,False)
"""
:arrow,a single sprite
:flock_of_birds,a sprite group
:return:boolean
"""
pygame.sprite.spritecollideany(arrow,flock_of_birds)
"""
:boombs,a single sprite
:cities,a sprite group
:third&fourth params: decide wheather to delete
:return,a dictionary contains collide object
"""
hit_list=pygame.sprite.groupcollide(boombs,cities,True,False)
"""
a distribution called mask should be provided
sub call include surface.lock(),surface.unlock(),surface.get_at()
surface.set_at()
"""
pygame.sprite.collide_mask(first,second)
#provide sound mixer
"""
if you want to use a mp3 decoder,you must get the approvement
ogg format is recommended for long time audio play
"""
pygame.mixer.init()
audio_clip=pygame.mixer.Sound("audio.wav")
channel=pygame.mixer.find_channel()
channel.play(sound_clip)
</span>
pygame入门笔记
最新推荐文章于 2023-02-11 15:41:56 发布
这篇博客介绍了如何使用pygame库来创建简单的游戏。通过设置屏幕、加载字体、绘制图像、处理事件、画圆、画矩形、画线、画弧以及处理键盘和鼠标输入,展示了pygame的基本用法。此外,还提到了pygame中的精灵(sprite)和碰撞检测功能,为初学者提供了入门指导。
摘要由CSDN通过智能技术生成