-
Notifications
You must be signed in to change notification settings - Fork 0
/
welcome.py
106 lines (87 loc) · 2.59 KB
/
welcome.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import random
import pygame
import os
WIDTH = 800
HEIGHT = 600
FPS = 60
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
#asset folders
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder,"res/images")
font_name = pygame.font.match_font("Arial Black")
def draw_text(surf, text, size, x, y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop = (x,y)
surf.blit(text_surface, text_rect)
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join(img_folder,"normal.png"))
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH/2, HEIGHT/2)
self.speedX = 0
self.speedY = 0
def update(self):
self.speedX = 0
self.speedY = 0
keyStat = pygame.key.get_pressed()
# Movement handling
if keyStat[pygame.K_w]:
self.speedY -= 3
if keyStat[pygame.K_CAPSLOCK]:
self.speedY *= 2
if keyStat[pygame.K_s]:
self.speedY += 3
if keyStat[pygame.K_CAPSLOCK]:
self.speedY *= 2
if keyStat[pygame.K_a]:
self.speedX -= 3
if keyStat[pygame.K_CAPSLOCK]:
self.speedX *= 2
if keyStat[pygame.K_d]:
self.speedX += 3
if keyStat[pygame.K_CAPSLOCK]:
self.speedX *= 2
# Screen boundaries collision
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
self.rect.x += self.speedX
self.rect.y += self.speedY
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("game")
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
running = True
while running:
# sync FPS
clock.tick(FPS)
# Process input(Event)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#update
all_sprites.update()
#render / draw
screen.fill(BLUE)
all_sprites.draw(screen)
draw_text(screen,str(clock), 18, WIDTH/2,5)
#AFTER drawing everything
pygame.display.flip()
pygame.quit()