美文网首页
day11pygame(2)

day11pygame(2)

作者: KingJX | 来源:发表于2018-07-30 20:56 被阅读0次

01event

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: KingJX
# @Date  : 2018/7/30 10:44
""""""
import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600, 400))
    screen.fill((255, 255, 255))
    # 设置窗口标题
    pygame.display.set_caption('游戏事件')
    pygame.display.flip()
    """
    QUIT:关闭按钮被点击事件
    MOUSEBUTTONDOWN:鼠标按下
    """
    while True:
        for event in pygame.event.get():
            # 不同类型的事件对应的type值不一样
            if event.type == pygame.QUIT:
                exit()
            # 鼠标按下事件
            # pos属性,获取鼠标事件产生的位置
            if event.type == pygame.MOUSEBUTTONDOWN:
                print('鼠标按下', event.pos)
            if event.type == pygame.MOUSEBUTTONUP:
                print('鼠标弹起', event.pos)
            if event.type == pygame.MOUSEMOTION:
                print('鼠标移动', event.pos)
            # 键盘相关事件
            # KEY属性,被按的暗降对应的值得编码
            if event.type == pygame.KEYDOWN:
                print('键盘按键被按下', chr(event.key))
            if event.type == pygame.KEYUP:
                print('键盘按键弹起', chr(event.key))
鼠标移动 (376, 286)
鼠标移动 (373, 285)
鼠标移动 (372, 285)
鼠标按下 (372, 285)
鼠标弹起 (372, 285)
键盘按键被按下 f
键盘按键弹起 f
键盘按键被按下 d
键盘按键弹起 d
键盘按键被按下 v
键盘按键弹起 v
鼠标按下 (372, 285)
鼠标弹起 (372, 285)
鼠标按下 (372, 285)
键盘按键被按下 w
键盘按键弹起 w
鼠标弹起 (372, 285)

02-鼠标事件得应用


import pygame
from random import randint

def rand_color():
    return randint(0,255),randint(0,255),randint(0,255)

def draw_ball(screen, pos):
    pygame.draw.circle(screen, rand_color(), pos, randint(10, 20))

    # 只要屏幕上的内容有更新,都需要调用这两个方法中得一个
    # pygame.display.flip()
    pygame.display.update()

# 写一个函数,判断指定的点,是否在指定的矩形范围中
def isInRect(point, rect):
    x, y = point
    rx,ry,rw,rh = rect
    if (rx<=x<rx+rw) and (ry<=y<=ry+rh):
        return True
    return False

# 画一个按钮
def draw_button(screen, btn_color, title_color):

    # 画个按钮
    """矩形框"""
    pygame.draw.rect(screen, btn_color, (100, 100, 100, 60))
    """文字"""
    font = pygame.font.SysFont('Times', 30)
    title = font.render('clicke', True, title_color)
    screen.blit(title, (120, 120))






if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,400))
    screen.fill((255,255,255))
    pygame.display.set_caption('鼠标事件')
    draw_button(screen,(0,255,0),(255,0,0))


    pygame.display.flip()

    while True:
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                exit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                if isInRect(event.pos,(100,100,100,60)):
                    draw_button(screen,(0,100,0),(0,0,100))
                    pygame.display.update()

            if event.type == pygame.MOUSEBUTTONUP:
                if isInRect(event.pos,(100,100,100,60)):
                    draw_button(screen,(0,255,0),(255,0,0))
                    pygame.display.update()

            if event.type == pygame.MOUSEMOTION:
                screen.fill((255,255,255))
                draw_button(screen, (0, 255, 0), (255, 0, 0))
                draw_ball(screen,event.pos)
                # pygame.display(screen,event.pos)
鼠标点击事件(1)png.png 鼠标点击事件(2)png.png

03鼠标点击事件


import pygame

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



if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    screen.fill((255,255,255))

    img = pygame.image.load('./imges/img1.png')
    img_x = 100
    img_y = 100
    screen.blit(img, (img_x,img_y))
    pygame.display.flip()

    # 用来存储图片是否能移动
    is_move = False

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

            # 鼠标按下,让状态变为可以移动,弹起时不可以移动
            if event.type == pygame.MOUSEBUTTONDOWN:
                img = pygame.image.load('./imges/img1.png')
                x, y = img.get_size()
                img_w, img_h = img.get_size()

                if is_in_rect(event.pos,(img_x,img_y,img_w,img_h)):
                    is_move = True

            if event.type == pygame.MOUSEBUTTONUP:
                is_move = False

            if event.type == pygame.MOUSEMOTION:
                if is_move:
                    screen.fill((255, 255, 255))
                    img = pygame.image.load('./imges/img1.png')
                    x,y = event.pos
                    img_w,img_h = img.get_size()
                    img_x = x-img_w/2
                    img_y = y-img_h/2
                    screen.blit(img, (img_x,img_y))
                    # 3.展示在屏幕上
                    pygame.display.update()
                    # pygame.display.update()

拖拽图片.png

04-动画效果


import pygame
from random import randint

def static_page(screen):
    # 静态文字
    font = pygame.font.SysFont('Times',40)
    title = font.render('Hello',True,(255,0,0))
    screen.blit(title,(200,200))

def animation_title(screen):
    font = pygame.font.SysFont('Times', 40)
    title = font.render('Hello', True, (randint(0,255), randint(0,255), randint(0,255)))
    screen.blit(title, (100, 100))



if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    screen.fill((255,255,255))
    static_page(screen)

    pygame.display.flip()

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

        pygame.time.delay(300)
        screen.fill((255, 255, 255))
        static_page(screen)
        animation_title(screen)
        pygame.display.update()

05-ballgame


import pygame
from random import randint
def draw_ball(place, color,pos):
    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((800,600))
    screen.fill((255,255,255))



    pygame.display.flip()

    ball_x = 400
    ball_y = 300
    x_speed = 0
    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:
                    y_speed = -1
                    x_speed = 0
                elif event.key == Down:
                    y_speed = 1
                    x_speed = 0
                elif event.key == Left:
                    y_speed = 0
                    x_speed = -1
                elif event.key == Right:
                    x_speed = 1
                    y_speed = 0




        screen.fill((255, 255, 255))


        ball_x += x_speed
        ball_y += y_speed
        # 小球到边界后,自动弹回
        if ball_x + 20 >= 800 or ball_x + 20 <= 20:
            x_speed *= -1
        if ball_y + 20 >= 600 or ball_y + 20 <= 20:
            y_speed *=-1
        draw_ball(screen,(255,0,0),(ball_x,ball_y))
        pygame.display.update()

相关文章

  • day11pygame(2)

    01event 02-鼠标事件得应用 03鼠标点击事件 04-动画效果 05-ballgame

  • DAY 2(2/2)

    五彩滩声名在外,但是我们去的时候在休整,我们十分不甘心,根据各种攻略告诉我们在景区出口有村民守着问你要不要去五彩滩...

  • 2-2-2

    自由写作群 转化与蜕变 继续刚才的梦的后记 我想梦是用最形象的比喻告诉我内在正在经历着发生着什么,这是潜意识里已经...

  • 2 (2)

    突然想到Jenny ,那个有些神经质的女孩儿。 对我来说,Jenny 给我最深的印象是作家。作为一个作家,她的灵感...

  • 2-2-2 RelativeLayout

    标注:本文为个人整理,仅做自己学习参考使用,请勿转载和转发2018-06-03: 初稿,参考博主coder-pig...

  • 2️⃣0️⃣2️⃣0️⃣🔚🔜2️⃣0️⃣2️⃣1️⃣

    今天风小了,夕阳很平静,但2020年终究是不平静的一年。 不平静的2020年,第一次有了一张小区出入证。不能飞去热...

  • 2-2

    ❤️起步,️️(若起步的右车道前方无车,可以不用转到左车道; 转发了右车道一定要变更车道) 一段车程 ❤️右转,右...

  • < маленький принц > 2-2

    Итак, в первый вечер я уснул на песке в пустыне, где на...

  • 2-2

    悠闲的一天。

  • 2-2

    翠绿幽篁浴暖阳 仄仄平平仄仄平 笛声绕耳浸心房 平平仄仄仄平平 鲜闻繁琐劳神事 平平仄仄平平仄 袅袅香茗伴月尝 仄...

网友评论

      本文标题:day11pygame(2)

      本文链接:https://www.haomeiwen.com/subject/pxrqvftx.html