[雪峰磁针石博客]python绘图作业:使用pygame库画房子

简介:

使用pygame库画如下房子

图片.png

参考资料

代码

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 技术支持:https://www.jianshu.com/u/69f40328d4f0 
# 技术支持 https://china-testing.github.io/
# https://github.com/china-testing/python-api-tesing/blob/master/practices/pygame_house.py
# 项目实战讨论QQ群630011153 144081101
# CreateDate: 2018-12-01
import pygame
pygame.init()
screen = pygame.display.set_mode((640,480))

#used http://colorpicker.com/ to find RGB colors

def draw_tree(x,y):
    #tree trunk (50 wide and 100 tall)
    pygame.draw.rect(screen,(117,90,0),(x,y-100,50,100))
    #leaves are a circle
    pygame.draw.circle(screen,(27,117,0),(x+25,y-120),50)

def draw_house(x,y):
    #pink house
    pygame.draw.rect(screen,(255,171,244),(x,y-180,200,180))
    #brown door
    pygame.draw.rect(screen,(89,71,0),(x+80,y-60,40,60))
    #yellow door knob
    pygame.draw.circle(screen,(255,204,0),(x+112,y-30),4)
    #triangle roof
    pygame.draw.polygon(screen, (125,125,125), ( (x,y-180),(x+100,y-250),(x+200,y-180) ) )
    draw_window(x+20,y-90)
    draw_window(x+130,y-90)

def draw_window(x,y):
    #glass
    pygame.draw.rect(screen,(207,229,255),(x,y-50,50,50))
    #frame
    pygame.draw.rect(screen,(0,0,0),(x,y-50,50,50),5)
    pygame.draw.rect(screen,(0,0,0),(x+23,y-50,5,50))
    pygame.draw.rect(screen,(0,0,0),(x,y-27,50,5))

#this function is able to draw clouds of different sizes
def draw_cloud(x,y,size):
    #put int() around any multiplications by decimals to get rid of this warning:
    #DeprecationWarning: integer argument expected, got float
    pygame.draw.circle(screen,(255,255,255),(x,y),int(size*.5))
    pygame.draw.circle(screen,(255,255,255),(int(x+size*.5),y),int(size*.6))
    pygame.draw.circle(screen,(255,255,255),(x+size,int(y-size*.1)),int(size*.4))


#green ground
pygame.draw.rect(screen,(0,160,3),(0,400,640,80))
#light blue sky
pygame.draw.rect(screen,(135,255,255),(0,0,640,400))

draw_tree(60,400) #x and y location are the bottom left of tree trunk
draw_tree(550,400)

draw_house(225,400)

draw_cloud(60,120,80)
draw_cloud(200,50,40)
draw_cloud(450,100,120)


pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()
相关文章
|
2天前
|
Python
在Python中绘制K线图,可以使用matplotlib和mplfinance库
使用Python的matplotlib和mplfinance库可绘制金融K线图。mplfinance提供便利的绘图功能,示例代码显示如何加载CSV数据(含开盘、最高、最低、收盘价及成交量),并用`mpf.plot()`绘制K线图,设置类型为'candle',显示移动平均线(mav)和成交量信息。可通过调整参数自定义图表样式,详情参考mplfinance文档。
12 2
|
3天前
|
机器学习/深度学习 边缘计算 TensorFlow
【Python机器学习专栏】Python机器学习工具与库的未来展望
【4月更文挑战第30天】本文探讨了Python在机器学习中的关键角色,重点介绍了Scikit-learn、TensorFlow和PyTorch等流行库。随着技术进步,未来Python机器学习工具将聚焦自动化、智能化、可解释性和可信赖性,并促进跨领域创新,结合云端与边缘计算,为各领域应用带来更高效、可靠的解决方案。
|
3天前
|
Serverless Python
使用Python的pandas和matplotlib库绘制移动平均线(MA)示例
使用Python的pandas和matplotlib库绘制移动平均线(MA)示例:加载CSV数据,计算5日、10日和20日MA,然后在K线图上绘制。通过`rolling()`计算平均值,`plot()`函数展示图表,`legend()`添加图例。可利用matplotlib参数自定义样式。查阅matplotlib文档以获取更多定制选项。
14 1
|
3天前
|
数据采集 SQL 数据挖掘
Python数据分析中的Pandas库应用指南
在数据科学和分析领域,Python语言已经成为了一种非常流行的工具。本文将介绍Python中的Pandas库,该库提供了强大的数据结构和数据分析工具,使得数据处理变得更加简单高效。通过详细的示例和应用指南,读者将了解到如何使用Pandas库进行数据加载、清洗、转换和分析,从而提升数据处理的效率和准确性。
|
3天前
|
SQL 关系型数据库 MySQL
使用Python的pymysql库连接MySQL,执行CRUD操作
使用Python的pymysql库连接MySQL,执行CRUD操作:安装pymysql,然后连接(host='localhost',user='root',password='yourpassword',database='yourdatabase'),创建游标。查询数据示例:`SELECT * FROM yourtable`;插入数据:`INSERT INTO yourtable...`;更新数据:`UPDATE yourtable SET...`;删除数据:`DELETE FROM yourtable WHERE...`。
10 0
|
8天前
|
存储 人工智能 数据处理
Python:编程的艺术与科学的完美交融
Python:编程的艺术与科学的完美交融
13 1
|
3天前
|
测试技术 调度 索引
python编程中常见的问题
【4月更文挑战第23天】
15 2
|
4天前
|
网络协议 算法 网络架构
Python网络编程之udp编程、黏包以及解决方案、tcpserver
Python网络编程之udp编程、黏包以及解决方案、tcpserver
|
4天前
|
机器学习/深度学习 数据挖掘 算法框架/工具
Python:编程的艺术与魅力
Python:编程的艺术与魅力
15 3
|
4天前
|
机器学习/深度学习 数据可视化 数据挖掘
实用技巧:提高 Python 编程效率的五个方法
本文介绍了五个提高 Python 编程效率的实用技巧,包括使用虚拟环境管理依赖、掌握列表推导式、使用生成器提升性能、利用装饰器简化代码结构以及使用 Jupyter Notebook 进行交互式开发。通过掌握这些技巧,可以让你的 Python 编程更加高效。