Python Pygame 基本程序
我在最后一天左右一直在尝试学习 pygame,并尝试编写一个基本程序,该程序只有一个从屏幕顶部落下的叶子的小图像。 当我运行它时,什么也没有出现,我想我在执行此操作时遗漏了一些明显的东西。 (我可以说这也是一种非常低效的方法,因此我们将不胜感激!)
这是代码:
import pygame
from pygame.locals import *
import random
pygame.init()
class Leaf:
def __init__(self):
self.leafimage = pygame.image.load('fallingleaf.jpg').convert()
self.leafrect = self.leafimage.get_rect()
xpos = random.randint(0, 640)
self.leafrect.midtop = (xpos, 0)
def move(self):
self.leafrect = self.leafrect.move([0, 1])
def main():
width= 640
heigth = 480
dimensions = (width, heigth)
screen = pygame.display.set_mode(dimensions)
pygame.display.set_caption('Some Epic Pygame Stuff')
clock = pygame.time.Clock()
leaves = []
for i in range(5):
leaves.append(Leaf())
running = 1
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = 0
for i in leaves:
i.move()
screen.blit(i.leafimage, i.leafrect)
screen.fill((255, 255, 255))
pygame.display.flip()
if __name__ == '__main__': main()
I have been trying to learn pygame the last day or so, and tried to write a basic program that just has a small image of a leaf falling from the top of the screen.
Nothing appears when I run it, and I imagine I'm missing something obvious with how I'm doing this. (I can tell this is a very inefficient way of doing this as well, so tips would be appreciated!)
Here's the code:
import pygame
from pygame.locals import *
import random
pygame.init()
class Leaf:
def __init__(self):
self.leafimage = pygame.image.load('fallingleaf.jpg').convert()
self.leafrect = self.leafimage.get_rect()
xpos = random.randint(0, 640)
self.leafrect.midtop = (xpos, 0)
def move(self):
self.leafrect = self.leafrect.move([0, 1])
def main():
width= 640
heigth = 480
dimensions = (width, heigth)
screen = pygame.display.set_mode(dimensions)
pygame.display.set_caption('Some Epic Pygame Stuff')
clock = pygame.time.Clock()
leaves = []
for i in range(5):
leaves.append(Leaf())
running = 1
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = 0
for i in leaves:
i.move()
screen.blit(i.leafimage, i.leafrect)
screen.fill((255, 255, 255))
pygame.display.flip()
if __name__ == '__main__': main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能不想要这个顺序:
绘制叶子,然后用白色填充整个屏幕,然后显示屏幕。
填充屏幕,然后绘制叶子,然后
flip()
You probably don't want this sequence:
You draw the leaves, and then fill the entire screen with white, and then show the screen.
fill the screen, then draw the leaves, then
flip()