一缘挽、pygame事件
pygame.init()
screen=pygame.display.set_mode((600,400))
screen.fill((255,255,255))
設(shè)置游戲標(biāo)題
pygame.display.set_caption('游戲事件')
pygame.display.flip()
while True:
每次循環(huán)檢測(cè)有沒有事件發(fā)生
for event in pygame.event.get():
不同類型的事件對(duì)應(yīng)的type值是不同
if event.type==pygame.QUIT:
exit()
鼠標(biāo)按下相關(guān)
pos屬性湿酸,獲取鼠標(biāo)事件產(chǎn)生的位置
if event.type==pygame.MOUSEBUTTONDOWN:
print('鼠標(biāo)按下',event.pos)
if event.type==pygame.MOUSEBUTTONUP:
print('鼠標(biāo)彈起',event.pos)
if event.type==pygame.MOUSEMOTION:
print('鼠標(biāo)移動(dòng)',event.pos)
#鍵盤相關(guān)事件
#key屬性:被按的按鍵對(duì)應(yīng)的值的Asscall碼
if event.type==pygame.KEYDOWN:
print('鍵盤按下',chr(event.key))
if event.type==pygame.KEYUP:
print('鍵盤彈起',chr(event.key))
鼠標(biāo)事件的運(yùn)用
import pygame
from random import randint
def rand_color():
"""
#產(chǎn)生隨機(jī)顏色
:return:
"""
return (randint(0,255),randint(0,255),randint(0,255))
def draw_ball(screen,pos):
pygame.draw.circle(screen, rand_color(), event.pos, randint(10, 20))
# 只要屏幕上的內(nèi)容有更新,都需要調(diào)用下面兩個(gè)方法中的一個(gè)
# pygame.display.flip()
pygame.display.update()
# 寫一個(gè)函數(shù)枣氧,判斷指定的點(diǎn)是否在指定的矩形范圍內(nèi)
def is_in_rect(point,rect):
x,y=point
rx,ry,rw,rh = rect
if (rx<=x<=rx+rw) and (ry<=y<=ry+rh):
return True
return False
# 寫函數(shù)拦宣,畫按鈕
def draw_buttom(screen,bth_color,title_color):
"""矩形框"""
pygame.draw.rect(screen, bth_color, (100, 100, 100, 60))
"""文字"""
font = pygame.font.SysFont('Times', 30)
title = font.render('clicke', True, title_color)
screen.blit(title, (120, 120))
# 按鈕坐標(biāo)
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((600,400))
screen.fill((255,255,255))
pygame.display.set_caption('鼠標(biāo)事件')
#畫個(gè)按鈕
draw_buttom(screen,(0,255,0),(255,0,0))
# """矩形框"""
# pygame.draw.rect(screen,(0,255,0),(100,100,100,60))
# """文字"""
# font=pygame.font.SysFont('Times',30)
# title=font.render('clicke',True,(255,0,0))
# screen.blit(title,(120,120))
pygame.display.flip()
while True:
for event in pygame.event.get():
#退出
if event.type==pygame.QUIT:
exit()
if event.type==pygame.MOUSEBUTTONDOWN:
#
#
if is_in_rect(event.pos,(100,100,100,60)):
draw_buttom(screen,(0,100,0),(100,0,0))
pygame.display.update()
if event.type==pygame.MOUSEBUTTONUP:
if is_in_rect(event.pos,(100,100,100,60)):
draw_buttom(screen,(0,255,0),(255,0,0))
pygame.display.update()
if event.type==pygame.MOUSEMOTION:
screen.fill((255,255,255))
draw_buttom(screen,(0,255,0),(255,0,0))
draw_ball(screen,event.pos)
三截粗、要求:屏幕上顯示圖片,鼠標(biāo)按下就動(dòng)鸵隧,松開就停
import pygame
# 寫函數(shù)绸罗,判斷點(diǎn)是否在范圍內(nèi)
def is_in_rect(pos,rect):
x,y=pos
rx,ry,rw,rh=rect
if (rx<=x<=rx+rw) and (ry<=y<=ry+rh):
return True
return False
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255))
pygame.display.set_caption('鼠標(biāo)拖動(dòng)事件')
image = pygame.image.load('./lufei.jpg')
image_x=100
image_y=100
screen.blit(image, (image_x,image_y ))
pygame.display.flip()
#用來存儲(chǔ)圖片是否可以移動(dòng)
is_move=False
while True:
for event in pygame.event.get():
#退出
if event.type==pygame.QUIT:
exit()
# 鼠標(biāo)按下,狀態(tài)可以移動(dòng)
if event.type==pygame.MOUSEBUTTONDOWN:
w,h=image.get_size()
if is_in_rect(event.pos,(image_x,image_y,w,h)):
is_move=True
# 鼠標(biāo)彈起,讓狀態(tài)變?yōu)椴豢蓜?dòng)
if event.type==pygame.MOUSEBUTTONUP:
is_move=False
#鼠標(biāo)移動(dòng)事件
if event.type==pygame.MOUSEMOTION:
screen.fill((255,255,255))
x ,y = event.pos
image_w,image_h=image.get_size()
# 保證鼠標(biāo)在圖片中心
image_x=x-image_h/2
image_y=y-image_w/2
screen.blit(image,(image_x,image_y))
pygame.display.update()
四豆瘫、畫球
import pygame
def draw_ball(place,color,pos):
"""
畫球
:param place:
:return:
"""
pygame.draw.circle(place,color,pos,20)
Up=273
Down=274
Left=276
Right=275
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((600,400))
screen.fill((255,255,255))
pygame.display.flip()
# 保存初始坐標(biāo)
ball_x=100
ball_y=100
x_speed=1
y_speed=0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
if event.type==pygame.KEYDOWN:
if event.key==Up:
x_speed=0
y_speed=-1
elif event.key==Down:
x_speed=0
y_speed=1
elif event.key==Right:
x_speed = 1
y_speed = 0
elif event.key==Left:
x_speed = -1
y_speed = 0
# 刷新屏幕
screen.fill((255,255,255))
ball_x+=x_speed
ball_y+=y_speed
if ball_x+20>=600 or ball_x<=20 or ball_y+20>=400 or ball_y<=20:
print('游戲結(jié)束')
break
# ball_x=600-20
# x_speed -= 1
#
# if ball_x==20:
# x_speed+=1
draw_ball(screen,(255,0,0),(ball_x,ball_y))
pygame.display.update()
五珊蟀、多球一起動(dòng)
import pygame
import random
random_color=random.randint(0,255),random.randint(0,255),random.randint(0,255)
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((600,400))
pygame.display.flip()
# all_balls中保存多個(gè)球,每個(gè)球要保存半徑外驱、位置育灸、圓心坐標(biāo)、顏色等信息
all_balls=[
{
'r': random.randint(10,20),
'pos':(100,100),
'color':random_color,
'x_speed':random.randint(-1,1),
'y_speed':random.randint(-1,1)
} ,
{
'r': random.randint(10, 20),
'pos': (300, 300),
'color': random_color,
'x_speed': random.randint(-1, 1),
'y_speed': random.randint(-1, 1)
}
]
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
# 點(diǎn)擊畫一個(gè)
if event.type==pygame.MOUSEBUTTONDOWN:
ball={
'r': random.randint(10, 20),
'pos': event.pos,
'color': random_color,
'x_speed': random.randint(-1, 1),
'y_speed': random.randint(-1, 1)
}
#保存球
all_balls.append(ball)
#刷新界面
pygame.time.delay(10)
screen.fill((255,255,255))
for ball_dict in all_balls:
#取出球原來的x坐標(biāo)和y坐標(biāo)昵宇,以及他們的速度
x,y=ball_dict['pos']
x_speed=ball_dict['x_speed']
y_speed = ball_dict['y_speed']
x+=x_speed
y+=y_speed
pygame.draw.circle(screen,ball_dict['color'],(x,y),ball_dict['r'])
#更新球?qū)?yīng)的新的坐標(biāo)
ball_dict['pos']=x,y
pygame.display.update()