import pygame,sys
from pygame.locals import *
def RGBChannel(a):
return 0 if a<0 else (255 if a>255 else int(a))
pygame.init()
size = width,height = 600,400
bgcolor = pygame.Color("black")
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("滑滑稽")
photo = pygame.image.load("huaji.png")
position = photo_rect = photo.get_rect()
speed = [2,0]
photo_right = pygame.transform.rotate(photo,90)
photo_top = pygame.transform.rotate(photo,180)
photo_left = pygame.transform.rotate(photo,270)
photo_bottom = photo
photo = photo_top
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
position = position.move(speed)
if position.right > width:
photo = photo_right
speed = [0,2]
position = photo_rect = photo.get_rect()
position.left = width - photo_rect.width
if position.bottom > height:
photo = photo_bottom
speed = [-2,0]
position = photo_rect = photo.get_rect()
position.left = width - photo_rect.width
position.top = height - photo_rect.height
if position.left < 0:
photo = photo_left
speed = [0,-2]
position = photo_rect = photo.get_rect()
# position.left = width - photo_rect.width
position.top = height - photo_rect.height
if position.top < 0:
photo = photo_top
position = photo_rect = photo.get_rect()
speed = [2,0]
# position.left = width - photo_rect.width
# position.top = height - photo_rect.height
bgcolor.r = RGBChannel(photo_rect.left * 255 / width)
bgcolor.g = RGBChannel(photo_rect.top * 255 / height)
bgcolor.b = RGBChannel(min(speed[0], speed[1]) * 255 / max(speed[0], speed[1], 1))
screen.fill(bgcolor)
screen.blit(photo,position)
pygame.display.update()
clock.tick(600)
'''
裁剪圖片
'''
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 600,400
bg = (255,255,255)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次見面,請大家多多關照")
dog = pygame.image.load("huaji.png")
# 使用 select 判斷選擇框狀態,drag 判斷拖拽的狀態
# 0->未選擇,1->選擇中,2->完成選擇
select = 0
# 0->未拖拽,1->拖拽中,2->完成拖拽
drag = 0
# Rect 用於存儲直角座標的pygame對象
# Rect(左,頂,寬,高) 這些屬性可以控制一個矩形的位置,大小
select_rect = pygame.Rect(0,0,0,0)
# 獲得圖像的位置矩形
position = dog.get_rect()
# 爲了控制將初始圖像放置在screen的中心處
position.center = width //2,height //2 # 整除獲得圖像中心位置
# 設置爲死循環,確保窗口一直顯示
while True:
# 遍歷所有的事件
for event in pygame.event.get():
# 如果單擊關閉窗口,則退出
if event.type == pygame.QUIT:
sys.exit()
# 如果發生鼠標按下事件
elif event.type == MOUSEBUTTONDOWN:
# 鼠標摁下的兩個屬性,button(1:鼠標摁下)和pos(鼠標點擊的座標)
if event.button == 1:
# 第一次點擊,選擇範圍
if select == 0 and drag == 0:
pos_start = pygame.mouse.get_pos()#或者event.pos
select = 1
# 第二次點擊,拖拽圖像
elif select ==2 and drag == 0:
# subsurface 創建一個引用其父級的新表面
# copy 方法,返回與原始位置和大小相同的新矩形
capture = screen.subsurface(select_rect).copy()
cap_rect = capture.get_rect()
drag = 1
# 第三次點擊,初始化
elif select == 2 and drag == 2:
select =0
drag = 0
elif event.type == MOUSEBUTTONUP:
# button = 1 代表鬆開鼠標
if event.button == 1:
# 第一次釋放,結束選擇
if select == 1 and drag ==0:
# 記錄釋放點的座標
#pos_stop = event.pos#這個沒必要加
select = 2
if select == 2 and drag == 1:
drag = 2
# 填充背景
screen.fill(bg)
# 更新圖像
screen.blit(dog,position)
# 實時繪製選擇框
if select:
#pygame.mouse.get_pos():返回鼠標光標的位置X和Y位置。該位置相對於顯示屏的左上角。
mouse_pos = pygame.mouse.get_pos()
if select == 1:
pos_stop = mouse_pos
# 選擇框的 left 和 top 通過起始鼠標點擊處確定選擇框的位置
select_rect.left,select_rect.top = pos_start
# 確定選擇框的寬和高
select_rect.width,select_rect.height = pos_stop[0] - pos_start[0],pos_stop[1] - pos_start[1]
# 畫一個矩形
pygame.draw.rect(screen,(0,0,0),select_rect,4)#那3個0是邊框顏色,4代表邊框線的厚度
# 拖拽裁剪圖像
if drag:
if drag == 1:
cap_rect.center = mouse_pos
screen.blit(capture,cap_rect)
# 更新界面
pygame.display.flip()
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width,height = 658,302
bg = (0,0,0)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次見面,請大家多多關照")
dog = pygame.image.load("huaji.png").convert()
# dog.set_colorkey(255,255,255)
background = pygame.image.load("chuanqi.png").convert()
position = dog.get_rect()
position.center = width //2,height //2
dog.set_colorkey((255,255,255))
dog.set_alpha((255,255,255))
# 設置爲死循環,確保窗口一直顯示
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 更新圖像
screen.blit(background,(0,0))
screen.blit(dog, position)
# 更新界面
pygame.display.flip()
clock.tick(30)
#壁球遊戲
import pygame,sys
def RGBChannel(a):
return 0 if a<0 else (255 if a>255 else int(a))
pygame.init()
icon = pygame.image.load("cat.png")
pygame.display.set_icon(icon)
# VInfo = pygame.display.Info()
# size = width,height = VInfo.current_w,VInfo.current_h
size = width,height = 600,400#1200,700
speed=[1,1]
# BLACK = 230, 230, 230
screen = pygame.display.set_mode(size)
bgcolor = pygame.Color("black")
# screen = pygame.display.set_mode(size,pygame.RESIZABLE)
# screen = pygame.display.set_mode(size,pygame.NOFRAME)
# screen = pygame.display.set_mode(size,pygame.FULLSCREEN)
pygame.display.set_caption("Pygame遊戲壁球")
ball = pygame.image.load("liqiangg.png")
ballrect = ball.get_rect()
fps =300
fclock = pygame.time.Clock()
still = False
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_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0])-1)*int(speed[0])/abs(speed[0])
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0]-1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1]-1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1])-1)*int(speed[1])/abs(speed[1])
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width,height = event.size[0],event.size[1]
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
still = True
elif event.type == pygame.MOUSEBUTTONUP:
still = False
if event.button == 1:
ballrect = ballrect.move(event.pos[0]-ballrect.left,event.pos[1]-ballrect.top)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1:
ballrect = ballrect.move(event.pos[0]-ballrect.left,event.pos[1]-ballrect.top)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed[0],speed[1])
ballrect = ballrect.move(speed[0],speed[1])
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
speed[1] = -speed[1]
# screen.fill(BLACK)
bgcolor.r = RGBChannel(ballrect.left*255/width)
bgcolor.g = RGBChannel(ballrect.top*255/height)
bgcolor.b = RGBChannel(min(speed[0],speed[1])*255/max(speed[0],speed[1],1))
screen.fill(bgcolor)
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
python pygame筆記
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.