《Pygame游戏编程入门》学习——第5章 Math和Graphics:Analog Clock游戏


第5章 挑战1

问题1. Circle示例程序是典型的视频游戏中的众多问题的解决方案。要更多地体验围绕圆周移动的相关算法,修改该程序,以使得在每个角度绘制不同的形状,而不是绘制一个小的填充的圆。

思路:建立DrawCircle类,添加成员变量pos_xpos_y(绕圆周不断移动的坐标),半径radius、角度angle、颜色color,然后在draw()方法中,实现以pos_xpos_y为中心,相距5像素的地方画多边形。

代码:

import sys, random, math, pygame, RGB
from pygame.locals import *

class DrawCircle():
	def __init__( self ):
		self.pos_x, self.pos_y = screen_width / 2, screen_height / 2
		self.radius = 200
		self.angle = 360
		self.color = random.randint( 0, 255 ), random.randint( 0, 255 ), random.randint( 0, 255 )

	def draw( self ):
		self.angle += 1
		if self.angle >= 360:
			self.angle = 0
			self.color = random.randint( 0, 255 ), random.randint( 0, 255 ), random.randint( 0, 255 )
		
		x = math.cos( math.radians( self.angle ) ) * self.radius
		y = math.sin( math.radians( self.angle ) ) * self.radius
		pos = ( int( self.pos_x + x ), int( self.pos_y + y ) )
		print( pos )
		
		x1, y1 = int( self.pos_x + x ) - 5, int( self.pos_y + y ) - 5
		x2, y2 = int( self.pos_x + x ) + 5, int( self.pos_y + y ) - 5
		x3, y3 = int( self.pos_x + x ) - 5, int( self.pos_y + y ) + 5
		x4, y4 = int( self.pos_x + x ) + 5, int( self.pos_y + y ) + 5	
		points = [ ( x1, y1 ), ( x2, y2 ), ( x3, y3 ), ( x4, y4 ) ]
		
		pygame.draw.polygon( screen, self.color, points, 0 )
		pygame.display.update()
	
screen_width, screen_height = 600, 500
pygame.init()
screen = pygame.display.set_mode( ( screen_width, screen_height ) )
pygame.display.set_caption( '几何形状画圆示例' )
screen.fill( RGB.Blue1 )
c = DrawCircle()

while True:
	for event in pygame.event.get():
		if event.type == QUIT:
			sys.exit()
	
	keys = pygame.key.get_pressed()
	if keys[K_ESCAPE]:
		sys.exit()
	
	c.draw()	

运行结果:
在这里插入图片描述


问题2. Analog Clock示例程序现在仅仅是能够工作,而忽略了美观方面的要求。看看你是否能用更好的颜色使它更好看一些,可能要使用不同的背景颜色,而且数字和钟表指针使用不同的大小。

思路:构建AnalogClock类,参数同上。添加draw()方法,实现设置背景颜色、显示钟表数字。

代码如下:

import sys, random, math, pygame, RGB
from pygame.locals import *
from datetime import datetime, date, time

class AnalogClock():
	def __init__( self ):
		self.pos_x, self.pos_y = screen_width / 2, screen_height / 2
		self.radius = 200
		self.angle = 360
		self.color = random.randint( 0, 255 ), random.randint( 0, 255 ), random.randint( 0, 255 )		

	def print_text( self, font, x, y, text, color=RGB.White, shadow=True ):
		imgText = font.render( text, True, color )
		screen.blit( imgText, ( x, y ) )

	def show( self ):
		screen.fill( RGB.Blue1 )	
		pygame.draw.circle( screen, RGB.Black, ( self.pos_x, self.pos_y ), self.radius, 0 )	# pos-圆心,10-半径,0-填充。
		pygame.draw.circle( screen, RGB.White, ( self.pos_x, self.pos_y ), self.radius, 6 )	

		for n in range( 1, 13 ):
			radian = math.radians( -92 + n * 30 )	# 2度是打印文字的修正值。
			x = math.cos( radian ) * ( self.radius - 20 ) - 10
			y = math.sin( radian ) * ( self.radius - 20 ) - 15
			self.print_text( font1, self.pos_x + x, self.pos_y + y, str( n ) )
	
		today = datetime.today()	
		current_hour = str( today.hour ) + ':' + str( today.minute ) + ':' + str( today.second )
		self.print_text( font1, 0, 0, current_hour )		

		# 时针	
		hour = today.hour % 12
		n = hour
		radian = math.radians( -90 + n * 30 + 30 * ( ( today.minute * 6 ) % 360 ) / 360 )
		x = math.cos( radian ) * ( self.radius - 80 )
		y = math.sin( radian ) * ( self.radius - 80 )	
		start = ( self.pos_x, self.pos_y )
		end = ( self.pos_x + x, self.pos_y + y )
		pygame.draw.line( screen, RGB.Pink, start, end, 25 )
		
		# 分针
		n = today.minute
		radian = math.radians( -90 + n * 6 + 6 * ( ( today.second * 6 ) % 360 ) / 360 )
		x = math.cos( radian ) * ( self.radius - 60 )
		y = math.sin( radian ) * ( self.radius - 60 )		
		start = ( self.pos_x, self.pos_y )
		end = ( self.pos_x + x, self.pos_y + y )
		pygame.draw.line( screen, RGB.Pink, start, end, 12 )

		# 秒针
		n = today.second
		radian = math.radians( -90 + n * 6 )
		x = math.cos( radian ) * ( self.radius - 40 )
		y = math.sin( radian ) * ( self.radius - 40 )
		
		start = ( self.pos_x, self.pos_y )
		end = ( self.pos_x + x, self.pos_y + y )
		pygame.draw.line( screen, RGB.Pink, start, end, 6 )	
		
		# 中心圆
		pygame.draw.circle( screen, RGB.White, ( self.pos_x, self.pos_y ), 10, 0 )
		
		pygame.display.update()

screen_width, screen_height = 600, 500
pygame.init()
screen = pygame.display.set_mode( ( screen_width, screen_height ) )
pygame.display.set_caption( '时钟' )
font1 = pygame.font.SysFont( 'Arial', 24 )
a = AnalogClock()

while True:
	for event in pygame.event.get():
		if event.type == QUIT:
			sys.exit()
	
	keys = pygame.key.get_pressed()
	if keys[K_ESCAPE]:
		sys.exit()

	a.show()

模块RGB.py的部分代码如下:

White = ( 255, 255, 255 )
Blue1 = ( 0, 0, 255 )
Pink = ( 255, 192, 203 )
Black = ( 0, 0, 0 )

以上是本游戏用到的颜色,全部RGB的颜色代码可以查RGB颜色表

运行结果:
在这里插入图片描述


  1. [美] Jonathan S. Harbour 著,李强 译,Python游戏编程入门,人民邮电出版社,2015.1 ↩︎

猜你喜欢

转载自blog.csdn.net/quanet_033/article/details/126957675