本文来源吾爱破解论坛
本帖最后由 ymhld 于 2020-1-8 07:57 编辑 QQ图片20200107223946.png (582.5 KB, 下载次数: 1)
下载附件
保存到相册
做习题弄出来的小东西群星闪耀,过年了,来点过年的气氛
#-*-coding:GBK -*-
#13.1
import sys
import pygame
from random import randint
from star import Star
class starvasion:
"""创建星星类."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
print (self.settings)
#self.screen = pygame.display.set_mode((self.settings.screen_width ,self.settings.screen_height),0,32)
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("群星闪耀")
self.stars = pygame.sprite.Group()
self._create_fleet()
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
#self._update_stars()
self._update_screen()
def _check_events(self):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
def _check_keydown_events(self, event):
"""Respond to keypresses."""
if event.key == pygame.K_q:
sys.exit()
def _create_fleet(self):
"""Create the fleet of stars."""
# Create an star and find the number of stars in a row.
# Spacing between each star is equal to one star width.
star = Star(self)
star_width, star_height = star.rect.size
available_space_x = self.settings.screen_width - (2 * star_width)
number_stars_x = available_space_x // (2 * star_width)
# Determine the number of rows of stars that fit on the screen.
available_space_y = (self.settings.screen_height -
(3 * star_height) - 100)
number_rows = available_space_y // (2 * star_height)
# Create the full fleet of stars.
for row_number in range(number_rows):
for star_number in range(number_stars_x):
self._create_star(star_number, row_number)
def _create_star(self, star_number, row_number):
"""Create an star and place it in the row."""
self.randomnumber = randint(-30,30)
star = Star(self)
star_width, star_height = star.rect.size
star.x = star_width + 2 * star_width * star_number
star.rect.x = star.x+self.randomnumber
self.randomnumber = randint(-30,30)
star.rect.y = star.rect.height + 2 * star.rect.height * row_number+self.randomnumber
self.stars.add(star)
def _update_screen(self):
"""Update images on the screen, and flip to the new screen."""
self.screen.fill(self.settings.bg_color)
self.stars.draw(self.screen)
pygame.display.flip()
class Settings:
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = starvasion()
ai.run_game()
star.py
#-*-coding:GBK -*-
#13.1-1
import pygame
from pygame.sprite import Sprite
from random import randint
class Star(Sprite):
"""A class to represent a single alien in the fleet."""
def __init__(self, ai_game):
"""Initialize the alien and set its starting position."""
super().__init__()
self.screen = ai_game.screen
# Load the alien image and set its rect attribute.
self.image = pygame.image.load('star.png')
self.width,self.height = self.image.get_size()
self.randomscale = int(randint(1,10))
self.image = pygame.transform.smoothscale(self.image,(
self.width//(2*self.randomscale),
self.height//(2*self.randomscale)))
self.rect = self.image.get_rect()
# Start each new alien near the top left of the screen.
self.rect.x = self.rect.width
self.rect.y = self.rect.height
# Store the alien's exact horizontal position.
self.x = float(self.rect.x)
https://www.lanzous.com/i8j3ted
版权声明:
本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。
- 上一篇: [Python]推送文档到kindle
- 下一篇: 免费下载QQ音乐高品质、无损音质