2019-01-17 day19 pygame事件\基本操作
2019-01-17 本文已影响0人
woming
01事件
import pygame
from color import Color
from random import randint
"""
游戏中的事件:
1.鼠标相关的事件
MOUSEBUTTONDOWN - 鼠标按下
MOUSEBUTTONUP - 鼠标弹起
MOUSEMOTION - 鼠标移动
鼠标事件要关注事件发生的位置:event.pos - 鼠标的坐标,元祖
2.键盘事件
KEYDOWN
KEYUP
键盘事件要关注哪个键被按了:event.key - 按键对应的字符编码,数字
"""
def main():
pygame.init()
window = pygame.display.set_mode((400, 600))
pygame.display.set_caption('事件')
window.fill(Color.white)
pygame.display.flip()
is_move = False
while True:
# 不断检测是否有事件产生,如果有事件产生才会进入for循环
for event in pygame.event.get():
# 这儿的event是事件对象,我们可以通过事件对象的type值来判断事件的类型
# =====================鼠标事件===========================
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 鼠标按下要做什么,就将代码写在这个if语句中
print('鼠标按下', event.pos)
# pygame.draw.circle(window, Color.random_color(), event.pos, randint(10, 20))
# pygame.display.update()
is_move = True
elif event.type == pygame.MOUSEBUTTONUP:
# 鼠标弹起要做什么,就将代码写在这个if语句中
print('鼠标弹起')
is_move = False
elif event.type == pygame.MOUSEMOTION:
# 鼠标移动要做什么,就将代码写在这个if语句中
if is_move:
pygame.draw.circle(window, Color.random_color(), event.pos, 20)
pygame.display.update()
print('鼠标移动')
# ===============键盘事件===================
if event.type == pygame.KEYDOWN:
print('按键被按下')
print(event.key, chr(event.key))
elif event.type == pygame.KEYUP:
print('按键弹起')
if __name__ == '__main__':
main()
02按钮 老师写的
import pygame
from color import Color
from random import randint
class Button:
def __init__(self, x, y, width, height, text='', background_color=Color.red, text_color=Color.white):
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.background_color = background_color
self.text_color = text_color
self.font_size = 30
def show(self, window):
pygame.draw.rect(window, self.background_color, (self.x, self.y, self.width, self.height))
font = pygame.font.SysFont('Times', self.font_size)
text = font.render(self.text, True, self.text_color)
w, h = text.get_size()
x = self.width / 2 - w / 2 + self.x
y = self.height / 2 - h / 2 + self.y
window.blit(text, (x, y))
def is_cliecked(self, pos):
x, y = pos
return (self.x <= x <= self.x + self.width) and (self.y <= y <= self.y + self.height)
def main():
pygame.init()
window = pygame.display.set_mode((400, 600))
pygame.display.set_caption('事件')
window.fill(Color.white)
# add_btn(window)
add_btn = Button(100, 100, 100, 50, 'del')
add_btn.show(window)
btn2 = Button(100, 250, 100, 60, 'Score', background_color=Color.yellow, text_color=Color.black)
btn2.show(window)
pygame.display.flip()
is_move = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mx, my = event.pos
if add_btn.is_cliecked(event.pos):
print('删除!')
continue
if btn2.is_cliecked(event.pos):
# print('hello')
btn2.text = str(randint(0, 100))
btn2.show(window)
pygame.display.update()
continue
if __name__ == '__main__':
main()
03移动控制
import pygame
from color import Color
from random import randint
window_width = 400
window_height = 600
class Direction:
UP = 273
DOWN = 274
RIGHT = 275
LEFT = 276
class Ball:
def __init__(self, center_x, center_y, radius, bg_color=Color.random_color()):
self.center_x = center_x
self.center_y = center_y
self.radius = radius
self.bg_color = bg_color
self.is_move = False # 是否移动
self.move_direction = Direction.DOWN
self.speed_y = 2
self.speed_x = 2
def disappear(self, window):
pygame.draw.circle(window, Color.white, (self.center_x, self.center_y), self.radius)
def show(self, window):
pygame.draw.circle(window, self.bg_color, (self.center_x, self.center_y), self.radius)
def move_random(self, window):
self.disappear(window)
self.center_x = randint(self.radius, window_width-self.radius)
self.center_y = randint(self.radius, window_height-self.radius)
self.show(window)
def move_U_D(self, window):
self.disappear(window)
new_y = self.center_y + self.speed_y
if new_y >= window_height - self.radius:
new_y = window_height - self.radius
self.speed_y *= -1
elif new_y <= self.radius:
new_y = self.radius
self.speed_y *= -1
self.center_y = new_y
self.show(window)
def move_L_R(self, window):
self.disappear(window)
new_x = self.center_x + self.speed_x
if new_x >= window_width - self.radius:
new_x = window_width - self.radius
self.speed_x *= -1
elif new_x <= self.radius:
new_x = self.radius
self.speed_x *= -1
self.center_x = new_x
self.show(window)
def main():
pygame.init()
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('事件')
window.fill(Color.white)
ball1 = Ball(randint(0, 400), randint(0, 600), 30, bg_color=Color.red)
ball1.show(window)
ball2 = Ball(randint(0, 400), randint(0, 600), 20)
ball2.show(window)
pygame.display.flip()
time = 0
while True:
time += 1
if time % 5000 == 0:
# ball.move_random(window)
# pygame.display.update()
ball1.move_U_D(window)
ball1.move_L_R(window)
pygame.display.update()
if time % 5000 == 0:
ball2.move_U_D(window)
ball2.move_L_R(window)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == Direction.DOWN or event.key == Direction.UP or event.key == Direction.RIGHT or event.key == Direction.LEFT:
pass
if __name__ == '__main__':
main()