Python 100 -- day 10 图形用户界面和游戏开发

目录

基于tkinter模块的GUI

实例

使用Pygame开发游戏

实例:大球吃小球


基于tkinter模块的GUI

使用tkinter开发GUI需要5个步骤

  1. 导入tkinter模块中我们需要的东西
  2. 创建一个顶层窗口对象并用它来承载整个GUI应用
  3. 在顶层窗口对象上添加GUI组件
  4. 通过代码将这些GUI组件的功能组织起来
  5. 进入主事件循环(main loop)

实例

import tkinter
import tkinter.messagebox

def main():
	flag = True

	def change_label_text():
		nonlocal flag
		flag = not flag
		color, msg = ('red', 'Hello world!')\
			if flag else ('blue', 'Goodbye, world!')
		label.config(text=msg, fg=color)

	def confirm_to_quit():
		if tkinter.messagebox.askokcancel('温馨提示', '确定要退出吗'):
			top.quit()

	top = tkinter.Tk()
	top.geometry('240x160')
	top.title('小游戏')
	label = tkinter.Label(top, text='Hello,world',
						font = 'Arial -32', fg = 'red')
	label.pack(expand = 1)
	panel = tkinter.Frame(top)

	button1 = tkinter.Button(panel, text='修改', command = change_label_text)
	button2 = tkinter.Button(panel, text='退出', command = confirm_to_quit)
	button1.pack(side='left')
	button2.pack(side='right')
	panel.pack(side='bottom')

	tkinter.mainloop()


if __name__ == '__main__':
	main()

使用Pygame开发游戏

实例:大球吃小球

  1. 在窗口中绘图
  2. 加载图像
  3. 实现动画效果
  4. 碰撞检测
  5. 事件处理
from enum import Enum, unique
from math import sqrt
from random import randint

import pygame

@unique
class Color(Enum):
	RED = (255,0,0)
	GREEN = (0,255,0)
	BULE = (0,0,255)
	BLACK = (0,0,0)
	WHITE = (255,255,255)
	GRAY = (242,242,242)

	@staticmethod
	def random_color():
		r = randint(0,255)
		g = randint(0,255)
		b = randint(0,255)
		return (r, g, b)

class Ball(object):
	def __init__(self, x, y, radius, sx, sy, color=Color.RED):
		self.x = x
		self.y = y
		self.radius = radius
		self.sx = sx
		self.sy = sy
		self.color = color
		self.alive = True

	def move(self, screen):
		self.x += self.sx
		self.y += self.sy
		if self.x - self.radius <= 0 or \
				self.x + self.radius >= screen.get_width():
			self.sx = -self.sx

		if self.y - self.radius <= 0 or \
				self.y + self.radius >= screen.get_height():
			self.sy = -self.sy

	def eat(self, other):
		if self.alive and other. alive and self != other:
			dx, dy = self.x - other.x, self.y - other.y
			distance = sqrt(dx**2 + dy**2)
			if distance < self.radius + other.radius \
					and self.radius > other.radius:
				other.alive = False
				self.radius = self.radius + int(other.radius * 0.146)

	def draw(self, screen):
		pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius, 0)


def main():
	balls = []
	pygame.init()
	screen = pygame.display.set_mode((800,600))
	#print(screen.get_width())
	#print(screen.get_height())
	pygame.display.set_caption('大球吃小球')
	running = True

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

			if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
				x, y = event.pos
				radius = randint(10,100)
				sx, sy = randint(-10,10), randint(-10,10)
				color = Color.random_color()
				ball = Ball(x, y, radius, sx, sy, color)
				balls.append(ball)
		screen.fill((255,255,255))
		for ball in balls:
			if ball.alive:
				ball.draw(screen)
			else:
				balls.remove(ball)
		pygame.display.flip()

		pygame.time.delay(50)
		for ball in balls:
			ball.move(screen)
			for other in balls:
				ball.eat(other)

if __name__ == '__main__':
	main()

学习pygame最好的教程是pygame的官网  https://www.pygame.org/news,若是想要开发3D游戏不妨看看Panda3D

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值