python-pygame 事件學習

#coding:utf-8
import pygame
import sys
from pygame.locals import *

# 初始化Pygame
pygame.init()

size = width, height = 600, 400
speed = [-2, 1]
bg = (255, 255, 255) # RGB

# 創建指定大小的窗口 Surface
screen = pygame.display.set_mode(size)
# 設置窗口標題
pygame.display.set_caption(" see you")

# 加在圖片
turtle = pygame.image.load("turtle.png")
# 獲得圖像的位置矩形
position = turtle.get_rect()

l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                turtle = l_head
                speed = [-1, 0]
            if event.key == K_RIGHT:
                turtle = r_head
                speed = [1, 0]
            if event.key == K_UP:
                speed = [0, -1]
            if event.key == K_DOWN:
                speed = [0, 1]

    # 移動圖像
    position = position.move(speed)

    if position.left < 0 or position.right > width:
        # 翻轉圖像
        turtle = pygame.transform.flip(turtle, True, False)
        # 反方向移動
        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]

    # 填充背景
    screen.fill(bg)
    # 更新圖像
    screen.blit(turtle, position)
    # 更新界面
    pygame.display.flip()
    # 延遲10毫秒
    pygame.time.delay(10)
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章