typegame
import pygame
import sys
import random
# 窗口
class Window:
def __init__(self):
self.length = 1180
self.height = 775
self.apple_fre = 0
self.apples = pygame.sprite.Group()
self.background = pygame.image.load("./image/forest.jpg")
self.background = pygame.transform.smoothscale(self.background,(self.length,self.height))
self.screen = pygame.display.set_mode((self.length,self.height))
self.rect = self.screen.get_rect()
pygame.init()
def create_apple(self):
self.apple_fre += 1
if self.apple_fre > 100:
apple = Apple()
self.apples.add(apple)
self.apple_fre = 0
def move_apple(self):
for x in self.apples:
x.move()
if x.rect.bottom >= self.rect.bottom:
self.apples.remove(x)
break
# 显示
def show(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.create_apple()
self.move_apple()
self.screen.blit(self.background,(0,0))
self.apples.draw(self.screen)
pygame.display.update()
# 苹果
class Apple(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.size = (50,50)
self.image = pygame.image.load("./image/apple.png")
self.image = pygame.transform.smoothscale(self.image,self.size)
self.speed = 1
self.rect = self.image.get_rect()
self.rect.centerx = random.randint(0,1180)
def move(self):
self.rect.bottom += self.speed
window = Window()
window.show()