From 36592ab040b4c261f4cebc010fba9f87f05ef9ca Mon Sep 17 00:00:00 2001 From: "Goncalo Lourenco (dacostag)" Date: Mon, 27 Apr 2020 19:38:35 +0100 Subject: [PATCH] organizing code + gravity + jumping --- .idea/vcs.xml | 1 + 8-bit wonder text files/8-BIT WONDER.TXT | 22 ++ 8-bit wonder text files/README.TXT | 53 ++++ __pycache__/settings.cpython-38.pyc | Bin 0 -> 669 bytes __pycache__/sprites.cpython-38.pyc | Bin 0 -> 1936 bytes first.py | 195 +++++++++++++++ background.jpg => img/background.jpg | Bin enemy.png => img/enemy.png | Bin icon.png => img/icon.png | Bin player.png => img/player.png | Bin spray.png => img/spray.png | Bin license.txt | 14 ++ main.py | 275 +++++++-------------- new_main.py | 0 settings.py | 24 ++ sneeze.png | Bin 815 -> 0 bytes 31 Flashback.mp3 => sound/31 Flashback.mp3 | Bin quickfart.wav => sound/quickfart.wav | Bin soundtrack.aup => sound/soundtrack.aup | 0 sprites.py | 56 +++++ 20 files changed, 449 insertions(+), 191 deletions(-) create mode 100644 8-bit wonder text files/8-BIT WONDER.TXT create mode 100644 8-bit wonder text files/README.TXT create mode 100644 __pycache__/settings.cpython-38.pyc create mode 100644 __pycache__/sprites.cpython-38.pyc create mode 100644 first.py rename background.jpg => img/background.jpg (100%) rename enemy.png => img/enemy.png (100%) rename icon.png => img/icon.png (100%) rename player.png => img/player.png (100%) rename spray.png => img/spray.png (100%) create mode 100644 license.txt create mode 100644 new_main.py create mode 100644 settings.py delete mode 100644 sneeze.png rename 31 Flashback.mp3 => sound/31 Flashback.mp3 (100%) rename quickfart.wav => sound/quickfart.wav (100%) rename soundtrack.aup => sound/soundtrack.aup (100%) create mode 100644 sprites.py diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..b3561a0 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/8-bit wonder text files/8-BIT WONDER.TXT b/8-bit wonder text files/8-BIT WONDER.TXT new file mode 100644 index 0000000..7724218 --- /dev/null +++ b/8-bit wonder text files/8-BIT WONDER.TXT @@ -0,0 +1,22 @@ +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + VIEW THIS USING A MONOSPACED FONT!!! +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + :: SPECIFIC INFO FOR THIS FONT :: + + Heh, I'm a big fan of 8-bit things, so I made an 8-bit font. The original plan + was to make it a pixel-perfect low resolution font, and that part does work as + well, but as an additional bonus it looks pretty interesting if used at bigger + sizes in italic mode. + + This font was made for making titles for my website and my pictures, so it has + certain specific optimizations. When used on-screen it will look the best with + font sizes starting with 9 and then any next 9. That is 9, 18, 27, 36, etc. + + Enjoy, and don't forget to send feedback and tell me what characters you would + like to see added. + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + Revision: 0.80 | 26.01.2001 + [EOF] \ No newline at end of file diff --git a/8-bit wonder text files/README.TXT b/8-bit wonder text files/README.TXT new file mode 100644 index 0000000..b70f725 --- /dev/null +++ b/8-bit wonder text files/README.TXT @@ -0,0 +1,53 @@ +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + VIEW THIS USING A MONOSPACED FONT!!! +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + :: INTRODUCTION :: + + Greetings, welcome to the readme file for my fonts. That's it. :) + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + :: TERMS OF USE :: + + Currently all my fonts are freeware. That doesn't mean however, that you can + do whatever you want with them. Here are the rules for use and distribution: + + 1. You WILL NOT modify the fonts, the copyright notices, or this text file. + 2. You WILL NOT sell the fonts in ANY way. + 3. Whatever you do with it, you take full responsibility. + + Those are the rules you MUST follow to use or distribute these fonts. If you + disagree, stop using the fonts immediately. If I grow aware of any violations + of these terms, expect legal action to be taken. So, you have been warned. + + And, if you decide to use any of the fonts for commercial purposes, it would + be really nice to receive a product sample. I can make a special version of a + font exclusively for you as well. Just e-mail me if you are interested. +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + :: ABOUT ME :: + + Well, not much to say, really. I'm a guy from Latvia, called Joiro Hatagaya, + I like making fonts, 2D/3D graphics, music, games and programs and many other + things. Send me feedback to make me happy and give me energy to do more. :) + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + :: CONTACT INFORMATION AND WEBSITES :: + + E-MAIL: JOIRO@HOTMAIL.COM | Please put "FONTS" in caps as the subject. Do not + send spam, or you WILL regret it. + + WEBSITE: JOIRO.THE3DSTUDIO.COM | Come here to see some of my 3D work and find + out what other things I do. + + NEW!!! + FONT WEBSITE: http://www.typesource.com/Presents/Hatagaya/Fonts.html | Yes! I + now have a special mini-website for my fonts only, where you can find all the + latest versions of these fonts and also brand new fonts. So go visit it! NOW! + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + Revision: 1.20 | 05.01.2001 + [EOF] \ No newline at end of file diff --git a/__pycache__/settings.cpython-38.pyc b/__pycache__/settings.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1b6784504554dc5c1802418cc8cfbe63b65adfb1 GIT binary patch literal 669 zcmYk2&2AGh5XZe=`PgidHqD_3R4o@oB`61OsG^nN3Q~o7z*=>+&3GNBoAt7GsM0$x zz!S75)Hmn}UI1ReS56V!xb=V-r=c*G|Nj3xwl$u^ZZ{+tM-M(7y>bcpZsGrEAnfB* zA5nmSQxJNyI@AG|y5La{eCk6$14br|^CoLuIJCX=A!|e9!lj*CY9d9(HSCAk2eklQ0*e+(+%+CD4ODtmQteV{WiXV8ask=mt1%$o6o7b&=O1_6QM| z4k3#zDLsph7& hJC!gi*mLX=Q87x(X}P|G!}ZEVcO*tfp-`wv9l$7=up literal 0 HcmV?d00001 diff --git a/__pycache__/sprites.cpython-38.pyc b/__pycache__/sprites.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..da78e96799cb609e151678dc94981446b7104db7 GIT binary patch literal 1936 zcmZ`(TaOzx6t?H;+;+1l&>Khycwj2JKwm(pqARtuKv4wjmWL?98pcjC*~tal>5^5D zklLTX56C_Ne}Et3CnTOC@yr84TF&t#+d!#DK0fF3WzPL!qfsR={(k+Zum6k)`5lGr z5rA+2Q{M$5h@b^I!qqEihe|s1hJGiwPlPXmCqx8*gv02u--!VWMFd!MfidA7k|bWi zOC+I|UTGB8KOfjXp2Zxl|e6-3;VW z^*b>20*E0)P|%e@l8QM$FQAvs}^XndJ{FasM6WZoYb z8)b5Aq_X}anOT22(a06nPqQrXY@lV)Q*}`07ox7h^4IMW~Wh9`OjL2C{D%ED(pRgW&n!LohdI-gjpfR2>Ki`!MFr z1tVy$@F(~3KhDq3lZx5{!P4&(gH@rfwow|8O^PBH(mBLdrbTLc6E)K6lDjZZJaq-R zV2~T_+*Oim4jj%#Q;fKF<>fWO+$Ig^X>DyV?myqyU*V>KVl&iblxO^5unf-#m`8z+ zt`K$I#5FYBBB9*`mVuToTQjVm%|QO5DSisVzndbo!(ibrf*v$MXlWq?HC#s6 zAfaI*AG?+a=>`(xl?A2@<5d@Subud-AK*>(?uI0_r!}Rp3`#dNklR7Bi=>GJYtThl zy$W0sy2^SASbAdV;J+Tj-f|PB#*T%(rRh2gqo;ct^Dp{JI|= width - 32: + playerX = width - 32 + if playerY <= 0: + playerY = 0 + elif playerY >= height - 32: + playerY = height - 32 + + # Enemy movement patterns + for i in range(num_of_enemies): + + # Game over + player_collision = isCollision(playerX, playerY, enemyX[i], enemyY[i]) + if player_collision: + score_value = 0 + game_over = True + + enemyX[i] += enemyX_change[i] + # set boundaries to define enemy movement + if enemyX[i] <= 0: # later change this value to any left boundary, like a wall on it's left + enemyX_change[i] = 0.7 + elif enemyX[i] >= width - 32: # later change this value to any right boundary, like a wall on it's right + enemyX_change[i] = -0.7 + + # Collision between attack and enemy + attack_collision = isCollision(enemyX[i], enemyY[i], sprayX, sprayY) + if attack_collision: + sprayY = height + spray_state = "ready" + score_value += 1 + enemyX[i] = random.randint(120, width - 32) + enemyY[i] = 360 + enemydeath_sound = mixer.Sound('quickfart.wav') # later change .wav to the player being attacked sound + enemydeath_sound.play() + + enemy(enemyX[i], enemyY[i], i) + + # Spray attack movement + if sprayX >= width: + sprayX = 0 + spray_state = "ready" + + if spray_state is "fire": + sprayX += sprayX_change + player_attack(sprayX, sprayY) + + # When player loses the game: + if game_over: + game_over_text() + + player(playerX, playerY) + show_score(textX, textY) + # note: things appear on top of the things above it in the code + pygame.display.update() diff --git a/background.jpg b/img/background.jpg similarity index 100% rename from background.jpg rename to img/background.jpg diff --git a/enemy.png b/img/enemy.png similarity index 100% rename from enemy.png rename to img/enemy.png diff --git a/icon.png b/img/icon.png similarity index 100% rename from icon.png rename to img/icon.png diff --git a/player.png b/img/player.png similarity index 100% rename from player.png rename to img/player.png diff --git a/spray.png b/img/spray.png similarity index 100% rename from spray.png rename to img/spray.png diff --git a/license.txt b/license.txt new file mode 100644 index 0000000..9dd33a2 --- /dev/null +++ b/license.txt @@ -0,0 +1,14 @@ + +############################################################################### + + Platformer graphics (Deluxe) by Kenney Vleugels (www.kenney.nl) + + ------------------------------ + + License (CC0) + http://creativecommons.org/publicdomain/zero/1.0/ + + You may use these graphics in personal and commercial projects. + Credit (Kenney or www.kenney.nl) would be nice but is not mandatory. + +############################################################################### \ No newline at end of file diff --git a/main.py b/main.py index 6de9ce0..3baada1 100644 --- a/main.py +++ b/main.py @@ -2,194 +2,87 @@ import pygame import random import math from pygame import mixer - -# initialize pygame -pygame.init() - -# screen resolutions -width = 800 -height = 425 -screen = pygame.display.set_mode((width, height)) # create screen - -# Background -background = pygame.image.load('background.jpg') - -# Soundtrack -mixer.music.load('31 Flashback.mp3') -mixer.music.play(-1) - -# Title and Icon -pygame.display.set_caption("You should be in quarantine!") -icon = pygame.image.load('icon.png') -pygame.display.set_icon(icon) - -# Player -playerImg = pygame.image.load('player.png') -playerX = 100 -playerY = 360 -playerX_change, playerY_change = 0, 0 -score = 0 - -# Enemy -enemyImg = [] -enemyX = [] -enemyY = [] -enemyX_change = [] -enemyY_change = [] -num_of_enemies = 6 - -for i in range(num_of_enemies): - enemyImg.append(pygame.image.load('enemy.png')) - enemyX.append(random.randint(150, width - 32)) - enemyY.append(360) - enemyX_change.append(-0.7) - enemyY_change.append(0) - -# Player spray Attack: Ready - spray attack isn't being seen / Fire - spray is moving -sprayImg = pygame.image.load('spray.png') -sprayX = 0 -sprayY = 360 -sprayX_change = 2 -spray_state = "ready" - -# Score text - -score_value = 0 -font = pygame.font.Font('8-BIT WONDER.ttf', 18) -textX, textY = 10, 10 - -# Game Over text -over_font = pygame.font.Font('8-BIT WONDER.ttf', 36) -game_over = False #game_over remains False until player loses the game - -def show_score(x, y): - score = font.render("Score " + str(score_value), True, (245, 194, 66)) - screen.blit(score, (x, y)) - - -def game_over_text(): - over_text = over_font.render("GAME OVER", True, (0, 0, 0)) - screen.blit(over_text, (int(width/2) - 160, int(height / 2)-20)) - - -def player(x, y): - screen.blit(playerImg, (x, y)) - - -def enemy(x, y, i): - screen.blit(enemyImg[i], (x, y)) - - -def player_attack(x, y): - global spray_state - spray_state = "fire" - screen.blit(sprayImg, (x + 10, y + 10)) - - -def isCollision(x1, y1, x2, y2): - distance = math.sqrt((math.pow(x1 - x2, 2)) + (math.pow(y1 - y2, 2))) - if distance < 27: - return True - #if x1 == playerX - - return False - - -# Game loop -running = True -while running: # leaves window opened - screen.fill((250, 152, 3)) # note: things appear on top of the things above it in the code - # background image - screen.blit(background, (0, 0)) - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - # when key is being pressed, below are all the game keys - if event.type == pygame.KEYDOWN and game_over is False: - if event.key == pygame.K_LEFT: - playerX_change = -1 - if event.key == pygame.K_RIGHT: - playerX_change = 1 - if event.key == pygame.K_UP: - playerY_change = -1 - if event.key == pygame.K_DOWN: - playerY_change = 1 - if event.key == pygame.K_z: - if spray_state is "ready": - # spray_sound = mixer.Sound('quickfart.wav') #later change .wav to the spray sound - # spray_sound.play() - # get's current x, y coordinate of the player - sprayX = playerX - sprayY = playerY - player_attack(sprayX, sprayY) - - if event.type == pygame.KEYUP: - if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: - playerX_change = 0 - if event.key == pygame.K_UP or event.key == pygame.K_DOWN: - playerY_change = 0 - - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_SPACE: - game_over = False - - playerX += playerX_change - playerY += playerY_change - - # set boundaries so player doesn't leave the screen - if playerX <= 0: - playerX = 0 - elif playerX >= width - 32: - playerX = width - 32 - if playerY <= 0: - playerY = 0 - elif playerY >= height - 32: - playerY = height - 32 - - # Enemy movement patterns - for i in range(num_of_enemies): - - # Game over - player_collision = isCollision(playerX, playerY, enemyX[i], enemyY[i]) - if player_collision: - game_over = True - - enemyX[i] += enemyX_change[i] - # set boundaries to define enemy movement - if enemyX[i] <= 0: # later change this value to any left boundary, like a wall on it's left - enemyX_change[i] = 0.7 - elif enemyX[i] >= width - 32: # later change this value to any right boundary, like a wall on it's right - enemyX_change[i] = -0.7 - - # Collision between attack and enemy - attack_collision = isCollision(enemyX[i], enemyY[i], sprayX, sprayY) - if attack_collision: - sprayY = height - spray_state = "ready" - score_value += 1 - enemyX[i] = random.randint(120, width - 32) - enemyY[i] = 360 - enemydeath_sound = mixer.Sound('quickfart.wav') # later change .wav to the player being attacked sound - enemydeath_sound.play() - - enemy(enemyX[i], enemyY[i], i) - - # Spray attack movement - if sprayX >= width: - sprayX = 0 - spray_state = "ready" - - if spray_state is "fire": - sprayX += sprayX_change - player_attack(sprayX, sprayY) - - # When player loses the game: - if game_over: - game_over_text() - - player(playerX, playerY) - show_score(textX, textY) - # note: things appear on top of the things above it in the code - pygame.display.update() +from settings import * +from sprites import * + + +class Game: + def __init__(self): + # initialize game window, etc + pygame.init() + pygame.mixer.init() + self.screen = pygame.display.set_mode(((width, height))) + + # Title and Icon + pygame.display.set_caption(title) + icon = pygame.image.load('img/icon.png') + pygame.display.set_icon(icon) + + self.clock = pygame.time.Clock() + self.running = True + + def new(self): + # starts a new game not a new program + self.all_sprites = pygame.sprite.Group() + self.platforms = pygame.sprite.Group() + self.player = Player(self) + self.all_sprites.add(self.player) + for plat in platform_list: + p = Platform(*plat) + self.all_sprites.add(p) + self.platforms.add(p) + self.run() + + def run(self): + # Game loop + self.playing = True + while self.playing: + self.clock.tick(fps) + self.events() + self.update() + self.draw() + + def update(self): + # Game loop - Update + self.all_sprites.update() + # check if player hits platform - only if falling + if self.player.vel.y > 0: + hits = pygame.sprite.spritecollide(self.player, self.platforms, False) + if hits: + self.player.pos.y = hits[0].rect.top + self.player.vel.y = 0 + + def events(self): + # Game loop - events + for event in pygame.event.get(): + if event.type == pygame.QUIT: + if self.playing: + self.playing = False + self.running = False + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + self.player.jump() + + def draw(self): + # Game loop - draw/render + self.screen.blit(background, (0, 0)) + self.all_sprites.draw(self.screen) + pygame.display.flip() + + def show_start_screen(self): + # game start screen + pass + + def show_go_screen(self): + # game over screen + pass + + +g = Game() +g.show_start_screen() + +while g.running: + g.new() + g.show_go_screen() # go stands for Game Over + +pygame.quit() diff --git a/new_main.py b/new_main.py new file mode 100644 index 0000000..e69de29 diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..34ebd5d --- /dev/null +++ b/settings.py @@ -0,0 +1,24 @@ +import os +import pygame + +width = 800 +height = 425 +fps = 30 + +title = "You should be in quarantine!" +# setup game assets (images/sounds) +game_folder = os.path.dirname(__file__) +img_folder = os.path.join(game_folder, "img") +sound_folder = os.path.join(game_folder, "sound") +background = pygame.image.load('img/background.jpg') +# Player properties +player_acc = 0.9 +player_friction = -0.12 +player_gravity = 1.2 + +# Starting platforms + +platform_list = [(0, height - 40, width, 40), + (width / 2, 250, 60, 20), + (90, height - 300, 115, 20)] + diff --git a/sneeze.png b/sneeze.png deleted file mode 100644 index 7aa3856562ea07dca6ff5e8a812545309812110f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 815 zcmV+~1JL}5P)%^)4*_^U3-*MTZP0;XdoD2lhOeCJ``p<1>E4eB!UEN`cNGU%V0 zFrYV9AIYS69q<*n2Mhu?V;fMnmAbMqCa1wa2S9~0%Vxl3;DSl5zpw(MKx4RF2n>f! zMbliOXUzSoZ#9-1&l#h~n+=amn*+Rv(MyUl;B!>03%C{|S8~Y8GnZ-&)Z}?)bVZrZ zP}SSX3P<9MYQF?p;^cM#o#E#@5*4-pH-LPTMizi8Kn3stSc;J&10j?3P=N2itjYYP z@bzYu9Mj?(fS3bjI0&2vE&?{d$LfpNB+v~!O;p(IO1K=pzXKKl4fqCpiE{-IlRE^w zHmQ*^Wk6;`doM7SE^&Z6z@>hCTy0#mYI*Az3UiGYg?*ItTh);hd!LvT}$M(U5Di zf2_>P@mJ^O&uEf72K1$3;0W-3jeQ)5P3kOSl#)h6eDhnW6iJpdfFF25if@rDPQVlA znHx9(WT(&0ABuEE4V1fbf(}`pz~oWf?4i^Q^)*`xt)FU}#@yisS$Q002ovPDHLkV1f( width: + self.pos.x = 0 + elif self.pos.x < 0: + self.pos.x = width + + self.rect.midbottom = self.pos + +class Platform(pygame.sprite.Sprite): + def __init__(self, x, y, w, h): + pygame.sprite.Sprite.__init__(self) + self.image = pygame.Surface((w, h)) + self.image.fill((128, 128, 128)) + self.rect = self.image.get_rect() + self.rect.x = x + self.rect.y = y \ No newline at end of file