starteds pygame shmup tutorial
This commit is contained in:
parent
fb79384114
commit
30844e3bae
94
PyGame/practice/gameprac/smup.py
Normal file
94
PyGame/practice/gameprac/smup.py
Normal file
|
@ -0,0 +1,94 @@
|
|||
import pygame
|
||||
import random
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
WIDTH = 480
|
||||
HEIGHT = 600
|
||||
WINDOW_SIZE = (WIDTH, HEIGHT)
|
||||
FPS = 60
|
||||
|
||||
WHITE = (255, 255, 255)
|
||||
BLACK = (0, 0, 0)
|
||||
RED = (255, 0, 0)
|
||||
GREEN = (0, 255, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
|
||||
pygame.init()
|
||||
pygame.mixer.init()
|
||||
screen = pygame.display.set_mode(WINDOW_SIZE)
|
||||
pygame.display.set_caption("Shmup")
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
all_sprites = pygame.sprite.Group()
|
||||
|
||||
class PLayer(pygame.sprite.Sprite):
|
||||
def __init__(self):
|
||||
pygame.sprite.Sprite.__init__(self)
|
||||
self.image = pygame.Surface((50, 40))
|
||||
self.image.fill(GREEN)
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.centerx =WIDTH / 2
|
||||
self.rect.bottom = HEIGHT - 10
|
||||
self.speedx = 0
|
||||
|
||||
def update(self):
|
||||
self.speedx = 0
|
||||
keystate = pygame.key.get_pressed()
|
||||
if keystate[pygame.K_a]:
|
||||
self.speedx = -8
|
||||
if keystate[pygame.K_d]:
|
||||
self.speedx = 8
|
||||
self.rect.x += self.speedx
|
||||
if self.rect.right > WIDTH:
|
||||
self.rect.right = WIDTH
|
||||
if self.rect.left < 0:
|
||||
self.rect.left = 0
|
||||
|
||||
class Mob(pygame.sprite.Sprite):
|
||||
def __init__(self):
|
||||
pygame.sprite.Sprite.__init__(self)
|
||||
self.image = pygame.Surface((30, 40))
|
||||
self.image.fill(RED)
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.x = random.randrange(0, WIDTH - self.rect.width)
|
||||
self.rect.y = random.randrange(-100, -40)
|
||||
self.speedy = random.randrange(1, 8)
|
||||
self.speedx = random.randrange(-3, 3)
|
||||
|
||||
|
||||
def update(self):
|
||||
self.rect.y += self.speedy
|
||||
self.rect.x += self.speedx
|
||||
if self.rect.top > HEIGHT + 10:
|
||||
self.rect.x = random.randrange(0, WIDTH - self.rect.width)
|
||||
self.rect.y = random.randrange(-100, -40)
|
||||
self.speed = random.randrange(1, 8)
|
||||
|
||||
all_sprites = pygame.sprite.Group()
|
||||
mobs = pygame.sprite.Group()
|
||||
player = PLayer()
|
||||
all_sprites.add(player)
|
||||
for i in range(8):
|
||||
m = Mob()
|
||||
all_sprites.add(m)
|
||||
mobs.add(m)
|
||||
|
||||
running = True
|
||||
while running:
|
||||
clock.tick(FPS)
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
all_sprites.update()
|
||||
|
||||
screen.fill(BLACK)
|
||||
all_sprites.draw(screen)
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
|
||||
pygame.quit()
|
|
@ -1,6 +1,6 @@
|
|||
import pygame
|
||||
import random
|
||||
import os
|
||||
|
||||
|
||||
|
||||
WIDTH = 360
|
||||
|
@ -19,7 +19,6 @@ pygame.mixer.init()
|
|||
screen = pygame.display.set_mode(WINDOW_SIZE)
|
||||
pygame.display.set_caption("My Game")
|
||||
clock = pygame.time.Clock()
|
||||
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
|
||||
running = True
|
||||
while running:
|
||||
clock.tick(FPS)
|
||||
|
|
Loading…
Reference in New Issue
Block a user