''' pg_draw_circle_save101py
draw a blue solid circle on a white background
save the drawing to an image file
tested with Python 27 and PyGame 192 by vegaseat 16may2013
'''
import pygame as pg
# pygame uses (r, g, b) color tuples
white = (255, 255, 255)
blue = (0, 0, 255)
width = 300
height = 300
# create the display window
win = pgdisplayset_mode((width, height))
# optional title bar caption
pgdisplayset_caption("Pygame draw circle and save")
# default background is black, so make it white
winfill(white)
# draw a blue circle
# center coordinates (x, y)
radius = min(center)
# width of 0 (default) fills the circle
# otherwise it is thickness of outline
width = 0
# drawcircle(Surface, color, pos, radius, width)
pgdrawcircle(win, blue, center, radius, width)
# now save the drawing
# can save as bmp tga png or jpg
fname = "circle_bluepng"
pgimagesave(win, fname)
print("file {} has been saved"format(fname))
# update the display window to show the drawing
pgdisplayflip()
# event loop and exit conditions
# (press escape key or click window title bar x to exit)
while True:
for event in pgeventget():
if eventtype == pgQUIT:
# most reliable exit on x click
pgquit()
raise SystemExit
elif eventtype == pgKEYDOWN:
# optional exit with escape key
if eventkey == pgK_ESCAPE:
pgquit()
raise SystemExit黑屏可能是因为更新屏幕的方法有问题。代码中的两行设置屏幕大小的代码重复了,并且最后一行调用的是 selfbg_color 而不是 selfsettingsbg_color,这也可能是导致黑屏的原因。请尝试更改代码:
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
def __init__(self):
pygameinit()
selfsettings=Settings()
selfscreen=pygamedisplayset_mode((selfsettingsscreen_width, selfsettingsscreen_height))
pygamedisplayset_caption("Alien Invasion")
selfship=Ship(self)
def run_game(self):
while True:
self_check_events()
self_update_screen()
def _check_events(self):
for event in pygameeventget():
if eventtype == pygameQUIT:
sysexit()
elif eventtype == pygameKEYDOWN:
if eventkey == pygameK_RIGHT:
selfshiprectx+=1
def _update_screen(self):
selfscreenfill(selfsettingsbg_color)
selfshipblitme()
pygamedisplayflip()
if __name__ == '__main__':
ai = AlienInvasion()
airun_game()要用Python画五一海报,需要用到Python的绘图库,比如matplotlib、Pillow等。以下是一个简单的例子,演示如何使用Python和Pillow库来制作五一海报:
from PIL import Image, ImageDraw, ImageFont
# 创建一个空白的画布,大小为800x600,背景色为白色
canvas = Imagenew('RGB', (800, 600), (255, 255, 255))
# 在画布上添加五一节日祝福语
font = ImageFonttruetype('arialttf', 48) # 设置字体样式和大小
draw = ImageDrawDraw(canvas)
drawtext((200, 100), '五一快乐!', fill=(0, 0, 0), font=font) # 设置文字位置、颜色和字体
# 在画布上添加五星红旗图案
flag = Imageopen('flagpng') # 读取五星红旗
flag = flagresize((200, 133)) # 设置大小为200x133
canvaspaste(flag, (300, 300)) # 将粘贴到画布上
# 保存并展示画布
canvassave('maydaypng')
canvasshow()
在这个例子中,我们首先使用Pillow库创建了一个800x600大小的空白画布,并在画布上添加了五一节日祝福语和五星红旗图案。最后使用save()方法将画布保存为PNG格式的,并使用show()方法展示。在实际 *** 作中,您需要根据自己的需求来修改代码,比如更换祝福语、调整字体大小和颜色、更换等。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)