Pygame从0实战8(泡泡小游戏)

1.Pygame从0实战8之泡泡小游戏

    用面向对象的思想做一个泡泡拯救世界的小游戏

下面是所要用到的图片和源代码链接:点击打开链接 密码:l68h

sprite模块、Sprite类

    在pygame.sprite模块中包含了一个名为Sprite类,它是pygame

自带的一个精灵。但这个类的功能比较少,因此我们新建一个类对

其继承,在Sprite类的基础上丰富,以方便我们的使用.

官方: 

pygame.sprite.Sprite是精灵基类 
Simple base class for visible game objects.
Sprite类中主要的方法和作用
pygame.sprite.Sprite.update -method to control sprite behavior 
pygame.sprite.Sprite.add    -add the sprite to groups 
pygame.sprite.Sprite.remove -remove the sprite from groups
pygame.sprite.Sprite.kill   -remove the Sprite from all Groups
pygame.sprite.Sprite.alive  -does teh sprite belong to any groups
pygame.sprite.Sprite.groups -list of Groups that contain this Sprite
Sprite类的解释:
The base class for visible game objects. Derived classes will want to
override the Sprite.update() and assign a Sprite.image and Sprite.rect 
attributes. The initializer can accept any number of Group instances to be added
to.When subclassing the Sprite, be sure to call the base initializer before

adding the Sprite to Groups.


# -*- coding: utf-8 -*-
# @Author: Clarence
# @Date:   2018-03-09 08:18:46
# @Last Modified by:   Clarence
# @Last Modified time: 2018-03-09 13:33:36
import  pygame
import sys
from pygame.locals import *
from random import *

class Ball(pygame.sprite.Sprite):
	def __init__(self, image, position, speed, bg_size):
		# Call the parent class (Sprite) constructor
		pygame.sprite.Sprite.__init__(self)

		self.image = pygame.image.load(image).convert_alpha()
		self.rect = self.image.get_rect()
		#小球的位置
		self.rect.left, self.rect.top = position
		self.speed = speed
		self.width, self.height = bg_size[0], bg_size[1]

	'''
	Pygame.Rect.move():
		moves the rectangle 
		move(x, y) -> Rect
		Returns a new rectangle that is moved by the given offset. The
		x and y arguments can be any integer value, positive or 
		negative.
		可以在Rect对象的move方法中添加可正可负的两元素
		如果要实现小球的移动,则要在类中添加一个move()方法,并且在绘图的时候调用小球
		对象的move()方法
	'''
	def move(self):
		self.rect = self.rect.move(self.speed)

		#类似实现贪吃蛇穿入墙壁从对面墙壁出来(左右方向)
		if self.rect.right < 0:
			self.rect.left = self.width
		elif self.rect.left > self.width:
			self.rect.right = 0

		#(上下方向) 从下往上 和 从上往下
		elif self.rect.bottom < 0:
			self.rect.top = self.height
		elif self.rect.top > self.height:
			self.rect.bottom = 0


def main():
	pygame.init()

	ball_image = "gray_ball.png"
	bg_image = "background.png"

	running = True

	bg_size = width, height = 1024, 681
	screen = pygame.display.set_mode(bg_size)
	pygame.display.set_caption("Play the Ball")

	#.png格式可以加入apha通道
	background = pygame.image.load(bg_image).convert_alpha()

	balls = []

	for i in range(5):
		#球的尺寸是100*100 随机产生小球的位置
		position = randint(0, width-100), randint(0, height-100)
		#两个元素的一个列表,表示x轴和y轴方向的速度
		speed = [randint(-10, 10), randint(-10, 10)]
		#实例化小球对象 分别传入Surface对象 位置二元组 速度两元素列表
		ball = Ball(ball_image, position, speed, bg_size)
		balls.append(ball) #将小球加入到小球列表中

	# CLock()对象用来设置小球的帧率
	clock = pygame.time.Clock()

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

		screen.blit(background, (0, 0))

		for each in balls:
			each.move()
			screen.blit(each.image, each.rect) 

		pygame.display.flip() #将显示缓冲区中的数据刷入显示器中
		clock.tick(30)




if __name__ == "__main__":
	main()


下半节介绍碰撞检测和如何加入一些声音元素,并进一步完善界面

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youaresherlock

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值