python从入门到实践做习题出来的小东西
本帖最后由 ymhld 于 2020-1-8 07:57 编辑做习题弄出来的小东西群星闪耀,过年了,来点过年的气氛
#-*-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()
https://static.52pojie.cn/static/image/hrline/1.gif
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.lanzouj.com/i8j3ted
这本书感觉挺不错的,大学时候选修发的这本书,后面用pygame照着做过飞机大战外星人{:301_978:},讲解的很有意思。学的也是怎么去做一款小游戏的逻辑,游戏各模块代码怎么拆分,怎么方便后续的优化修改 银.桑 发表于 2020-1-8 16:48
这本书感觉挺不错的,大学时候选修发的这本书,后面用pygame照着做过飞机大战外星人,讲解的很 ...
太难弄的就是空格、大小写,一不小心就弄错喽,然后就全错 nice{:1_893:} 我还记得我做的小飞机,最后一直出错 星光漫天好景致 厉害啊,膜拜大佬,向大佬学习 向楼主学习,好好学习,天天向上~~~!!!! 做的不错哦,楼主加油,期待更好的作品! 有进步就行 向你学习,学习的收获 很厉害,支持一下
页:
[1]
2