动态滑稽
"""
__author__ = 'rz'
__Time__ = 2018/9/5 0005 下午 1:48
"""
'''
滑稽
'''
import pygame
from pygame.locals import *
from random import randint,choice
WINDOW_WIDTH = 1000
WINDOW_HEIGHT = 600
face_list = []
key_face_pos = 'face_pos'
key_face_xspeed = 'face_xspeed'
key_face_yspeed = 'face_yspeed'
key_face_scale = 'face_scale'
key_face_size = 'face_size'
def draw_all_face(window):
window.fill(rz_color.white)
for face in face_list:
image = pygame.image.load('./static/img/huaji.jpg')
new_image = pygame.transform.rotozoom(image, 0, face[key_face_scale])
window.blit(new_image, face['face_pos'])
def move_face():
"""
使求移动,修改球的位置
:return:
"""
for face in face_list:
face_x,face_y = face[key_face_pos]
new_x,new_y = face[key_face_xspeed]+face_x,face[key_face_yspeed]+face_y
face[key_face_pos] =(new_x, new_y)
#边界检测
if new_x < 0:
new_x = 0
face[key_face_xspeed] *= -1
elif new_x > WINDOW_WIDTH-face[key_face_size][0]:
new_x = WINDOW_WIDTH-face[key_face_size][0]
face[key_face_xspeed] *= -1
if new_y < 0:
new_y = 0
face[key_face_yspeed] *= -1
elif new_y > WINDOW_HEIGHT-face[key_face_size][1]:
new_y = WINDOW_HEIGHT-face[key_face_size][1]
face[key_face_yspeed] *= -1
pygame.display.update()
def create_face(window,pos):
"""
在指定的位置画一个滑稽
:param window: 显示对象
:param pos: 鼠标坐标
:return:
"""
image = pygame.image.load('./static/img/huaji.jpg')
img_scale = choice([0.1,0.2,0.3,0.5])
new_image = pygame.transform.rotozoom(image, 0, img_scale)
img_size = new_image.get_size()
mouse_pos = pos
face = {
key_face_pos:mouse_pos,
key_face_xspeed:choice([-10,-20,10,20]),
key_face_yspeed:choice([-10,-20,10,20]),
key_face_scale:img_scale,
key_face_size:img_size
}
face_list.append(face)
window.blit(new_image, mouse_pos)
pygame.display.update()
def main_game():
"""
游戏主界面
:return:
"""
#初始化游戏
# pygame.time.delay(100)
pygame.init()
window = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT),0,32)
window.fill(rz_color.white)
pygame.display.flip()
# 进入游戏界面默认显示和操作
#游戏循环
while True:
# 游戏循环执行代码
move_face()
draw_all_face(window)
# 事件检测
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
create_face(window,event.pos)
elif event.type == K_a:
pass
if __name__ == '__main__':
main_game()