Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3648153
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2022-11-21 17:33:15

import random

import pygame

'''植物'''

class Plant(pygame.sprite.Sprite):

def __init__(self, WIDTH=640, HEIGHT=500):

pygame.sprite.Sprite.__init__(self)

self.WIDTH = WIDTH

self.HEIGHT = HEIGHT

# 统计分数时用的

self.added_score = False

self.speed = 5

# self.imgs = ['./images/obstacles/plant1.png', './images/obstacles/plant2.png', './images/obstacles/plant3.png', './images/obstacles/plant4.png']

self.imgs = ['./images/obstacles/plant_big.png', './images/obstacles/plant_small.png']

self.generate_random()

'''随机生成障碍物'''

def generate_random(self):

idx = random.randint(0, 1)

temp = pygame.image.load(self.imgs[idx]).convert_alpha()

if idx == 0:

self.plant = temp.subsurface((101*random.randint(0, 2), 0), (101, 101))

else:

self.plant = temp.subsurface((68*random.randint(0, 2), 0), (68, 70))

self.rect = self.plant.get_rect()

self.rect.left, self.rect.top = self.WIDTH+60, int(self.HEIGHT/2)

'''不停往左移动'''

def move(self):

self.rect.left = self.rect.left-self.speed

'''把自己画到屏幕上去'''

def draw(self, screen):

screen.blit(self.plant, self.rect)

'''飞龙'''

class Ptera(pygame.sprite.Sprite):

def __init__(self, WIDTH=640, HEIGHT=500):

pygame.sprite.Sprite.__init__(self)

self.WIDTH = WIDTH

self.HEIGHT = HEIGHT

# 统计分数时用的

self.added_score = False

self.imgs = ['./images/obstacles/ptera.png']

# 为了飞行特效

self.flying_count = 0

self.flying_flag = True

# 统计分数时用的

self.speed = 7

self.generate()

'''生成飞龙'''

def generate(self):

self.ptera = pygame.image.load(self.imgs[0]).convert_alpha()

self.ptera_0 = self.ptera.subsurface((0, 0), (92, 81))

self.ptera_1 = self.ptera.subsurface((92, 0), (92, 81))

self.rect =外汇跟单gendan5.com self.ptera_0.get_rect()

self.rect.left, self.rect.top = self.WIDTH+30, int(self.HEIGHT/20)

'''不停往左移动'''

def move(self):

self.rect.left = self.rect.left-self.speed

'''把自己画到屏幕上去'''

def draw(self, screen):

self.flying_count += 1

if self.flying_count % 6 == 0:

self.flying_flag = not self.flying_flag

if self.flying_flag:

screen.blit(self.ptera_0, self.rect)

else:

screen.blit(self.ptera_1, self.rect)

阅读(603) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~