Python

  1. Port your implementation of assignment 4 to Python. (Mario, the tubes, fireballs, and Goombas should all behave as in your other versions. You do not need to implement the AI.) Here's some starter code. If you save it to a file named "game.py", then you can execute it with the command "python game.py".
    from typing import Tuple
    import os
    os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
    import pygame
    import time
    import pygame.locals as pg
    from time import sleep
    
    class Turtle():
    	def __init__(self) -> None:
    		self.x = 100
    		self.y = 100
    		self.dest_x = self.x
    		self.dest_y = self.y
    		self.image = pygame.image.load("turtle.png")
    
    	def update(self) -> None:
    		if self.x < self.dest_x:
    			self.x += 1
    		if self.x > self.dest_x:
    			self.x -= 1
    		if self.y < self.dest_y:
    			self.y += 1
    		if self.y > self.dest_y:
    			self.y -= 1
    
    	def draw(self, screen: pygame.Surface) -> None:
    		rect_in = self.image.get_rect()
    		w = rect_in[2] - rect_in[0]
    		h = rect_in[3] - rect_in[1]
    		rect_out = (
    			self.x + rect_in[0] - w // 2,
    			self.y - h,
    			self.x + w,
    			self.y)
    		screen.blit(self.image, rect_out)
    
    class Model():
    	def __init__(self) -> None:
    		self.turtle = Turtle()
    
    	def update(self) -> None:
    		self.turtle.update()
    
    	def set_dest(self, pos: Tuple[int, int]) -> None:
    		self.turtle.dest_x = pos[0]
    		self.turtle.dest_y = pos[1]
    
    class View():
    	def __init__(self, model: Model) -> None:
    		screen_size = (800,600)
    		self.screen = pygame.display.set_mode(screen_size,
    			pygame.DOUBLEBUF | pygame.HWSURFACE, 32)
    		self.model = model
    
    	def update(self) -> None:
    		self.screen.fill([0,200,100])
    		self.model.turtle.draw(self.screen)
    		pygame.display.flip()
    
    class Controller():
    	def __init__(self, model: Model) -> None:
    		self.model = model
    		self.keep_going = True
    
    	def update(self) -> None:
    		for event in pygame.event.get():
    			if event.type == pg.QUIT:
    				self.keep_going = False
    			elif event.type == pg.KEYDOWN:
    				if event.key == pg.K_ESCAPE:
    					self.keep_going = False
    			elif event.type == pygame.MOUSEBUTTONUP:
    				self.model.set_dest(
    					pygame.mouse.get_pos())
    		keys = pygame.key.get_pressed()
    		if keys[pg.K_LEFT]:
    			self.model.turtle.dest_x -= 1
    		if keys[pg.K_RIGHT]:
    			self.model.turtle.dest_x += 1
    		if keys[pg.K_UP]:
    			self.model.turtle.dest_y -= 1
    		if keys[pg.K_DOWN]:
    			self.model.turtle.dest_y += 1
    
    print("Use the arrow keys to move. Press Esc to quit.")
    pygame.init()
    m = Model()
    v = View(m)
    c = Controller(m)
    while c.keep_going:
    	c.update()
    	m.update()
    	v.update()
    	sleep(0.02)
    print("Goodbye")
    


FAQ:

  1. Q: I'm getting a message about "pygame" not being found. What can I do about that?
    A: I used a package called "pygame". You probably want to use "pip" to install it. Use a search engine to find the exact command to install this package.