pygame基礎操作練習

2021-10-04 16:49:53 字數 3540 閱讀 3101

import pygame

import sys

import pygame.freetype

pygame.init(

)#對pygame的內容初始化

fclock = pygame.time.clock(

)#創造乙個clock物件,用於操作時間

vinfo = pygame.display.info(

)#獲取螢幕資訊大小尺寸

black =0,

0,0#視窗背景顏色

bgcolor = pygame.color(

'black'

)#創造乙個儲存顏色的物件,初始化顏色為黑色

speed =[1

,1]#速度

fps =

300#每秒的幀率引數

still =

false

ball = pygame.image.load(

'image/aline1.png'

)ball_rect = ball.get_rect(

)icon = pygame.image.load(

'image/aline1.png'

)#screen_rect = screen_width,screen_height = vinfo.current_w,vinfo.current_h#視窗的寬高設定為全屏大小

screen_rect = screen_width,screen_height =

300,

400#視窗的寬高

#pygame.resizable|pygame.noframe|pygame.fullscreen

#print(pygame.display.info())#獲取視窗資訊,沒定義視窗時返回螢幕資訊

screen = pygame.display.set_mode(screen_rect,pygame.resizable)

#視窗大小,返回乙個視窗物件

pygame.display.set_caption(

"計算機博弈大賽"

)#視窗標題

pygame.display.set_icon(icon)

#設定視窗圖示

defrgbchannel

(dat)

:return

0if dat <

0else

(255

if dat >

255else

int(dat)

)

while

true

:for event in pygame.event.get():

#獲取事件

if event.

type

== pygame.quit:

sys.exit(

)elif event.

type

== pygame.keydown:

if event.key == pygame.k_down:

speed[1]

= speed[1]

+2elif event.key == pygame.k_up:

speed[1]

= speed[1]

-2elif event.key == pygame.k_left:

speed[0]

= speed[0]

-2elif event.key == pygame.k_right:

speed[0]

= speed[0]

+2elif event.key == pygame.k_space:

sys.exit(

)elif event.

type

== pygame.videoresize:

#動態改變螢幕大小

screen_rect = screen_width,screen_height = event.size[0]

,event.size[1]

pygame.display.set_mode(screen_rect,pygame.resizable)

elif event.

type

== pygame.mousebuttondown:

#按下滑鼠

if event.button ==1:

still =

true

elif event.

type

== pygame.mousebuttonup:

#鬆開滑鼠

if event.button ==1:

still =

false

ball_rect = ball_rect.move(event.pos[0]

- ball_rect.left,event.pos[1]

- ball_rect.top)

elif event.

type

== pygame.mousemotion:

#滑鼠移動

if event.buttons[0]

==1: ball_rect = ball_rect.move(event.pos[0]

- ball_rect.left, event.pos[1]

- ball_rect.top)

if pygame.display.get_active(

)and

not still :

#判斷螢幕是否被最小化,沒有最小化返回true

ball_rect = ball_rect.move(speed[0]

, speed[1]

)# 改變象徵著的矩形的位置

if ball_rect.right > screen_width or ball_rect.left <0:

speed[0]

=-speed[0]

if ball_rect.top <

0or ball_rect.bottom > screen_height:

speed[1]

=-speed[1]

bgcolor.r = rgbchannel(ball_rect.right%

255)

bgcolor.g = rgbchannel(ball_rect.left%

255)

bgcolor.b = rgbchannel(

min(speed[0]

,speed[1]

)*255/

max(speed[1]

,speed[0]

,1))

screen.fill(bgcolor)

#用背景色覆蓋視窗

screen.blit(ball,ball_rect)

#將的顯示位置改變到象徵著的矩形的位置上

pygame.display.update(

)#更新螢幕

fclock.tick(fps)

#每秒更新的幀數

pygame模組練習

background image filename 素材 fly img name 素材 fugu.png from abc import abcmeta,abstractmethod import pygame 匯入pygame庫 from pygame.locals import 匯入一些常用的...

Pygame學習之畫線基本操作

pygame的座標原點 0,0 點位於左上角,x軸自左向右,y軸自上向下,單位為畫素。pygame.draw.line su ce,color,start pos,end pos,width 此方法用於繪製一條線段。pygame.draw.rect su ce,color,rect 此方法用於繪製乙...

pygame庫的操作使用總結

初始化遊戲物件 pygame.init 建立遊戲視窗 pygame.display.set mode win width,win hight pygame.display.set caption 遊戲視窗名稱 渲染背景 windos.fill rgb 注意是乙個引數 第一次重新整理 pygame.d...