Remaking a classic arcade game using python

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 2916 Views - Last Post: 01 October 2019 - 09:42 PM Rate Topic: -----

#31 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7501
  • View blog
  • Posts: 15,544
  • Joined: 16-October 07

Re: Remaking a classic arcade game using python

Posted 20 August 2019 - 05:05 AM

Sigh. Enthusiasm still seems to be beating education. We're trying to show you a tried and true direction to go and, I think, you're trying to show us "look, I made a thing." I mean, knock yourself out, make all the things that amuse you, but if you're going to simply ignore all the suggestions to make a better thing, I really can't see the point of this exercise.

I'm just going to leave this here. That last block of code was sincerely disappointing. I think, perhaps, pygame is saving you from seeing just how much extra work you're doing with it's intrinsic double buffering. In an attempt to highlight that, I put in updates to basically counter act the double buffer.

I'll leave you with this. Btw, I did like the car drawing, at least, good job on that. Again, again, again, again, if you really want to make a game, only draw the bloody thing once.

import pygame, random, math # no ,sys
from time import sleep
from math import pi

# Define some colors
# wtf do you define size and colors in muliples places?
# size = (830, 700)
# no, not here, ever
# screen = pygame.display.set_mode(size)

black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
darkgreen = (0, 102, 0)
pink = (255, 0, 255)
deepskyblue = (0, 191, 255)
dirt = (204, 102, 0)
carrot = (237, 145, 33)
grey = (224, 224, 224)
blue = (0, 0, 255)
darkblue = (0, 0, 153)
yellow = (255, 255, 0)


class Car(object):
    def __init__(self, y):
        self.y = y

    def move(self):
        self.y += 1

    def draw(self, screen):
        pass


class PlayerCar(Car):
    def __init__(self, y):
        Car.__init__(self, y)

    def reset(self):
        self.y = 0

    # you didn't pass screen?!?
    def draw(self, screen):
        # get an idea of the actual size of this
        # pygame.draw.rect(screen, yellow, ((380, 440 + self.y), (40, 60)), 2)
        pygame.draw.line(screen,white,[386,453 + self.y],[415,453 + self.y],10)#front wheels
        pygame.display.update()
        pygame.draw.line(screen,white,[386,483 + self.y],[415,483 +self.y],10)#back wheels
        pygame.display.update()
        pygame.draw.line(screen,white,[400,440 + self.y],[400,500 + self.y],20)#centre of car538 
        pygame.display.update()
        pygame.draw.line(screen,white,[390,442 + self.y],[390,498 + self.y],2)
        pygame.display.update()
        pygame.draw.line(screen,white,[389,444 + self.y],[389,496 + self.y],2)
        pygame.display.update()
        pygame.draw.line(screen,white,[388,446 + self.y],[388,494 + self.y],2)
        pygame.display.update()
        pygame.draw.line(screen,white,[387,448 + self.y],[387,492 + self.y],2)
        pygame.display.update()
        pygame.draw.line(screen,grey,[393,453 + self.y],[410,453 + self.y],18)#front hood
        pygame.display.update()
        pygame.draw.line(screen,black,[392,443 + self.y],[395,443 + self.y],2)#left head light
        pygame.display.update()
        pygame.draw.line(screen,black,[405,443 + self.y],[408,443 + self.y],2)#right head light    
        pygame.display.update()
        pygame.draw.line(screen,black,[397,447 + self.y],[397,449 + self.y],2)#front air intake
        pygame.display.update()
        pygame.draw.line(screen,black,[397,449 + self.y],[403,449 + self.y],2)#horizontal line
        pygame.display.update()
        pygame.draw.line(screen,black,[404,447 + self.y],[404,450 + self.y],2)
        pygame.display.update()
        pygame.draw.line(screen,white,[410,442 + self.y],[410,498 + self.y],2)
        pygame.display.update()
        pygame.draw.line(screen,white,[411,444 + self.y],[411,496 + self.y],2)
        pygame.display.update()
        pygame.draw.line(screen,white,[412,446 + self.y],[412,494 + self.y],2)
        pygame.display.update()
        pygame.draw.line(screen,white,[413,448 + self.y],[413,492 + self.y],2)
        pygame.display.update()
        pygame.draw.ellipse(screen,grey,[391,475 + self.y,25,27],11)#grey shade on back of car
        pygame.display.update()
        pygame.draw.line(screen,deepskyblue,[389,449 + self.y],[389,490 + self.y],3)
        pygame.display.update()
        pygame.draw.line(screen,deepskyblue,[413,449 + self.y],[413,490 + self.y],3)
        pygame.display.update()
        pygame.draw.line(screen,pink,[391,448 + self.y],[391,494 + self.y],3)
        pygame.display.update()
        pygame.draw.line(screen,pink,[411,448 + self.y],[411,494 + self.y],3)
        pygame.display.update()
        pygame.draw.line(screen,pink,[391,494 + self.y],[394,500 + self.y],3)#back left pink line
        pygame.display.update()
        pygame.draw.line(screen,pink,[411,494 + self.y],[408,500 + self.y],3)#back right pink line
        pygame.display.update()
        pygame.draw.arc(screen,black,[392,457 + self.y,20,10],0,pi,5)#Front window
        pygame.display.update()
        pygame.draw.line(screen,black,[401,462 + self.y],[401,475 + self.y],22)#side windows
        pygame.display.update()
        pygame.draw.ellipse(screen,black,[390, 467 + self.y, 25, 27],11)
        pygame.display.update()
        pygame.draw.line(screen,white,[389,460 + self.y],[393,465 + self.y],2)#left front white line
        pygame.display.update()
        pygame.draw.line(screen,white,[407,465 + self.y],[413,460 + self.y],2)#right front white line
        pygame.display.update()
        pygame.draw.line(screen,white,[389,480 + self.y],[393,476 + self.y],2)#left centre white line
        pygame.display.update()
        pygame.draw.line(screen,white,[407,476 + self.y],[413,480 + self.y],2)#right centre white line
        pygame.display.update()
        pygame.draw.line(screen,black,[393,486 + self.y],[408,486 + self.y],7)#coves open area of ellipse
        pygame.display.update()
        pygame.draw.line(screen,white,[391,488 + self.y],[396,480 + self.y],2)#left rear line
        pygame.display.update()
        pygame.draw.line(screen,white,[406,479 + self.y],[408,488 + self.y],2)#right rear line
        pygame.display.update()
        pygame.draw.line(screen,white,[401,464 + self.y],[401,481 + self.y],13)#top of car
        pygame.display.update()

class RealGameObject(object):
    def __init__(self, image):
        self.image = image
        self.rect = self.image.get_rect()
        self.delta = None
        self.home = None

    def reset(self):
        if self.home:
            self.rect.topleft = self.home

    def move(self):
        if self.delta:
            self.rect.move_ip(self.delta)

    def draw(self, screen):
        screen.blit(self.image, self.rect)
        pygame.display.update()


def init_car2():
    thing_that_draws_crap = PlayerCar(0)
    canvas = pygame.Surface((1000, 1000))
    thing_that_draws_crap.draw(canvas)
    obj = RealGameObject(canvas.subsurface((380, 440), (40, 60)))
    obj.delta = (0, 1)
    obj.home = (300, 400)
    obj.reset()
    return obj

def main():
    pygame.init()
    screen = pygame.display.set_mode((830, 700))
    car = PlayerCar(0) # y = 0
    car2 = init_car2()
    clock = pygame.time.Clock()
    
    
    done = False
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
                break
        if done:
            break  # this leaves the while loop
        screen.fill(black)
        car2.draw(screen)
        car.draw(screen)
        pygame.display.flip()
        # add a little movement
        car2.move()
        car.move()
        if car.y > 100:
            car.reset()
            car2.reset()
        clock.tick(60)

    pygame.quit()

main()


Was This Post Helpful? 0
  • +
  • -

#32 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 29 August 2019 - 11:04 PM

I followed your advice and I am working on the code instead of the drawings. I've made significant progress breaking the game into separate files and then importing them into the game file. This is what I did...

I made a file that has the colours (From a post you guys suggested earlier) and then I import it to each file for the game. Reducing redundancy, so when I need to add a new colour, I just do it once instead of many times on different files.

I made a separate file using the class for each car (from a previous post you suggested). Then on the game file I have it imported.

I have three questions for you guys.

1. The post you gave me with the nested functions, I was thinking of making a separate file to draw the back ground once and have a for loop count down the time for each map. So say for example each map will be on the screen for 15seconds. Then it will go to the next map of the game. I would then have the game file import it. Am I wrong in that logic?

2. I have the file with the car class and a class for each car in the game. They inherit the y movement from the car class. My question is wouldn't it be better to have a separate file with movement (button pressed, y and x movement?.

3. This last one maybe an obvious one, but I honestly can't decide which would be better or more cleaner. Would it be better to have the cars weapons for each vehicle in the same file where I draw the cars for the game or make a separate file with a class for each car that has weapons and then import it to the file with the car class in it?

This post has been edited by albert003: 29 August 2019 - 11:06 PM

Was This Post Helpful? 0
  • +
  • -

#33 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,794
  • Joined: 27-December 13

Re: Remaking a classic arcade game using python

Posted 03 September 2019 - 08:47 AM

I've been on a - real - vacation (no computer) for a period; thus this late answer.

ad. 1: I think you need the background to scroll; I did show how this can be made back in post #20. You describe a static background (for a period) changing to a new background - that won't work. You need the car steady more or less in the middle of the display and the background scrolling behind.

ad. 2: I'm not sure I understand the question, please show some code. Why should the y position be inherited from the Car base class? Sounds more like a property individual to each instance.

ad. 3: All that drawing (car art) should not be part of the running code. Make a completely separate service code (not meant for importing), do the drawing here and save the finished images (use pygame.image.save). Unless you want to change the artwork, this code only needs to run one single time ever!!!. The images can then be loaded (use pygame.image.load) to the running code where you also handle weapons or other car methods.
Was This Post Helpful? 0
  • +
  • -

#34 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 06 September 2019 - 01:56 PM

Lets start trying to solve the problems one at a time instead of many questions at the same time.

This is a video on youtube of the game I am trying to make being played. You can see the back ground scrolls and there are many maps. My question is, I thought it would be a good idea to make a separate file which would have a class and would have all of the maps of the game. It would also import the file that has the button game movement and would scroll when the player moves the button to move up. I know Baavgai used nested functions, but my thinking is that since there are many maps. It would be cleaner to use a class and each method would draw each map on the screen.

This is what I have right now.
import pygame,random
from exspyhuntercolours import Colour
from time import sleep

size = (830, 700)

from exspyhuntermove import Move

class Map(object):
    def __init__(self):
        super(self,Map).__init__()
        pass
        #this imports a file which controls the movement
        #when the player moves the car in the game

    #each method would draw a map for part of the game
    #as far as I can tell there are 6 maps in the game
    #so I would have 6 methods
    
    def maps1(self):
        pass

    def maps2(self):
        pass


I can't remember which chapter but when I was learning python from Learn python the hard way, there was a chapter talking about importing file. I thought it was considered good programming to have the main game loop and then import the other files. Am I wrong?

This is a link to the game.
https://www.youtube....DXNvElulik&t=6s
Was This Post Helpful? 0
  • +
  • -

#35 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,794
  • Joined: 27-December 13

Re: Remaking a classic arcade game using python

Posted 08 September 2019 - 07:48 AM

I'm now on post #35 in this thread.

All we have seen from you is some images. Images of cars and images of (one) background. If I remember right, no game code yet.
The images are nice, but they really should be loaded to the game.
Just like you loaded the food pictures back in the Jerusalem menu.
Making the static images should not be part of the game code.
Images is a ressource for the game.

You need to start coding the game!
How do the parts interact?
How does the user-interphase works?
At this point, it's not important how things are structured in modules - just get something working.

Your code in post #34 is peculiar.
The class "Map" (I prefer "Background") is, - well, a class.
The individual backgrounds should all be instances of the class - not methods as you indicate.
Images for the individual backgrounds should be loaded in the __init__() function.
The class then need some methods: 'scroll', 'draw', 'mask', etc.
The 'mask' method is to distinguish between road and dirt.
Was This Post Helpful? 0
  • +
  • -

#36 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 14 September 2019 - 09:49 PM

Dk3250
I am working on the code and not drawing. I'm not ignoring you or blowing you off, I am trying to figure out how to do what you and Baavgai suggested to me. I am working on the logic on paper drawing everything in a flow chart. I don't want to post anything here until I make some progress in the code to show you guys.

I am trying to work on a small part of the program at a time so I am not overwhelmed.
Was This Post Helpful? 0
  • +
  • -

#37 WayneCool   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-September 18

Re: Remaking a classic arcade game using python

Posted 01 October 2019 - 09:42 PM

I'm really loving this project and I hope it sees completion because I love this game and would love to see it fully functional with python code. :D
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3