初中生自学编程,随手写的小游戏:飞翔的小鸟
话不多说,代码如下:
import sys
import pygame
import random
from time import sleep as time_sleep
from pathlib import Path
states_top = 0
path = Path('states.txt')
path.write_text(str(states_top))
class Run:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((800, 700))
self.background_image = pygame.image.load("background-night.png")
self.background_image = pygame.transform.scale(self.background_image, (800, 700))
self.exit = False
self.start = False
self.box = Box(self)
self.game_states = GameStates(self)
self.button = Button(self)
self.top_pillars = []
self.bottom_pillars = []
self.top_pillar_num = 0
self.bottom_pillars_num = 0
self.pillar_height = random.randint(100, 500)
pygame.display.set_caption('Jump')
self.clock = pygame.time.Clock()
def run_game(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP or event.key == pygame.K_SPACE:
self.box.jumping = False
elif event.key == pygame.K_q:
sys.exit()
elif event.type == pygame.KEYUP:
self.box.jumping = True
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
if self.button.click_button(x, y):
self.start = True
self.draw()