-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code
113 lines (88 loc) · 2.59 KB
/
Code
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
import pygame
from pygame.locals import *
import os
import sys
import math
pygame.init()
#Initialize Background and sprites
width, height = 1000, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Barbie Dash")
bg2 = pygame.image.load('Background2.jpeg')
barbie = pygame.image.load('Character1.png')
barbie = pygame.transform.scale(barbie, (100, 200))
bg0 = 0.0
bg02 = bg2.get_width()
red = (255, 0, 0)
#Player Class
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = barbie
self.rect = self.image.get_rect()
self.rect.center = (width // 2, height - 50)
self.velocity = 0
self.jump_height = -20
def update(self):
self.velocity += 1
self.rect.y += self.velocity
if self.rect.bottom > height:
self.rect.bottom = height
self.velocity = 0
def jump(self):
if self.rect.bottom == height:
self.velocity = self.jump_height
#Obstacle Class
class Obstacle(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, speed):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(red)
self.rect = self.image.get_rect()
self.rect.bottomright = (x, y)
self.speed = speed
def update(self):
self.rect.x -= self.speed
if self.rect.right < 0:
self.rect.left = width
self.rect.centery = height - 50
#Change background
def redrawGameWindow():
global walkCount
screen.blit(bg2, (0,0))
#Run the Game
allSprites = pygame.sprite.Group()
player = Player()
obstacle = Obstacle(width, height, 50, 50, 5)
allSprites.add(player, obstacle)
clock = pygame.time.Clock()
speed = 100
run = True
while run:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player.jump()
redrawGameWindow()
bg0 -= 1.4
bg02 -= 1.4
if bg0 < bg2.get_width() * -1:
bg0 = bg2.get_width()
if bg02 < bg2.get_width() * -1:
bg02 = bg2.get_width()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
if event.type == USEREVENT+1:
speed += 1
allSprites.update()
allSprites.draw(screen)
clock.tick(60)
pygame.display.flip()
pygame.quit()