用户交互----进入游戏

一、增加交互----点击和拖动

1、点击鼠标画点的程序

设置

import pygame                           # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("单击画圆点")
keep_going = True
RED = (255,0,0)                         # RGB color triplet for RED
radius = 15

在游戏循环中处理鼠标点击事件 

while keep_going:                       # Game loop
    for event in pygame.event.get():    # Handling events 
        if event.type == pygame.QUIT: 
            keep_going = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            spot = event.pos
            pygame.draw.circle(screen, RED, spot, radius)

更新屏幕以及退出时要干嘛并整合 

# ClickDots.py
import pygame                           # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("单击画圆点")
keep_going = True
RED = (255,0,0)                         # RGB color triplet for RED
radius = 15
while keep_going:                       # Game loop
    for event in pygame.event.get():    # Handling events 
        if event.type == pygame.QUIT: 
            keep_going = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            spot = event.pos
            pygame.draw.circle(screen, RED, spot, radius)
    pygame.display.update()             # Update display
        
pygame.quit()                           # Exit

2、拖动绘制连续的圆点的程序

拖动的特点是鼠标在按下的同时鼠标在移动。

该程序的总体结构与上面相同,故直接给出代码:

# DragDots.py
import pygame                           # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Click and drag to draw")
keep_going = True
YELLOW = (255,255,0)                    # RGB color triplet for YELLOW
radius = 15
mousedown = False
while keep_going:                       # Game loop
    for event in pygame.event.get():    # Handling events
        if event.type == pygame.QUIT: 
            keep_going = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            mousedown = True
        if event.type == pygame.MOUSEBUTTONUP:
            mousedown = False
    if mousedown:                       # Draw/update graphics
        spot = pygame.mouse.get_pos()
        pygame.draw.circle(screen, YELLOW, spot, radius)
    pygame.display.update()             # Update display
        
pygame.quit()                           # Exit

二、高级交互----笑脸爆炸

1、笑脸精灵

在屏幕上移动的图形化对象叫精灵,因为它们在背景上漂移。

Pygame的pygame.sprite.Sprite类支持精灵图形。 

对象是一个特定类的实例。

每个类都可以有自己的方法、属性和数据。

面向对象编程需要将问题或程序分解为对象,然后构建创建这些对象的类。

代码重用的一个情况是多个程序都用上了某个类。

Group是一个容器类,允许我们将Sprite对象作为一组存储在一起。

笑脸精灵类如下:

class Smiley(pygame.sprite.Sprite):
    pos = (0,0)
    xvel = 1 #笑脸的水平速度
    yvel = 1 #笑脸的垂直速度
    scale = 100 #笑脸的大小

我们的Smiley类定义以关键字class开始,后面跟着想要的类名以及要扩展的类型。

笑脸爆炸程序的设置部分:

# SmileyExplosion.py
import pygame
import random
BLACK = (0,0,0)
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Smiley Explosion")
mousedown = False
keep_going = True
clock = pygame.time.Clock()
pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
sprite_list = pygame.sprite.Group()

2、设置精灵

在初始化方法也叫构造方法中设置精灵:

def __init__(self, pos, xvel, yvel):
        # Call the parent class (Sprite) constructor
        #pygame.sprite.Sprite.__init__(self)
        super().__init__()
        self.image = pic
        #self.scale = random.randrange(10,100)
        #self.image = pygame.transform.scale(self.image, (self.scale,self.scale))
        self.rect = self.image.get_rect()
        self.pos = pos #把鼠标的位置作为精灵的位置
        self.rect.x = pos[0] - self.scale/2 #这行和下一行,使笑脸的中心位于鼠标位置
        self.rect.y = pos[1] - self.scale/2
        self.xvel = xvel
        self.yvel = yvel

3、更新精灵

更新精灵的位置,重写精灵的update方法:

def update(self):
        self.rect.x += self.xvel
        self.rect.y += self.yvel
        if self.rect.x <= 0 or self.rect.x > screen.get_width() - self.scale:
            self.xvel = -self.xvel
        if self.rect.y <= 0 or self.rect.y > screen.get_height() - self.scale:
            self.yvel = -self.yvel

4、较大和较小的笑脸

在构造方法里,我们将笑脸精灵的大小设置为10到100像素:

        self.scale = random.randrange(10,100)
        self.image = pygame.transform.scale(self.image, (self.scale,self.scale))

5、整合

# SmileyExplosion.py
import pygame
import random
BLACK = (0,0,0)
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Smiley Explosion")
mousedown = False
keep_going = True
clock = pygame.time.Clock()
pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
sprite_list = pygame.sprite.Group()
class Smiley(pygame.sprite.Sprite):
    pos = (0,0)
    xvel = 1
    yvel = 1
    scale = 100
    def __init__(self, pos, xvel, yvel):
        super().__init__()
        self.image = pic
        self.scale = random.randrange(10,100)
        self.image = pygame.transform.scale(self.image, (self.scale,self.scale))
        self.rect = self.image.get_rect()
        self.pos = pos
        self.rect.x = pos[0] - self.scale/2
        self.rect.y = pos[1] - self.scale/2
        self.xvel = xvel
        self.yvel = yvel
    def update(self):
        self.rect.x += self.xvel
        self.rect.y += self.yvel
        if self.rect.x <= 0 or self.rect.x > screen.get_width() - self.scale:
            self.xvel = -self.xvel
        if self.rect.y <= 0 or self.rect.y > screen.get_height() - self.scale:
            self.yvel = -self.yvel
while keep_going:
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            keep_going = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            mousedown = True
        if event.type == pygame.MOUSEBUTTONUP:
            mousedown = False
    screen.fill(BLACK)
    sprite_list.update()
    sprite_list.draw(screen)
    clock.tick(60)
    pygame.display.update()
    if mousedown:
        speedx = random.randint(-5, 5)
        speedy = random.randint(-5, 5)
        newSmiley = Smiley(pygame.mouse.get_pos(),speedx,speedy)
        sprite_list.add(newSmiley)
        
pygame.quit()

三、SmileyPop1.0版

1、检测碰撞和删除精灵

2、整合

四、本章小结

五、编程挑战

你可能感兴趣的:(python,python)