added player drawing onto the screen
This commit is contained in:
parent
bfd9971cfb
commit
ef4695aae2
4 changed files with 57 additions and 2 deletions
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,8 @@
|
||||||
SCREEN_WIDTH = 1280
|
SCREEN_WIDTH = 1280
|
||||||
SCREEN_HEIGHT = 720
|
SCREEN_HEIGHT = 720
|
||||||
|
|
||||||
|
PLAYER_RADIUS = 20
|
||||||
|
PLAYER_TURN_SPEED = 300
|
||||||
ASTEROID_MIN_RADIUS = 20
|
ASTEROID_MIN_RADIUS = 20
|
||||||
ASTEROID_KINDS = 3
|
ASTEROID_KINDS = 3
|
||||||
ASTEROID_SPAWN_RATE = 0.8 # seconds
|
ASTEROID_SPAWN_RATE = 0.8 # seconds
|
||||||
|
|
|
||||||
12
main.py
12
main.py
|
|
@ -1,5 +1,6 @@
|
||||||
import pygame
|
import pygame
|
||||||
from constants import *
|
from constants import *
|
||||||
|
from player import Player
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("Starting asteroids!")
|
print("Starting asteroids!")
|
||||||
|
|
@ -7,11 +8,18 @@ def main():
|
||||||
print(f"Screen height: {SCREEN_HEIGHT}")
|
print(f"Screen height: {SCREEN_HEIGHT}")
|
||||||
pygame.init()
|
pygame.init()
|
||||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
dt = 0
|
||||||
|
p1 = Player(SCREEN_WIDTH/2,SCREEN_HEIGHT/2)
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
return
|
return
|
||||||
screen.fill((0, 0, 0)) # simplified fill call
|
screen.fill((0, 0, 0))
|
||||||
pygame.display.flip() # fixed typo in flip
|
p1.draw(screen)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
tick = clock.tick(60)
|
||||||
|
dt = tick/1000
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
23
player.py
Normal file
23
player.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import circleshape
|
||||||
|
import constants
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
class Player(circleshape.CircleShape):
|
||||||
|
def __init__(self,x,y):
|
||||||
|
super().__init__(x,y,constants.PLAYER_RADIUS)
|
||||||
|
self.rotation = 0
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def rotate(self,dt):
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue