Remaking a classic arcade game using python

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

30 Replies - 1759 Views - Last Post: Today, 05:05 AM Rate Topic: -----

#16 albert003   User is online

  • D.I.C Addict

Reputation: 35
  • View blog
  • Posts: 734
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 02 August 2019 - 03:32 PM

I actually had an idea how to do the various back grounds using something you helped me with in a previous program. I was going to use a class a class for each map and then use a function with a for loop in it with a timer to say how long it will be rendered on the screen. Once the for loop runs its course, the game will present the next map.

This is my idea, I still need to work out the details.

def map1():
for i in range(120):#it will show this map for 120 seconds
while True:
grass()
trees()
pygame.display.flip()




I figured out the timer after your hint and I plan to temporarily draw a block to measure the speed and how the background will move when the game is running.

I'm also in the process of drawing the trees and will use a loop to randomly show them on either side as the game screen moves.

I'll work on it this weekend and show my progress Monday.
Was This Post Helpful? 0
  • +
  • -

#17 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7464
  • View blog
  • Posts: 15,471
  • Joined: 16-October 07

Re: Remaking a classic arcade game using python

Posted 03 August 2019 - 01:50 AM

I went back and looked at your game. Heh, I recall playing this one in arcades ( do those exist anymore?)

It strikes me that even the "random" road isn't random. I'm not ocd enough to spot all the tiles, but guess it's no more than a dozen. On a curvy road you can kind of see they're about a car and a half long:

Attached Image

Indeed, disregarding IP legalities, you could reasonably build up the needed image library from a few screen shots, like above.

The point is, you should't be drawing anything. Like the original game, or really any 2D game, you should be blitting everything. No pygame.draw. Period.
Was This Post Helpful? 0
  • +
  • -

#18 albert003   User is online

  • D.I.C Addict

Reputation: 35
  • View blog
  • Posts: 734
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 03 August 2019 - 11:43 PM

I see a few arcades here and there, but they're rather small. There's actually three restaurants in town that have arcades.

I stand corrected, the game doesn't use random maps what happens is that the maps repeat themselves in the game.

I thought when the game was originally created the programmers wrote it in c or c++ and drew everything. I wanted to emulate them and try to make it as close as I could to the original arcade game. I have the game on my NES and although the graphics are pitiful the programmers coded everything. I was actually inspired by a video a guy made of the game in c++ and thought I'm at least have intermediate skills in python, I could do it as well. After reading your suggestion, I believe he also just used images for the cars and back ground. This is the link to what he did in c++.

https://www.youtube....h?v=kTlkA1V__MI

The reason for me trying to draw everything is that I am trying to build my portfolio and I thought remaking the game and drawing it would be a great addition to it, even if it looked as bad as the spyhunter game in the NES. My thinking is that it would show that I am detail oriented and that I am enthusiastic about coding.

I'll do as you suggest, but I have a question for you. As an expert in python and c++ do you think the original game was written in c++ or c?

This post has been edited by albert003: 03 August 2019 - 11:43 PM

Was This Post Helpful? 0
  • +
  • -

#19 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7464
  • View blog
  • Posts: 15,471
  • Joined: 16-October 07

Re: Remaking a classic arcade game using python

Posted 04 August 2019 - 03:58 AM

If the goal is to draw stuff with code, do that; it's fun. If the goal is to emulate ancient game design, know that almost all visual assets will be images. At least, when images are possible: earliest games are really just working with two "colors," on and off. Though even into color games, mono vector games still find their place...

View Postalbert003, on 04 August 2019 - 01:43 AM, said:

do you think the original game was written in c++ or c?

Neither. Or both. ;)

It depends where the game is deployed:

Quote

Spy Hunter is an overhead view, vehicular combat game developed by Bally Midway and released in arcades in 1983.
...
Spy Hunter was ported to the Atari 2600, Atari 8-bit family, Amstrad CPC, ZX Spectrum, Commodore 64, Apple II, ColecoVision, MS-DOS, Nintendo Entertainment System, and BBC Micro.
-- https://en.wikipedia...wiki/Spy_Hunter

An arcade game is a closed system. The programmer is writing to the hardware, not allowing a language to abstract them away from it. So, an arcade game or console is going to be running machine code. Which means assembly for the programmer or, if they were really lucky, C. It's not beyond the realm of possibility that different development houses had other tools that got them to that machine code, but they ain't saying.

For those old 8-bit boxen I can almost guarantee it was some unholy fusion of machine code and, wait for it, BASIC. BASIC was the glue of machine code in that era, forests of DATA lines with liberal PEEKs and POKEs. On the Apple II you could often break out of a running program and find yourself some gwbasic source, for all the good it did you.

Keeping in mind that C++ was a new fangled fad to game developers early on. Game developers who are used to global state and writing directly to memory addresses. OOP is the antitheses of such a mindset.

Even today, when C++ dominates game dev and OOP is pushed as the one true paradigm, looking inside modern game code will often reveal a "C with classes" bias.

Since you like the YouTube stuff, I found this recent bit on The Fascinating Story Behind the First Commercially Successful Video Game rather, well, fascinating.
Was This Post Helpful? 0
  • +
  • -

#20 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 550
  • View blog
  • Posts: 1,721
  • Joined: 27-December 13

Re: Remaking a classic arcade game using python

Posted 04 August 2019 - 08:26 AM

View Postalbert003, on 04 August 2019 - 07:43 AM, said:

The reason for me trying to draw everything is that I am trying to build my portfolio and I thought remaking the game and drawing it would be a great addition to it...

Fine, go ahead and make the drawings in code. But..!, - do it in aseparate module and only import the finished pygame surfaces to the main program. This will keep your main clean and well structured.

No matter how those drawings are made, you still need to scroll them. Allow me to push you a bit; I'll include a very simple scroll mechanism as demo of the concept. The code generates two surfaces (could be your background drawings) and scrolls them infinitely controlled by a 'scroll' parameter.

The idea builds on the fact that you can blit to the display surface outside the visible area. So, for example, a blit to (0, -400) will show the lower part of the surface to the upper half of the display.

Sample code:
import pygame
pygame.init()

X = 800
Y = 800

screen = pygame.display.set_mode((X, Y))

GREY_1 = (100, 100, 100)
GREY_2 = (200, 200, 200)


class BG():
    def __init__(self, color, scroll):
        self.surf = pygame.Surface((X, Y))
        self.surf.fill(color)
        self.scroll = scroll

    def move(self):
        self.scroll += 1
        if self.scroll == 800:
            self.scroll = -800

    def draw(self):
        screen.blit(self.surf, (0, self.scroll))
        

def main():
    all_bg =[BG(GREY_1, 0), BG(GREY_2, -800)]

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
        for bg in all_bg:
            bg.draw()
            bg.move()

        pygame.display.flip()
    
    
main()


This post has been edited by DK3250: 04 August 2019 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#21 albert003   User is online

  • D.I.C Addict

Reputation: 35
  • View blog
  • Posts: 734
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 05 August 2019 - 10:51 AM

Bavaai
Great video!
I honestly would've thought everything would've been written in c++. Thank you for the information.

DK3250
As the saying goes, great minds think alike. Back when you guys suggested I draw everything with sprites, I had a prototype I was making at the time to move a red square sprite across the screen. I was going to leave it that way (As a red block) till I could figure out how to put all of the sprites together to draw an image meanwhile working out how to move the back ground when the red block moved. In the game the player can move up, down and side to side. This was my original plan, but I have changed it since that time.

import pygame

white    = ( 255, 255, 255)
red      = ( 255,   0,   0)

#Classes
class Player(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        pygame.sprite.Sprite.__init__(self) 
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()

    def move(self, x_change, y_change):
        self.rect.x += x_change
        self.rect.y +=  y_change


spriteslist = pygame.sprite.Group()

#Create player
player = Player(red,16,16) 
spriteslist.add(player)
player.rect.x = 100
player.rect.y = 484


pygame.init()
#Set the width and height of the screen [width,height]
screen_height = 700
screen_width = 500
size=[screen_height,screen_width]
screen=pygame.display.set_mode(size)
#Name on top tab
pygame.display.set_caption("Move block")

done = False
clock=pygame.time.Clock()

#MAIN LOOP
while done == False:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Quit

        if event.type == pygame.KEYUP:
                    # If it is an arrow key, reset vector back to zero
                    if event.key == pygame.K_LEFT:
                        None      

    keyDown = pygame.key.get_pressed()
    if keyDown[pygame.K_RIGHT]:
        player.move(3, 0)   
    if keyDown[pygame.K_LEFT]:
        player.move(-3, 0)        
    if keyDown[pygame.K_DOWN]:
        player.move(0, 3)                        
    if keyDown[pygame.K_UP]:
        player.move(0,-3)

    #If player hits side of screen, do this
    if player.rect.x < 0:
        player.rect.x = 0
    if player.rect.x > 684:
        player.rect.x = 684
    if player.rect.y < 0:
        player.rect.y = 0
    if player.rect.y > 484:
        player.rect.y = 484    


    #Clear screen
    screen.fill(white)

    #Drawing
    spriteslist.draw(screen)
    pygame.display.flip()
    #FPS Lock
    clock.tick(60)

    #Update screen
    pygame.display.flip()

# Close the window and quit.
pygame.quit()




I will do your suggestion and make a separate module to draw the different cars and then import them to the game module when needed. I will also use the example Bavaai showed when he cleaned up my program.
Was This Post Helpful? 0
  • +
  • -

#22 albert003   User is online

  • D.I.C Addict

Reputation: 35
  • View blog
  • Posts: 734
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 08 August 2019 - 07:51 PM

I have a new question for you guys. I've almost finished drawing the various cars in the game and I am in the process of making the cars move. My goal is to make each car appear from the bottom of the screen from a function (Each car will have its own function which was used to create it), then the car will move to the top of the screen. At a certain time in the while loop each car will be called from a function. I wanted to make sure all of the cars move smoothly across the screen and then work on moving the back ground with the car at the same time. I've tried different ways and I can't get it to work.

I know in python I can do this...


	start = 0
	while True:#pygame loop
		if start == 1:
			redcar()
		elif start == 3:
			vwcar()
		elif start == 5:
			limocar()
		elif start == 6:
			playercar()
		elif start == 10:
			start = 0
		start +=1


Would someone please show me how to do that in pygame.
Was This Post Helpful? 0
  • +
  • -

#23 albert003   User is online

  • D.I.C Addict

Reputation: 35
  • View blog
  • Posts: 734
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 08 August 2019 - 08:22 PM

Disregard my previous post, I figured it out shortly after posting my question. I will use the example Bavaai suggested earlier, I made this separate program to draw the cars and move them. I will import it to the main game on a separate module. This is how I solved it.
while True:
    screen.fill(black)
    grass()
    draw_dirtonsides()
    trees()
    draw_gamescore()
    highscore()
        #player_car()#cars(recty) my original idea
        #redcars()
        #vwcar()
        #limocar()
    insertcoin()
    sec = (pygame.time.get_ticks() - start)/1000
    if sec == 1:
        redcar()
    if sec == 3:
        vwcar()
    if sec == 5:
        limocar()
    if sec == 6:
        playercar()
    if sec == 9:
        sec = 0
    
 
    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
 
# Close the window and quit.
pygame.quit()

Was This Post Helpful? 0
  • +
  • -

#24 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7464
  • View blog
  • Posts: 15,471
  • Joined: 16-October 07

Re: Remaking a classic arcade game using python

Posted 09 August 2019 - 05:26 AM

So, in post #21 you basically lifted the code, whole cloth, from this tutorial: http://kidscancode.o...g-with-sprites/

That's fine, tutorials are good. But, now, it almost looks and sounds like you've ditched that. You also appear to be redrawing your screen every time, again.

You seem to be just randomly throwing stuff on the screen? Are you using that pygame.sprite.Group()? It seems to be the kind of thing that might help you.
Was This Post Helpful? 0
  • +
  • -

#25 albert003   User is online

  • D.I.C Addict

Reputation: 35
  • View blog
  • Posts: 734
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 12 August 2019 - 01:20 PM

I tried to draw the cars using sprites and DK320 suggested back in post 5 and 8 that it would be easier not to use sprites.

I am going to use your suggestion to make the game and create anther module to draw the cars as well as move them. which is what I was working on in my previous post. Once I get the cars moving properly I will work on cleaning it up.
Was This Post Helpful? 0
  • +
  • -

#26 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 550
  • View blog
  • Posts: 1,721
  • Joined: 27-December 13

Re: Remaking a classic arcade game using python

Posted 13 August 2019 - 01:41 AM

@albert003,
You need a little consistency on this topic.
Until now you flicker from one subject to another and from one strategy to another.
This game is a big thing and you need to break it into some smaller components.

* Backgrounds; how do you want to make them? Full page or in segments (as suggested by baavgai). How do you want the scroll to work? How do you plan to differentiate between track and dirt?

* Sprites; (note, the term 'sprite' covers all moving parts, not only the 'pygame.Sprite' object). How do you plan to make the drawings? In a module using pygame? Imported images? What are the rules for moving the sprites (you, enemy, civil traffic)? What about the truck?

* Game dynamic; What are the rules for interaction between sprites? Will you use pygame's Sprite object? Or make your own class objects and collections?

There is no single 'correct' way to do this - but you should think it through and make a strategy. baavgai tend to use 'pygame.Sprite', I'd rather make my own classes. Both is ok - make your choice and see if it holds; be prepared, however, to revise the strategy if you have strong reasons to do so.
Make small 'sample code' demonstrating various properties: scrolling, collisions, user inter-phase, animation, control of AI sprites (other cars than 'you'), decorations (threes, bushes, etc.) - much of this can be made in separate modules and/or separate classes with associated methods.

As always: As long as you do your best the world is forgiving. You are welcome back here as often as needed. But take the time to work through the many aspects in an orderly manner - not all at the same time, and not changing approach every second post.
Was This Post Helpful? 1
  • +
  • -

#27 albert003   User is online

  • D.I.C Addict

Reputation: 35
  • View blog
  • Posts: 734
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted 14 August 2019 - 01:07 AM

Sorry I thought I posted my plan to make the program. I am breaking the program into small parts to be able to solve issues that I am either not sure how to do, movement of the back ground or how to make the cars move from different speeds (In the game the player can change from a slow gear (low) to a high gear (fast)). I've drawn the background of the game minus the tress and all of the cars. I am writing a small part of the game right now so I can troubleshoot the speed of the background, cars, score and random appearance of the enemy cars. I will use something I learned from working with you in various programs to make the back ground rise and once it reaches the top of the screen to appear again on the bottom of the screen. I will use one module to run the background and score, then import a second module which will have the various cars, speed and movement.

This is my plan for the game just to test the movement of the cars, background, score, etc. Once I figure out how to make it work properly I will then add the five maps and make the enemy cars randomly appear on the game screen. I am using an idea you gave me from a previous program to animate happy birthday to my wife to draw each car and map.

This is how I am planning to organise the game. Please critique and give me any advice you suggest to clean up the program. I have been able to make the cars move, but they blink and I am trying to figure out why.

#Using Bavaai's sketeton plan and some things Ive added

class Colour(Cars):
    pass#colours rbg of the game


def build_background(screen):
    def draw_grass():
        pass
    
    def map1():
        pass
    #draws dirt on side of road
    #Draws first map of the game

    def map2():
        pass
        #draws dirt on side of road
        #Draws 2nd map of the game

    def map3():
        pass
        #draws dirt on side of road
        #Draws thurd map of the game

    def map4():
        pass
        #draws dirt on side of road
        #Draws fourth map of the game

    def map5():
        pass
       #draws dirt on side of road
        #Draws fifth map of the game 

    def map6():
        pass
    #draws dirt on side of road
    #Draws sixth map of the game    

def drawgamescore():
    pass
    #draws on screen game score

def highscore():
    #once the game is over it will display the score
    #from game previous played
    pass

def upgrade():
    #Countdown when the player will get new
    #weapons and upgrades
    pass

def main():
    #contains the info of the game
    #screensize, font and game loop
    pass

############################
#imported module
import random

class Cars(object):
    def __init__(self, y):
        self.y = 700
    def player_car(self):
        #draws player car using lines,circles and arcs
        pass
    def vw_car(self):
        #draws vw car
        pass
    def red_car(self):
        #draws redcar
        pass
    def enforcer(self):
        #invinceable car
        pass
    def moto(self):
        #draws motorcycle
        pass
    def limo(self):
        #draws limo
        pass
    def chopper(self):
        #draws chopper that drops bombs on player
        pass
    def move(self):
        self.y += 1

def event():
    #this will have the game loop
    pass

p = Cars(700)
r = Cars(700)
Vw = Cars(700)
e = Cars(700)
m = Cars(700)
c = Cars(700)
#I will make a function for each car of the game
def playercar():
    pass
    #creates object of player car

def redcar():
    pass
    #creates object of redcar


def random():
    pass
#make cars randomlly appear on the screen
        




Was This Post Helpful? 0
  • +
  • -

#28 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7464
  • View blog
  • Posts: 15,471
  • Joined: 16-October 07

Re: Remaking a classic arcade game using python

Posted 14 August 2019 - 01:52 AM

Your Cars seems to be a catchall rather than a class. You're still no in that OOP groove.

I'd go with:
class Car(object):
    def __init__(self, y):
        self.y = y # stop ignoring the bloody y you pass 700

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

    # because this will be different for every car
    def draw(self, screen):
        pass

# creates object of player car
class PlayerCar(Car):
    def __init__(self, y):
        Car.__init__(self, y)
    def draw(self, screen):
        # draws the car

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


p = PlayerCar(700)
vw = VwCar(700)

# I will make a function for each car of the game
# no, put them in the class
def playercar():
    pass



I just did a pong brain dump on another thread, where I posted an entire game to their entire game. It mayn't be the best way to organize a game, but it's one way. Of note, it does properly use objects and sprites: https://www.dreaminc...ost__p__2403769

To be clear, a sprite is just an image and a location. This is something you should be doing on your own with anything you move, regardless. Also, you should never, ever, draw the same thing twice, at noted many times in this thread. Draw it once to a surface.

This post has been edited by baavgai: 14 August 2019 - 01:53 AM
Reason for edit:: evil grammar error

Was This Post Helpful? 1
  • +
  • -

#29 albert003   User is online

  • D.I.C Addict

Reputation: 35
  • View blog
  • Posts: 734
  • Joined: 15-December 14

Re: Remaking a classic arcade game using python

Posted Yesterday, 04:40 PM

Ok, I get that sprites are drawings on the screen. I honestly thought that the only way to make sprites was to make a class and instead of putting object you put in pygame.sprite.sprite in it. I read through Bavaai's example when I was stuck in the program I am working on and I still have the same questions I had when presented the example in the previous post. I put together the program the best I could and I have a few questions for you guys.

1. I'm still not sure what I am suppose to put in the method draw in the class Car. For example if the class Playercar is suppose to draw that car then why is there a method in the class car to draw?

2. I tried to make the program the best I could and when I run it, the cars don't appear on the screen. I tried to make the import file with the cars run as if it was a pygame program and the cars still didn't appear on the screen. Why?

This is the module that will be imported into the main game.



import pygame,random,math,sys
from time import sleep
from math import pi#
# Define some colors
size = (830, 700)
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#What am I suppose to draw here?
    
class PlayerCar(Car):
    def __init__(self, y):
        Car.__init__(self, y)
        
    def draw(self):
        pygame.draw.line(screen,white,[386,453 + self.y],[415,453 + self.y],10)#front wheels
        pygame.draw.line(screen,white,[386,483 + self.y],[415,483 +self.y],10)#back wheels
        pygame.draw.line(screen,white,[400,440 + self.y],[400,500 + self.y],20)#centre of car538 
        pygame.draw.line(screen,white,[390,442 + self.y],[390,498 + self.y],2)
        pygame.draw.line(screen,white,[389,444 + self.y],[389,496 + self.y],2)
        pygame.draw.line(screen,white,[388,446 + self.y],[388,494 + self.y],2)
        pygame.draw.line(screen,white,[387,448 + self.y],[387,492 + self.y],2)
        pygame.draw.line(screen,grey,[393,453 + self.y],[410,453 + self.y],18)#front hood
        pygame.draw.line(screen,black,[392,443 + self.y],[395,443 + self.y],2)#left head light
        pygame.draw.line(screen,black,[405,443 + self.y],[408,443 + self.y],2)#right head light    
        pygame.draw.line(screen,black,[397,447 + self.y],[397,449 + self.y],2)#front air intake
        pygame.draw.line(screen,black,[397,449 + self.y],[403,449 + self.y],2)#horizontal line
        pygame.draw.line(screen,black,[404,447 + self.y],[404,450 + self.y],2)
        pygame.draw.line(screen,white,[410,442 + self.y],[410,498 + self.y],2)
        pygame.draw.line(screen,white,[411,444 + self.y],[411,496 + self.y],2)
        pygame.draw.line(screen,white,[412,446 + self.y],[412,494 + self.y],2)
        pygame.draw.line(screen,white,[413,448 + self.y],[413,492 + self.y],2)
        pygame.draw.ellipse(screen,grey,[391,475 + self.y,25,27],11)#grey shade on back of car
        pygame.draw.line(screen,deepskyblue,[389,449 + self.y],[389,490 + self.y],3)
        pygame.draw.line(screen,deepskyblue,[413,449 + self.y],[413,490 + self.y],3)
        pygame.draw.line(screen,pink,[391,448 + self.y],[391,494 + self.y],3)
        pygame.draw.line(screen,pink,[411,448 + self.y],[411,494 + self.y],3)
        pygame.draw.line(screen,pink,[391,494 + self.y],[394,500 + self.y],3)#back left pink line
        pygame.draw.line(screen,pink,[411,494 + self.y],[408,500 + self.y],3)#back right pink line
        #pygame.draw.arc(screen,red,[50,50,50,50],0,pi)
        #first and second are x and y
        #third and fourth are the size
        pygame.draw.arc(screen,black,[392,457 + self.y,20,10],0,pi,5)#Front window
        pygame.draw.line(screen,black,[401,462 + self.y],[401,475 + self.y],22)#side windows
        pygame.draw.ellipse(screen,black,[390, 467 + self.y, 25, 27],11)
        pygame.draw.line(screen,white,[389,460 + self.y],[393,465 + self.y],2)#left front white line
        pygame.draw.line(screen,white,[407,465 + self.y],[413,460 + self.y],2)#right front white line
        pygame.draw.line(screen,white,[389,480 + self.y],[393,476 + self.y],2)#left centre white line
        pygame.draw.line(screen,white,[407,476 + self.y],[413,480 + self.y],2)#right centre white line
        pygame.draw.line(screen,black,[393,486 + self.y],[408,486 + self.y],7)#coves open area of ellipse
        pygame.draw.line(screen,white,[391,488 + self.y],[396,480 + self.y],2)#left rear line
        pygame.draw.line(screen,white,[406,479 + self.y],[408,488 + self.y],2)#right rear line
        pygame.draw.line(screen,white,[401,464 + self.y],[401,481 + self.y],13)#top of car
        

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

    def draw(self):
        pygame.draw.line(screen,red,[550,440 + self.y],[550,500 + self.y],18)#0 body of car
        pygame.draw.line(screen,red,[540,442 + self.y],[540,500 + self.y],3)#left body
        pygame.draw.line(screen,red,[537,444 + self.y],[537,500 + self.y],2)#left body
        pygame.draw.line(screen,red,[560,442 + self.y],[560,500 + self.y],3)#right body
        pygame.draw.line(screen,red,[562,444 + self.y],[562,500 + self.y],2)#right body
        pygame.draw.line(screen,red,[536,451 + self.y],[564,451 + self.y],10)#front wheels
        pygame.draw.line(screen,red,[536,488 + self.y],[564,488 + self.y],20)#back wheels
        pygame.draw.line(screen,yellow,[540,440 + self.y],[540,442 + self.y],3)#head light left
        pygame.draw.line(screen,yellow,[560,440 + self.y],[560,442 + self.y],3)#head light right
        pygame.draw.line(screen,yellow,[558,500 + self.y],[561,500 + self.y],3)#right rear light
        pygame.draw.line(screen,yellow,[540,500 + self.y],[543,500 + self.y],3)#left read light
        pygame.draw.line(screen,white,[544,440 + self.y],[556,440 + self.y],3)#front grill
        pygame.draw.line(screen,black,[546,445 + self.y],[554,445 + self.y],2)
        pygame.draw.line(screen,white,[538,445 + self.y],[539,453 + self.y],1)
        pygame.draw.line(screen,blue,[544,445 + self.y],[544,453 + self.y],2)
        pygame.draw.line(screen,white,[546,446 + self.y],[547,455 + self.y],1)
        pygame.draw.line(screen,white,[558,445 + self.y],[558,453 + self.y],1)        
        pygame.draw.line(screen,blue,[556,445 + self.y],[556,455 + self.y],2)
        pygame.draw.arc(screen,black,[540,457 + self.y,20,10],0,pi,5)
        pygame.draw.line(screen,black,[548,458 + self.y],[548,470 + self.y],10)
        pygame.draw.line(screen,black,[550,463 + self.y],[550,492 + self.y],23)
        pygame.draw.line(screen,black,[550,492 + self.y],[550,494 + self.y],15)
        pygame.draw.line(screen,grey,[550,463 + self.y],[550,487 + self.y],17)#grey shade top of windows
        pygame.draw.line(screen,red,[550,466 + self.y],[550,484 + self.y],14)#roof of car              
        pygame.draw.line(screen,red,[540,462 + self.y],[545,470 + self.y],2)#left line front window
        pygame.draw.line(screen,red,[560,458 + self.y],[555,469 + self.y],2)#right line front window
        pygame.draw.line(screen,red,[538,480 + self.y],[561,480 + self.y],3)
        pygame.draw.line(screen,red,[539,495 + self.y],[545,483 + self.y],3)
        pygame.draw.line(screen,red,[561,495 + self.y],[555,483 + self.y],3)
        pygame.draw.line(screen,pink,[550,467 + self.y],[550,482 + self.y],10) 
 
        
class VwCar(Car):
    def __init__(self, y):
        Car.__init__(self, y)

    def draw(self):
        pygame.draw.line(screen,deepskyblue,[400,440 + self.y],[400,482 + self.y],25)
        pygame.draw.line(screen,deepskyblue,[388,459 + self.y],[387,441 + self.y],4)#left front side bumper
        pygame.draw.line(screen,deepskyblue,[412,459 + self.y],[413,441 + self.y],4)
        pygame.draw.line(screen,black,[391,459 + self.y],[410,459 + self.y],5)#front window
        pygame.draw.line(screen,black,[410,464 + self.y],[410,470 + self.y],1)#right side window
        pygame.draw.line(screen,deepskyblue,[387,479 + self.y],[413,479 + self.y],6)#rear bumper
        pygame.draw.line(screen,deepskyblue,[389,482 + self.y],[411,482 + self.y],4)
        pygame.draw.line(screen,black,[410,472 + self.y],[407,476 + self.y],1)#rear side window right
        pygame.draw.line(screen,yellow,[388,440 + self.y],[394,440 + self.y],4)#left head light
        pygame.draw.line(screen,yellow,[408,440 + self.y],[414,440 + self.y],4)
        pygame.draw.line(screen,white,[396,439 + self.y],[406,439 + self.y],2)#front bumper
        pygame.draw.line(screen,red,[394,437 + self.y],[397,437 + self.y],2)#left bumper light red
        pygame.draw.line(screen,grey,[390,437 + self.y],[393,437 + self.y],2)#left bumper light grey
        pygame.draw.line(screen,grey,[406,437 + self.y],[409,437 + self.y],2)#right bumper light grey
        pygame.draw.line(screen,red,[402,437 + self.y],[405,437 + self.y],2)#right bumper light red
        pygame.draw.line(screen,white,[400,443 + self.y],[400,457 + self.y],2)#white line on trunk
        pygame.draw.line(screen,black,[401,442 + self.y],[401,457 + self.y],1)#black line on trunk
        pygame.draw.line(screen,white,[390,456 + self.y],[394,441 + self.y],2)#left white line forming trunk
        pygame.draw.line(screen,black,[397,476 + self.y],[404,476 + self.y],2)#rear window
        pygame.draw.line(screen,black,[395,476 + self.y],[390,474 + self.y],2)#left side window
        pygame.draw.line(screen,white,[395,474 + self.y],[390,472 + self.y],2)
        pygame.draw.line(screen,black,[387,479 + self.y],[397,483 + self.y],2)
        pygame.draw.line(screen,black,[403,482 + self.y],[410,480 + self.y],2)
        pygame.draw.line(screen,white,[390,474 + self.y],[390,464 + self.y],2)#left side window
        pygame.draw.line(screen,blue,[409,456 + self.y],[408,443 + self.y],3)#right blue line forming trunk
        pygame.draw.line(screen,white,[394,485 + self.y],[407,485 + self.y],2)#rear bumper
        pygame.draw.line(screen,red,[394,487 + self.y],[395,487 + self.y],3)#rear red light left
        pygame.draw.line(screen,red,[406,487 + self.y],[407,487 + self.y],3)#rear red light right




This is the game loop which will import the car module.


import pygame,random,math,sys
from time import sleep
size = (830, 700)
        
class Colour(object):
    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)

def build_background(screen):
    def draw_grass():
        pygame.draw.line(screen, Colour.darkgreen, [0, 0], [0, 800], 310)
        pygame.draw.line(screen, Colour.darkgreen, [735, 0], [735, 800], 270)
        
    def draw_trees():
        #medium tree
        pygame.draw.circle(screen, Colour.black, [125, 150], 12)
        pygame.draw.circle(screen, Colour.green, [136, 143], 1)
        pygame.draw.circle(screen, Colour.green, [128, 140], 1)
        pygame.draw.circle(screen, Colour.green, [136, 150], 1)
        pygame.draw.line(screen, Colour.green, [113,143],[121,143],2)
        pygame.draw.line(screen, Colour.green, [115,145],[121,145],2)##
        pygame.draw.line(screen, Colour.green, [110,151],[115,151],3)
        pygame.draw.circle(screen, Colour.green, [126,146],3)
        
    def draw_dirtonsides():
        # note, we needn't store this data, we are drawing it once
        pygame.draw.line(screen, Colour.carrot, [160, 0], [160, 800], 27)
        pygame.draw.line(screen, Colour.carrot, [590, 0], [590, 800], 27)
        #pygame.draw.line(screen, Colour.carrot, [640, 0], [640, 800], 27)
        #dirt_on_side_domain = list(range(143, 148)) + list(range(173, 180)) + list(range(621, 628)) + list(range(653, 663))
        dirt_on_side_domain = list(range(143, 148)) + list(range(173, 180)) + list(range(572, 578)) + list(range(600, 610))
        #red_speck_domain = list(range(155, 168)) + list(range(635, 645))
        red_speck_domain = list(range(155, 168)) + list(range(585, 603))
        for _ in range(1000):
            pt = random.choice(dirt_on_side_domain), random.randrange(0, 800)
            pygame.draw.circle(screen, Colour.carrot, pt, 1)
        for _ in range(400):
            pt = random.choice(red_speck_domain), random.randrange(0, 800)
            pygame.draw.circle(screen, Colour.red, pt, 1)
            
    screen.fill(Colour.black)
    draw_grass()
    draw_trees()
    draw_dirtonsides()
    import exSpyhuntercarmodule
    p = exSpyhuntercarmodule.PlayerCar(700)
    p.draw()
    
    return screen

def draw_gamescore(screen, myfont, score):
    scoretext = myfont.render(str(score).rjust(6,'0'),True,Colour.white)
    screen.blit(scoretext,(140,50))

def upgrade(screen, myfont, countdown):
    countdowntxt = myfont.render(str(countdown),True,Colour.white)
    screen.blit(countdowntxt,(350,50))

def highscore(screen, myfont,high):
    text = myfont.render("HI",True,Colour.white)
    screen.blit(text,(550,22))
    scoretext = myfont.render(high,True,Colour.white)
    screen.blit(scoretext,(510,50))
    
def main():
    # define score, or any state, outside the game loop
    done = False
    score = 0
    high = "020805"
    countdown = 999
    pygame.init()
    screen = pygame.display.set_mode(size)
    background = build_background(pygame.Surface(size))
    pygame.display.set_caption("Spy hunter")
    myfont = pygame.font.SysFont(None, 40)
    clock = pygame.time.Clock()

    # -------- Main Program Loop -----------
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
                # don't just mark done, bail from this loop
                break  # this leaves the for loop
        # check to see if we're done, before we go any farther
        if done:
            break  # this leaves the while loop
        screen.blit(background, (0, 0))
        draw_gamescore(screen, myfont, score)
        #score += 1
        #upgrade(screen, myfont, countdown)
        #countdown -= 1
        highscore(screen, myfont,high)
        pygame.display.flip()
        sleep(1)
        clock.tick(60)
        

    pygame.quit()

main()

Was This Post Helpful? 1
  • +
  • -

#30 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 550
  • View blog
  • Posts: 1,721
  • Joined: 27-December 13

Re: Remaking a classic arcade game using python

Posted Today, 04:09 AM

We are moving in circles here, but let me re-iterate:

Do NOT draw the car images to the screen. Not.
Draw the car images to a pygame surface of same (rectangular) size as the car.
This don't even has to be a module, can be a stand alone side program, drawing and saving the images once and for all - then the main can load those finished images at start.
This will make life so much easier for you...

Likewise, the function 'build_background' only draws one single image. Except line 56-58 which are utterly misplaced. Place the function in a stand-alone program, make the image, save it and load it in the main. Note that 'screen' in this function is different from pygame.display - to avoid any misunderstandings, I suggest you rename to 'img' or 'surf' or similar.

Answer to your question:
In your module you use 'draw' in two different contexts.
The high-level 'draw' (method in the Car class) should really blit the images to screen.
The 'draw' in the individual car sub-classes should be re-named e.g. to 'make_surface'.
As it is now, the individual methods override the general one.
But skip it and follow the above advise.
Was This Post Helpful? 0
  • +
  • -

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