完整代码
# /usr/bin/python3
# Author: 爱编程的章老师
# @Time: 2021/1/7 0007
# E-mail: [email protected]
import pygame, sys, time
from random import randint
def homework():
'''作业'''
'''
挡板接球
一个宽100 ,高10的棕色挡板,一个直径20的小球
小球从档板上方纵向距离200以上,左右位置随机的位置处以每次2单位,0.01秒一次的速度从上往下掉
用左右方向键(a 和 d也可以)来控制挡板左右移动来接住小球.
接住小不球,游戏胜利,
未接住,游戏失败.
未接住定义:当注球的最下方位于与挡板的最上方.3 个以上单位的时候
接住定义:当小球的圆心横坐标在挡板左右之间,小球与挡板相交,且小球的下方-挡板上方<=3
'''
pygame.init()
s = pygame.display.set_mode((800, 600))
# 挡板矩形
board = pygame.Rect(350, 550, 100, 10)
board_color = 139, 90, 43
pygame.draw.rect(s, board_color, board)
# 小球矩形
bx = randint(0, 780)
ball = pygame.Rect(bx, 100, 20,20)
ball_color = 255, 255, 255
pygame.draw.ellipse(s, ball_color, ball)
pygame.display.update()
pygame.key.set_repeat(pygame.KEYDOWN)
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ball.move_ip(0, 2)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
board.move_ip(-5, 0)
elif keys[pygame.K_RIGHT]:
board.move_ip(5, 0)
if ball.bottom - board.top > 5:
print("你失败了,游戏结束")
break
if ball.colliderect(board):
print("你胜利了")
break
s.fill((0, 0, 0))
pygame.draw.rect(s, board_color, board)
pygame.draw.ellipse(s, ball_color, ball)
pygame.display.update()
time.sleep(0.01)
if __name__ == '__main__':
homework()
几点说明:
上节课,我们学会了对单个按键的响应.今天我们来学习一下组合键(多键)响应
代码:
def combo_keydown():
pygame.init()
s = pygame.display.set_mode((800, 600))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
sys.exit()
备注:
在我们开发游戏的时候,组合键的功能还是比较常用的.比如我们需要让人物同时按下U + I 时发一个大招.
示例代码
def combination_key2():
pygame.init()
s = pygame.display.set_mode((800, 600))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_u] and keys[pygame.K_i]:
print("我发动了大招,就问你怕不怕")
效果图:
event == pygame.MOUSEBUTTONDOWN
属性:button
值 | 意义 |
---|---|
1 | 左键单击 |
2 | 滚轮单击 |
3 | 右键单击 |
4 | 滚轮朝上滚 |
5 | 滚轮朝下滚 |
示例:
# 鼠标单击事件
def mouse_click():
pygame.init()
s = pygame.display.set_mode((800, 600))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
print("左键单击")
elif event.button == 2:
print("按下滚轮键")
elif event.button == 3:
print("按下右键")
elif event.button == 4:
print("滚轮朝上")
elif event.button == 5:
print("按下朝下")
属性:pos
获取鼠标点击时的坐标
示例:
print("鼠标点击的位置:", event.pos)
event.type == pygame.MOUSEMOTION
属性:pos 实时获取鼠标当前位置的坐标
属性:rel 鼠标的移动逻辑(dx,dy)从上一个记录点到这一个记录点的坐标变化情况
属性:buttons:三个按键的按下情况的三元组.分别表示左中右三个按键.当按下时,值为1,松开时值为0
举例:按下左键时移动,buttons的值(1,0,0),按下右键的时候移动,buttons的值为(0,0,1)
示例:
# 鼠标移动
def mouse_motion():
pygame.init()
s = pygame.display.set_mode((800, 600))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
print(event.pos)
print(event.rel)
print(event.buttons)
event.type == pygame.MOUSEBUTTONUP:
用法同鼠标点击
# 鼠标松开事件
def mouse_up():
pygame.init()
s = pygame.display.set_mode((800, 600))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
print(event.button)
print(event.pos)
举例:
鼠标左键按下后拖拽效果
代码:
def mouse_drag():
'''鼠标左键拖拽效果'''
pygame.init()
s = pygame.display.set_mode((800, 600))
start_pos = 0, 0
end_pos = 0, 0
drag_flag = False
width, height = 0, 0
rect_color = GREEN = 0, 255, 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
drag_flag = False
s.fill((0,0,0))
pygame.display.update()
if not drag_flag and event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
drag_flag = True
start_pos = event.pos
if drag_flag and event.type == pygame.MOUSEMOTION:
end_pos = event.pos
if start_pos[0] > end_pos[0]:
left = end_pos[0]
else:
left = start_pos[0]
if start_pos[1] > end_pos[1]:
top = end_pos[1]
else:
top = start_pos[1]
width = abs(start_pos[0] - end_pos[0])
height = abs(start_pos[1] - end_pos[1])
s.fill((0,0,0))
pygame.draw.rect(s, rect_color, (left, top, width, height), 1)
pygame.display.update()
效果图:
代码解析:
当处于拖拽终点时,要画矩形.
当处于放弃拖拽时,要清空屏幕
终于把按键的响应讲完了.今天的内容比较多,也比较有用.是我们以后常用的内容.请多多练习巩固