drawing player
This commit is contained in:
0
__init__.py
Normal file
0
__init__.py
Normal file
22
circleshape.py
Normal file
22
circleshape.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import pygame
|
||||
|
||||
# Base class for game objects
|
||||
class CircleShape(pygame.sprite.Sprite):
|
||||
def __init__(self, x, y, radius):
|
||||
# we will be using this later
|
||||
if hasattr(self, "containers"):
|
||||
super().__init__(self.containers)
|
||||
else:
|
||||
super().__init__()
|
||||
|
||||
self.position = pygame.Vector2(x, y)
|
||||
self.velocity = pygame.Vector2(0, 0)
|
||||
self.radius = radius
|
||||
|
||||
def draw(self, screen):
|
||||
# sub-classes must override
|
||||
pass
|
||||
|
||||
def update(self, dt):
|
||||
# sub-classes must override
|
||||
pass
|
||||
@@ -1,6 +1,6 @@
|
||||
SCREEN_WIDTH = 1280
|
||||
SCREEN_HEIGHT = 720
|
||||
|
||||
PLAYER_RADIUS = 20
|
||||
ASTEROID_MIN_RADIUS = 20
|
||||
ASTEROID_KINDS = 3
|
||||
ASTEROID_SPAWN_RATE = 0.8 # seconds
|
||||
|
||||
6
main.py
6
main.py
@@ -2,6 +2,7 @@ import pygame
|
||||
import sys
|
||||
|
||||
from constants import *
|
||||
from player import Player
|
||||
|
||||
def main():
|
||||
print("Starting Asteroids!")
|
||||
@@ -19,12 +20,17 @@ def main():
|
||||
time = pygame.time.Clock()
|
||||
dt = 0
|
||||
|
||||
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
|
||||
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return
|
||||
|
||||
pygame.Surface.fill(screen, (0,0,0))
|
||||
|
||||
player.draw(screen)
|
||||
|
||||
pygame.display.flip() #refresh screen
|
||||
|
||||
dt = time.tick(60) / 1000 #conveted to ms
|
||||
|
||||
22
player.py
Normal file
22
player.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import pygame
|
||||
from circleshape import CircleShape
|
||||
from constants import PLAYER_RADIUS
|
||||
|
||||
|
||||
class Player(CircleShape):
|
||||
def __init__(self, x, y):
|
||||
super().__init__(x, y, PLAYER_RADIUS)
|
||||
self.rotation = 0
|
||||
|
||||
# in the player class
|
||||
def triangle(self):
|
||||
forward = pygame.Vector2(0, 1).rotate(self.rotation)
|
||||
right = pygame.Vector2(0, 1).rotate(self.rotation + 90) * self.radius / 1.5
|
||||
a = self.position + forward * self.radius
|
||||
b = self.position - forward * self.radius - right
|
||||
c = self.position - forward * self.radius + right
|
||||
return [a, b, c]
|
||||
|
||||
def draw(self, screen):
|
||||
pygame.draw.polygon(screen, (255,255,255), self.triangle(), 2)
|
||||
|
||||
Reference in New Issue
Block a user