python面向对象

利用python来进行一些简单的动画操作:
第一步:写出框架和构建一个窗口

# Pygame template - skel;enton for a new pygame project
import pygame
import random

#屏幕创建
WIDTH = 360
HEIGHT = 480
FPS = 30

#define colors
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)

#initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()
#Game loop
running = True
while running:
    #keep loop running at the right speed
    clock.tick(FPS)#过多长时间开始制作下一幅图
    #process input(events)
    for events in pygame.event.get():
        #check for closing window
        if events.type == pygame.QUIT:
            running = False
    #update
    all_sprites.update()
    #Draw/render
    screen.fill(BLACK)
    all_sprites.draw(screen)
    # *after drawing everything,flip the display
    pygame.display.flip()

pygame.quit()

2.根据这个延伸出的一些动画程序
(1)利用框架做出的一个方块往右运动,到右边返回左边的动画:

import random
import pygame
#第二节课我自己的pygame:
# Pygame template - skel;enton for a new pygame project

#屏幕创建
WIDTH = 800
HEIGHT = 600
FPS = 40

#define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
color = [WHITE, BLACK, RED, GREEN, BLUE]


class Player(pygame.sprite.Sprite):
    #sprite for the player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH/2, HEIGHT/2)

    def update(self):
        self.rect.x += 5
        if self.rect.left > WIDTH:
            self.rect.right = 0


#initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
#Game loop
running = True
while running:
    #keep loop running at the right speed
    clock.tick(FPS)  # 过多长时间开始制作下一幅图
    #process input(events)
    for events in pygame.event.get():
        #check for closing window
        if events.type == pygame.QUIT:
            running = False
    #update
    all_sprites.update()

    #Draw/render
    screen.fill(BLACK)
    all_sprites.draw(screen)
    # *after* drawing everything,flip the display
    pygame.display.flip()

pygame.quit()

(2)利用框架做出随机颜色随机个数随机速度很多小方块来回移动的动画:

import random
import pygame
#我自己打的随机速度颜色(粉红) :
# Pygame template - skel;enton for a new pygame project

#屏幕创建
WIDTH = 1200
HEIGHT = 1000
FPS = 60
num = 0

#define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PINK = (249, 204, 226)
PURPLE = (153, 50, 204)
color = [RED, GREEN, BLUE, PURPLE, WHITE]


class Player(pygame.sprite.Sprite):
    #sprite for the player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(random.choice(color))
        self.rect = self.image.get_rect()
        self.rect.center = (random.randint(1, WIDTH),
                            random.randint(1, HEIGHT))
        self.ra = random.randint(1, 2)
        self.speed = random.choice(
            [i for i in range(-20, 20) if not i in range(-5, 5)])

    def update(self):
        if self.ra == 1:
            self.rect.x += self.speed
        else:
            self.rect.y += self.speed


#initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()
#set group
all_sprites = pygame.sprite.Group()
player1 = [Player() for i in range(random.randrange(20, 40))]
player2 = [Player() for i in range(random.randrange(20, 40))]
all_sprites.add(player1)
all_sprites.add(player2)
#Game loop
running = True
while running:
    #设置对象
    num += 1
    if num % 100 == 0:
        player1 = [Player() for i in range(random.randrange(20, 40))]
        all_sprites.add(player1)
    if num % 150 == 0:
        player2 = [Player() for i in range(random.randrange(20, 40))]
        all_sprites.add(player2)
    #keep loop running at the right speed
    clock.tick(FPS)  # 过多长时间开始制作下一幅图
    #process input(events)
    for events in pygame.event.get():
        #check for closing window
        if events.type == pygame.QUIT:
            running = False
    #update
    all_sprites.update()

    #Draw/render
    screen.fill(PINK)
    all_sprites.draw(screen)
    # *after* drawing everything,flip the display
    pygame.display.flip()

pygame.quit()

(3)一个方块往上跑两次,另一个方块往右跑两次,无线循环直到关闭

# Pygame template - skel;enton for a new pygame project
import pygame
import random

#屏幕创建
WIDTH = 800
HEIGHT = 600
FPS = 60

#define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PINK = (249,204,226)

f = tm1 = tm2 =0 
class Player1(pygame.sprite.Sprite):
    #sprite for the player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH/2 ,HEIGHT/2)
    def update(self):
        global f,tm1
        if f==1:
            return
        elif f==0:
            self.rect.y-=5
            if tm1 == 2:
                f = 1 
                tm1 = 0
                return 
            if self.rect.bottom < 0:
                self.rect.top = HEIGHT
                tm1 += 1


class Player2(pygame.sprite.Sprite):
    #sprite for the player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH/2, HEIGHT/2)

    def update(self):
        global f,tm2
        if f==0:
            return
        elif f==1:
            self.rect.x += 5
            if tm2==2:
                f=0
                tm2=0
                return
            if self.rect.x > WIDTH:
                self.rect.x = 0
                tm2 += 1 

#initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()
t1 = pygame.sprite.Group()
t2 = pygame.sprite.Group()
player1 = Player1()
player2 = Player2()
all_sprites.add(player1)
all_sprites.add(player2)
t1.add(player1)
t2.add(player2)

#Game loop
running = True
while running:
    #keep loop running at the right speed
    clock.tick(FPS)  # 过多长时间开始制作下一幅图
    #process input(events)
    for events in pygame.event.get():
        #check for closing window
        if events.type == pygame.QUIT:
            running = False
    #update
    all_sprites.update()
    #Draw/render
    screen.fill(PINK)
    if f==0:
        t1.draw(screen)
    elif f==1:
        t2.draw(screen)
    # *after drawing everything,flip the display
    pygame.display.flip()

pygame.quit()

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值