-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_1.py
More file actions
105 lines (88 loc) · 2.92 KB
/
code_1.py
File metadata and controls
105 lines (88 loc) · 2.92 KB
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
import asyncio
import pathlib
import os
import pygame
main_dir = str(pathlib.Path(pygame.__file__).parent / "examples")
# Height and Width of screen
WIDTH = 640
HEIGHT = 480
# Height and width of the sprite
SPRITE_WIDTH = 80
SPRITE_HEIGHT = 60
# our game object class
class GameObject:
def __init__(self, image, height, speed):
self.speed = speed
self.image = image
self.pos = image.get_rect().move(0, height)
# move the object.
def move(self, up=False, down=False, left=False, right=False):
if right:
self.pos.right += self.speed
if left:
self.pos.right -= self.speed
if down:
self.pos.top += self.speed
if up:
self.pos.top -= self.speed
# controls the object such that it cannot leave the screen's viewpoint
if self.pos.right > WIDTH:
self.pos.left = 0
if self.pos.top > HEIGHT - SPRITE_HEIGHT:
self.pos.top = 0
if self.pos.right < SPRITE_WIDTH:
self.pos.right = WIDTH
if self.pos.top < 0:
self.pos.top = HEIGHT - SPRITE_HEIGHT
# quick function to load an image
def load_image(name):
path = os.path.join(main_dir, "data", name)
return pygame.image.load(path).convert()
# here's the full code
async def pymain():
#def pymain():
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
player = load_image("player1.gif")
entity = load_image("alien1.gif")
background = load_image("liquid.bmp")
# scale the background image so that it fills the window and
# successfully overwrites the old sprite position.
background = pygame.transform.scale2x(background)
background = pygame.transform.scale2x(background)
screen.blit(background, (0, 0))
objects = []
p = GameObject(player, 10, 3)
for x in range(10):
o = GameObject(entity, x * 40, x)
objects.append(o)
pygame.display.set_caption("Move It!")
# This is a simple event handler that enables player input.
while True:
# Get all keys currently pressed, and move when an arrow key is held.
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
p.move(up=True)
if keys[pygame.K_DOWN]:
p.move(down=True)
if keys[pygame.K_LEFT]:
p.move(left=True)
if keys[pygame.K_RIGHT]:
p.move(right=True)
# Draw the background
screen.blit(background, (0, 0))
for e in pygame.event.get():
# quit upon screen exit
if e.type == pygame.QUIT:
return
for o in objects:
screen.blit(background, o.pos, o.pos)
for o in objects:
o.move(right=True)
screen.blit(o.image, o.pos)
screen.blit(p.image, p.pos)
pygame.display.update()
#clock.tick(30)
await asyncio.sleep(0.01)
pymain()