-
Notifications
You must be signed in to change notification settings - Fork 8
/
basicpolygonpygame.py
59 lines (54 loc) · 1.84 KB
/
basicpolygonpygame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pygame as pyg
import sys
#Sprite class
class Sprite(pyg.sprite.Sprite):
def __init__(self, pos):
pyg.sprite.Sprite.__init__(self)
self.image = pyg.Surface([20, 20])
self.image.fill((255, 0, 255))
self.rect = self.image.get_rect()
self.rect.center = pos
def main():
pyg.init()
clock = pyg.time.Clock()
fps = 50
bg = [0, 0, 0]
size =[300, 300]
screen = pyg.display.set_mode(size)
player = Sprite([40, 50])
# Define keys for player movement
player.move = [pyg.K_LEFT, pyg.K_RIGHT, pyg.K_UP, pyg.K_DOWN]
player.vx = 5
player.vy = 5
wall = Sprite([100, 60])
wall_group = pyg.sprite.Group()
wall_group.add(wall)
player_group = pyg.sprite.Group()
player_group.add(player)
while True:
for event in pyg.event.get():
if event.type == pyg.QUIT:
return False
key = pyg.key.get_pressed()
for i in range(2):
if key[player.move[i]]:
player.rect.x += player.vx * [-1, 1][i]
for i in range(2):
if key[player.move[2:4][i]]:
player.rect.y += player.vy * [-1, 1][i]
screen.fill(bg)
# first parameter takes a single sprite
# second parameter takes sprite groups
# third parameter is a kill command if true
hit = pyg.sprite.spritecollide(player, wall_group, True)
if hit:
# if collision is detected call a function to destroy
# rect
player.image.fill((255, 255, 255))
player_group.draw(screen)
wall_group.draw(screen)
pyg.display.update()
clock.tick(fps) //fps indicates frmes per second
pyg.quit()
sys.exit
if __name__ == '__main__':