-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcactus.py
72 lines (55 loc) · 2.04 KB
/
cactus.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
import pygame
from pygame import *
import os, random
pygame.mixer.pre_init(44100, -16, 2, 2048) # fix audio delay
pygame.init()
scr_size = (width,height) = (600,150)
FPS = 60
gravity = 0.6
# screen = pygame.display.set_mode(scr_size)
class Cactus(pygame.sprite.Sprite):
def __init__(self,speed=5,sizex=-1,sizey=-1):
self.screen = pygame.display.set_mode(scr_size)
pygame.sprite.Sprite.__init__(self,self.containers)
self.images,self.rect = self.load_sprite_sheet('cacti-small.png',3,1,sizex,sizey,-1)
self.rect.bottom = int(0.98*height)
self.rect.left = width + self.rect.width
self.image = self.images[random.randrange(0,3)]
self.movement = [-1*speed,0]
def load_sprite_sheet(
self,
sheetname,
nx,
ny,
scalex = -1,
scaley = -1,
colorkey = None,
):
fullname = os.path.join('sprites_copy',sheetname)
sheet = pygame.image.load(fullname)
sheet = sheet.convert()
sheet_rect = sheet.get_rect()
sprites = []
sizex = sheet_rect.width/nx
sizey = sheet_rect.height/ny
for i in range(0,ny):
for j in range(0,nx):
rect = pygame.Rect((j*sizex,i*sizey,sizex,sizey))
image = pygame.Surface(rect.size)
image = image.convert()
image.blit(sheet,(0,0),rect)
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey,RLEACCEL)
if scalex != -1 or scaley != -1:
image = pygame.transform.scale(image,(scalex,scaley))
sprites.append(image)
sprite_rect = sprites[0].get_rect()
return sprites,sprite_rect
def draw(self):
self.screen.blit(self.image,self.rect)
def update(self):
self.rect = self.rect.move(self.movement)
if self.rect.right < 0:
self.kill()