Skip to content

Instantly share code, notes, and snippets.

@Snegovikufa
Created March 26, 2020 15:07
Show Gist options
  • Save Snegovikufa/a8b79c3a8165b562cd74725b31d29f3c to your computer and use it in GitHub Desktop.
Save Snegovikufa/a8b79c3a8165b562cd74725b31d29f3c to your computer and use it in GitHub Desktop.
import arcade
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_TITLE = "Platformer"
PLAYER_START_X = 64
PLAYER_START_Y = 94
class MyGame(arcade.Window):
def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
self.coin_list = None
self.wall_list = None
self.cactus_list = None
self.end_list = None
self.player_list = None
self.player_sprite = None
self.physics_engine = None
self.bg = None
self.end_flag = None
self.view_bottom = 0
self.view_left = 0
self.end_of_map = 1230
self.score = 0
self.level = 1
self.collect_coin_sound = arcade.load_sound("sounds/coin1.wav")
self.jump_sound = arcade.load_sound("sounds/jump1.wav")
self.game_over = arcade.load_sound("sounds/gameover1.wav")
def setup(self, level):
self.view_bottom = 0
self.view_left = 0
# self.score = 0
bg_image = "images/bg/bg" + str(level) + ".jpg"
self.bg = arcade.Sprite(bg_image)
self.bg.left = -350
self.bg.bottom = 0
self.player_list = arcade.SpriteList()
self.wall_list = arcade.SpriteList()
self.coin_list = arcade.SpriteList()
self.cactus_list = arcade.SpriteList()
self.end_list = arcade.SpriteList()
self.player_sprite = arcade.Sprite("images/player_1/player_stand.png", 1)
self.player_sprite.center_x = PLAYER_START_X
self.player_sprite.center_y = PLAYER_START_Y
self.player_list.append(self.player_sprite)
cactus_sprite = arcade.Sprite("images/tiles/cactus.png", 0.5)
cactus_sprite.center_x = 1200
cactus_sprite.center_y = 1500
self.cactus_list.append(cactus_sprite)
flag = arcade.Sprite("images/items/flagGreen1.png", 0.5)
flag.center_x = 1230
flag.center_y = 96
self.end_list.append(flag)
for x in range(0, 1250, 64):
wall = arcade.Sprite("images/tiles/grassMid.png", 0.5)
wall.center_x = x
wall.center_y = 32
self.wall_list.append(wall)
coordinate_list = [[512, 96],
[256, 96],
[768, 96]]
for coordinate in coordinate_list:
wall = arcade.Sprite("images/tiles/boxCrate_double.png", 0.5)
wall.position = coordinate
self.wall_list.append(wall)
for x in range(128, 1250, 256):
coin = arcade.Sprite("images/items/coinGold.png", 0.5)
coin.center_x = x
coin.center_y = 96
self.coin_list.append(coin)
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
self.wall_list,
1)
def on_draw(self):
arcade.start_render()
self.bg.draw()
self.wall_list.draw()
self.coin_list.draw()
self.cactus_list.draw()
self.end_list.draw()
self.player_list.draw()
score_text = f"Монет: {self.score} Уровень {self.level}"
arcade.draw_text(score_text, 10 + self.view_left, 600 + self.view_bottom,
arcade.csscolor.WHITE, 18)
def on_key_press(self, key, modifiers):
if key == arcade.key.UP:
if self.physics_engine.can_jump():
self.player_sprite.change_y = 15
arcade.play_sound(self.jump_sound)
elif key == arcade.key.LEFT:
self.player_sprite.change_x = -5
elif key == arcade.key.RIGHT:
self.player_sprite.change_x = 5
def on_key_release(self, key, modifiers):
if key == arcade.key.LEFT:
self.player_sprite.change_x = 0
elif key == arcade.key.RIGHT:
self.player_sprite.change_x = 0
def update(self, delta_time):
self.physics_engine.update()
coin_hit_list = arcade.check_for_collision_with_list(self.player_sprite,
self.coin_list)
for coin in coin_hit_list:
coin.remove_from_sprite_lists()
arcade.play_sound(self.collect_coin_sound)
self.score += 1
if self.player_sprite.center_y < -50:
self.player_sprite.center_x = PLAYER_START_X
self.player_sprite.center_y = PLAYER_START_Y
self.view_left = 0
self.view_bottom = 0
arcade.play_sound(self.game_over)
self.update_camera()
if arcade.check_for_collision_with_list(self.player_sprite, self.cactus_list):
self.player_sprite.center_x = PLAYER_START_X
self.player_sprite.center_y = PLAYER_START_Y
self.view_left = 0
self.view_bottom = 0
arcade.play_sound(self.game_over)
self.update_camera()
if self.player_sprite.center_x >= self.end_of_map:
self.level += 1
self.setup(self.level)
self.view_left = 0
self.view_bottom = 0
self.update_camera()
self.move_camera()
def move_camera(self):
old_view_left = self.view_left
old_view_bottom = self.view_bottom
left_boundary = self.view_left + 150
if self.player_sprite.left < left_boundary:
self.view_left -= left_boundary - self.player_sprite.left
right_boundary = self.view_left + SCREEN_WIDTH - 150
if self.player_sprite.right > right_boundary:
self.view_left += self.player_sprite.right - right_boundary
top_boundary = self.view_bottom + SCREEN_HEIGHT - 100
if self.player_sprite.top > top_boundary:
self.view_bottom += self.player_sprite.top - top_boundary
bottom_boundary = self.view_bottom + 50
if self.player_sprite.bottom < bottom_boundary:
self.view_bottom -= bottom_boundary - self.player_sprite.bottom
if old_view_left != self.view_left or old_view_bottom != self.view_bottom:
self.view_bottom = int(self.view_bottom)
self.view_left = int(self.view_left)
self.update_camera()
def update_camera(self):
arcade.set_viewport(self.view_left,
SCREEN_WIDTH + self.view_left,
self.view_bottom,
SCREEN_HEIGHT + self.view_bottom)
def main():
window = MyGame()
window.setup(window.level)
arcade.run()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment