-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgame.py
More file actions
135 lines (103 loc) · 4.09 KB
/
game.py
File metadata and controls
135 lines (103 loc) · 4.09 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import pygame
from pygame.locals import *
from player import Player
from block import Block
import pygame
import random
import time
class game:
def __init__(self,debug):
self._running = True
self._display_surf = None
self.size = self.weight, self.height = 640, 400
self.width=1400
self.height=800
self.playerQuit=False
def ApplyGravity(self,all_sprites_list):
for sprite in all_sprites_list:
if(sprite.gravity==True):
sprite.rect.y+=1
time.sleep(0.01)
def ClimbObsticle(self,player,block_list):
blocks_hit = pygame.sprite.spritecollide(player, block_list,False)
for hit_block in blocks_hit:
if(player.rect.y+player.height < hit_block.rect.y or
player.rect.y < hit_block.rect.y+player.height
):
player.rect.y = hit_block.rect.y-player.height
def run(self):
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
pygame.init()
screen_width = 1400
screen_height = 800
screen = pygame.display.set_mode([screen_width, screen_height])
player = Player(RED, 10, 15)
player.rect.y=screen_height/4
subblocks_list = pygame.sprite.Group()
block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
lag = screen_height/4
for i in range(screen_width/player.width):
# This represents a block
block = Block(BLACK, 20, 15)
# Set a random location for the block
block.rect.x = i*block.width
block.rect.y = (lag-15) + random.randrange(30)
height=block.rect.y
while(height < screen_height ):
subblock = Block(BLACK, 20, 15)
subblock.rect.x=block.rect.x
height = height + subblock.height
subblock.rect.y= height
subblocks_list.add(subblock)
all_sprites_list.add(subblock)
lag = block.rect.y
# Add the block to the list of objects
block_list.add(block)
all_sprites_list.add(block)
all_sprites_list.add(player)
done = False
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Clear the screen
screen.fill(WHITE)
self.ApplyGravity(all_sprites_list)
self.ClimbObsticle(player,block_list)
keys=pygame.key.get_pressed()
player.flush()
if keys[K_LEFT]:
player.moveleft(screen_width)
if keys[K_RIGHT]:
player.moveright(screen_width)
# Get the current mouse position. This returns the position
# as a list of two numbers.
'''pos = pygame.mouse.get_pos()
# Fetch the x and y out of the list,
# just like we'd fetch letters out of a string.
# Set the player object to the mouse location
player.rect.x = pos[0]
player.rect.y = pos[1]
# See if the player block has collided with anything.
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
# Check the list of collisions.
for block in blocks_hit_list:
score += 1
print(score)
'''
# Draw all the spites that may need to be drawn
all_sprites_list.draw(screen)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(120)
pygame.quit()
#############################################
if __name__ == "__main__" :
theApp = game(False)
#this is the game code containing the game loop and all apspects of controls